Prefer parent torch-config in host-repo torch subdir

This commit is contained in:
thePR0M3TH3AN committed 2026-02-25 15:04:02 -05:00
1 parent 45ce248945
commit 600c078cd3
2 files changed
+40 -2

No files matched your search

+20 -2
View File
@@ -69,9 +69,27 @@ export function getTorchConfigPath() {
if (explicitPath) return path.resolve(process.cwd(), explicitPath);
const localPath = path.resolve(process.cwd(), DEFAULT_CONFIG_PATH);
if (fs.existsSync(localPath)) return localPath;
const parentPath = path.resolve(process.cwd(), '..', DEFAULT_CONFIG_PATH);
const cwdBaseName = path.basename(process.cwd());
const parentPkgPath = path.resolve(process.cwd(), '..', 'package.json');
let parentLooksLikeHostRepo = false;
try {
if (fs.existsSync(parentPkgPath)) {
const parentPkgRaw = fs.readFileSync(parentPkgPath, 'utf8');
const parentPkg = JSON.parse(parentPkgRaw);
parentLooksLikeHostRepo = parentPkg && typeof parentPkg.name === 'string' && parentPkg.name !== 'torch-lock';
}
} catch {
parentLooksLikeHostRepo = false;
}
// Host-repo mode: when running from "<repo>/torch", prefer parent config.
if (cwdBaseName === 'torch' && parentLooksLikeHostRepo && fs.existsSync(parentPath)) {
return parentPath;
}
if (fs.existsSync(localPath)) return localPath;
if (fs.existsSync(parentPath)) return parentPath;
return localPath;
+20
View File
@@ -19,10 +19,12 @@ import { DEFAULT_RELAYS } from '../src/constants.mjs';
describe('torch-config', () => {
describe('getTorchConfigPath', () => {
const originalEnv = process.env.TORCH_CONFIG_PATH;
const originalCwd = process.cwd();
after(() => {
if (originalEnv === undefined) delete process.env.TORCH_CONFIG_PATH;
else process.env.TORCH_CONFIG_PATH = originalEnv;
process.chdir(originalCwd);
});
it('returns default path when env var is not set', () => {
@@ -34,6 +36,24 @@ describe('torch-config', () => {
process.env.TORCH_CONFIG_PATH = 'custom-config.json';
assert.strictEqual(getTorchConfigPath(), path.resolve(process.cwd(), 'custom-config.json'));
});
it('prefers parent torch-config.json in host-repo mode when cwd is <repo>/torch', () => {
delete process.env.TORCH_CONFIG_PATH;
const fixtureRoot = fs.mkdtempSync(path.join(process.cwd(), 'test-torch-config-host-'));
const hostPkgPath = path.join(fixtureRoot, 'package.json');
const parentConfigPath = path.join(fixtureRoot, 'torch-config.json');
const torchDir = path.join(fixtureRoot, 'torch');
const localConfigPath = path.join(torchDir, 'torch-config.json');
fs.mkdirSync(torchDir, { recursive: true });
fs.writeFileSync(hostPkgPath, JSON.stringify({ name: 'seedpass', version: '1.0.0' }), 'utf8');
fs.writeFileSync(parentConfigPath, JSON.stringify({ nostrLock: { namespace: 'parent' } }), 'utf8');
fs.writeFileSync(localConfigPath, JSON.stringify({ nostrLock: { namespace: 'local' } }), 'utf8');
process.chdir(torchDir);
assert.strictEqual(getTorchConfigPath(), path.resolve(parentConfigPath));
});
});
describe('parseTorchConfig', () => {