Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion bin/proxy-supervise.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ import fs from 'node:fs';
import http from 'node:http';
import os from 'node:os';
import path from 'node:path';
import { fileURLToPath } from 'node:url';

// ---------- small utilities ----------

Expand Down Expand Up @@ -639,6 +640,20 @@ const main = async () => {
process.exit(failures > 0 ? 1 : 0);
};

if (import.meta.url === `file://${process.argv[1]}`) {
// Entry-point check: compare realpaths, not the raw strings.
// process.argv[1] keeps the path exactly as invoked (e.g. through the
// ~/code -> ~/git/code symlink) while import.meta.url is the resolved
// module URL, so a string compare silently never matches and main()
// never runs -- the supervisor timer no-oped that way (#2083).
const isMain = (() => {
try {
if (!process.argv[1]) return false;
return fs.realpathSync(process.argv[1]) === fs.realpathSync(fileURLToPath(import.meta.url));
} catch {
return false;
}
})();

if (isMain) {
main();
}
31 changes: 31 additions & 0 deletions bin/proxy-supervise.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -239,3 +239,34 @@ test('foreign healthy instance is adopted, not duplicated', async (t) => {
test('module under test is the worktree copy', () => {
assert.ok(fs.existsSync(path.join(here, 'proxy-supervise.mjs')));
});

test('CLI entry point still runs when invoked through a symlinked path (#2083)', async () => {
const root = fs.mkdtempSync(path.join(os.tmpdir(), 'proxy-supervise-symlink-'));
try {
const link = path.join(root, 'proxy-supervise-link.mjs');
fs.symlinkSync(path.join(here, 'proxy-supervise.mjs'), link);
const cfgFile = path.join(root, 'config.json');
fs.writeFileSync(
cfgFile,
JSON.stringify({
stateDir: path.join(root, 'state'),
endpointDir: path.join(root, 'endpoints'),
proxies: [
{ name: 'ghost-proxy', canonicalPort: 19999, portEnv: 'GHOST_PROXY_PORT', command: ['/bin/true'], hashFiles: [] },
],
}),
);
const out = await new Promise((resolve, reject) => {
const child = spawn(process.execPath, [link, '--config', cfgFile, '--only', 'ghost-proxy', '--status']);
let buf = '';
child.stdout.on('data', (d) => (buf += d));
child.on('error', reject);
child.on('close', () => resolve(buf));
});
// With the old string-compare guard, main() never ran through a
// symlink and this printed nothing at all.
assert.match(out, /"name":"ghost-proxy"/);
} finally {
fs.rmSync(root, { recursive: true, force: true });
}
});
Loading