diff --git a/bin/check-opencode-metered-providers.mjs b/bin/check-opencode-metered-providers.mjs index e856a1a173..3a63959b35 100644 --- a/bin/check-opencode-metered-providers.mjs +++ b/bin/check-opencode-metered-providers.mjs @@ -1,5 +1,6 @@ #!/usr/bin/env node +import { realpathSync } from 'node:fs'; import { readFile } from 'node:fs/promises'; import { resolve } from 'node:path'; import { fileURLToPath } from 'node:url'; @@ -356,6 +357,22 @@ async function main() { console.log(`Metered-provider config guard passed: ${configPath}`); } -if (process.argv[1] && resolve(process.argv[1]) === fileURLToPath(import.meta.url)) { +// Entry-point check: compare realpaths, not the raw strings (#4694; +// same pattern as proxy-supervise.mjs / #2083 and +// provider-catalog-sync.mjs / #2235). process.argv[1] keeps the path +// exactly as invoked (e.g. through a file or directory symlink) while +// import.meta.url is the resolved module URL, so a resolve() string +// compare silently never matches and main() never runs via a symlink -- +// the guard exited 0 without checking anything. +const isMain = (() => { + try { + if (!process.argv[1]) return false; + return realpathSync(process.argv[1]) === realpathSync(fileURLToPath(import.meta.url)); + } catch { + return false; + } +})(); + +if (isMain) { await main(); } diff --git a/bin/check-opencode-metered-providers.test.mjs b/bin/check-opencode-metered-providers.test.mjs index e4b97839e6..07344a58db 100644 --- a/bin/check-opencode-metered-providers.test.mjs +++ b/bin/check-opencode-metered-providers.test.mjs @@ -178,3 +178,61 @@ test('rejects a PAYG provider in a provider group named providers', () => { assert.match(issues[0], /provider-route/); assert.ok(issues[0].includes('$.experimental.provider_routes.providers.providers[0]')); }); + +// #4694: the entry-point check must run main() when the guard is invoked +// through a symlink (file or directory). Before the realpath fix both +// invocations silently exited 0 without checking anything. +import { spawnSync } from 'node:child_process'; +import { mkdtempSync, symlinkSync, writeFileSync } from 'node:fs'; +import { tmpdir } from 'node:os'; +import { dirname, join } from 'node:path'; +import { fileURLToPath, pathToFileURL } from 'node:url'; + +const guardPath = fileURLToPath(new URL('./check-opencode-metered-providers.mjs', import.meta.url)); +const meteredConfig = '{ "model": "deepseek/deepseek-chat" }'; +const cleanCliConfig = '{ "model": "openai/gpt-6" }'; + +function runGuard(scriptPath, configSource) { + const dir = mkdtempSync(join(tmpdir(), 'metered-guard-')); + const configPath = join(dir, 'config.jsonc'); + writeFileSync(configPath, configSource); + return spawnSync(process.execPath, [scriptPath, configPath], { encoding: 'utf8' }); +} + +test('guard runs when invoked through a file symlink (metered config fails)', () => { + const dir = mkdtempSync(join(tmpdir(), 'metered-guard-link-')); + const linkPath = join(dir, 'guard-link.mjs'); + symlinkSync(guardPath, linkPath); + const res = runGuard(linkPath, meteredConfig); + assert.equal(res.status, 1); + assert.match(res.stderr, /Metered-provider config guard failed/); + assert.match(res.stderr, /deepseek/); +}); + +test('guard runs when invoked through a file symlink (clean config passes)', () => { + const dir = mkdtempSync(join(tmpdir(), 'metered-guard-link-')); + const linkPath = join(dir, 'guard-link.mjs'); + symlinkSync(guardPath, linkPath); + const res = runGuard(linkPath, cleanCliConfig); + assert.equal(res.status, 0); + assert.match(res.stdout, /Metered-provider config guard passed/); +}); + +test('guard runs when invoked through a symlinked directory (metered config fails)', () => { + const dir = mkdtempSync(join(tmpdir(), 'metered-guard-dirlink-')); + const linkDir = join(dir, 'bin-link'); + symlinkSync(dirname(guardPath), linkDir, 'dir'); + const res = runGuard(join(linkDir, 'check-opencode-metered-providers.mjs'), meteredConfig); + assert.equal(res.status, 1); + assert.match(res.stderr, /Metered-provider config guard failed/); +}); + +test('importing the guard module does not run main()', () => { + const res = spawnSync( + process.execPath, + ['--input-type=module', '-e', `import ${JSON.stringify(pathToFileURL(guardPath).href)};`], + { encoding: 'utf8' }, + ); + assert.equal(res.status, 0); + assert.doesNotMatch(res.stdout + res.stderr, /Metered-provider config guard/); +});