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
22 changes: 20 additions & 2 deletions bin/provider-catalog-sync.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@
// merged effective config (default + overlay; --runtime-config) and skips
// the sync-owned providers on --apply; sync-models owns their writes.
//
// Config resolution (#4588): this tool's target is the tracked configured
// default — an explicit --config wins, then OPENCODE_CATALOG_SYNC_CONFIG,
// then <repo>/.opencode/opencode.jsonc. OPENCODE_CONFIG_PATH is never the
// target: it is sync-models' variable and since #4544 it names the runtime
// overlay, which this tool only reads (via --runtime-config) to compute the
// merged effective config.
//
// Sources: laptop proxies are enumerated over HTTP on 127.0.0.1 (run this on
// the laptop, or pass --source-dir with <port>.json snapshots for an offline
// reproducible run). models.dev is a cross-check for every provider and the
Expand Down Expand Up @@ -817,6 +824,18 @@ export function parseArgs(argv) {
return { options, flags };
}

// Resolution order for the catalog target (#4588): explicit --config,
// then the catalog-sync-specific OPENCODE_CATALOG_SYNC_CONFIG, then the
// tracked configured default. OPENCODE_CONFIG_PATH is deliberately not
// consulted — since #4544 it names the runtime overlay (sync-models' file).
export function resolveConfigPath({ options, env, root }) {
return (
options['--config'] ??
env.OPENCODE_CATALOG_SYNC_CONFIG ??
path.join(root, '.opencode', 'opencode.jsonc')
);
}

async function main(argv) {
const { options, flags } = parseArgs(argv);
if (flags.has('--help')) {
Expand All @@ -825,8 +844,7 @@ async function main(argv) {
}
const opt = (name, dflt) => options[name] ?? dflt;
const root = path.resolve(path.dirname(new URL(import.meta.url).pathname), '..');
const configPath =
process.env.OPENCODE_CONFIG_PATH || opt('--config', path.join(root, '.opencode', 'opencode.jsonc'));
const configPath = resolveConfigPath({ options, env: process.env, root });
const runtimeConfigPath = opt(
'--runtime-config',
process.env.OPENCODE_RUNTIME_CONFIG ||
Expand Down
48 changes: 47 additions & 1 deletion bin/provider-catalog-sync.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
assertNotDefaultBranch,
openPrCommands,
runSync,
resolveConfigPath,
deepMergeProviders,
SYNC_OWNED_PROVIDERS,
} from './provider-catalog-sync.mjs';
Expand Down Expand Up @@ -287,7 +288,9 @@ test('parseArgs accepts the documented controls', () => {

function runCli(args, env = {}) {
const clean = { ...process.env, ...env };
delete clean.OPENCODE_CONFIG_PATH;
// Scrub the ambient overlay variable unless a test passes it explicitly
// (#4588: the explicit-pass case is the regression under test).
if (!Object.hasOwn(env, 'OPENCODE_CONFIG_PATH')) delete clean.OPENCODE_CONFIG_PATH;
return spawnSync(process.execPath, [SCRIPT, ...args], { encoding: 'utf8', env: clean });
}

Expand Down Expand Up @@ -339,6 +342,49 @@ test('CLI control: dry run exits 3 without writing; --apply exits 0 and writes',
assert.ok(fs.readFileSync(configPath, 'utf8').includes('acme-2'));
});

// ---------------------------------------------------------------------------
// #4588: config resolution — OPENCODE_CONFIG_PATH names the runtime overlay
// since #4544 and must never become this tool's target
// ---------------------------------------------------------------------------

test('resolveConfigPath: --config beats env, catalog env beats default, OPENCODE_CONFIG_PATH ignored (#4588)', () => {
const root = '/repo';
const overlay = '/state/opencode-runtime.jsonc';
const dflt = path.join(root, '.opencode', 'opencode.jsonc');
// Explicit flag wins over every env var.
assert.equal(
resolveConfigPath({ options: { '--config': '/flag.jsonc' }, env: { OPENCODE_CONFIG_PATH: overlay, OPENCODE_CATALOG_SYNC_CONFIG: '/env.jsonc' }, root }),
'/flag.jsonc',
);
// Catalog-sync-specific env is the only env override.
assert.equal(
resolveConfigPath({ options: {}, env: { OPENCODE_CONFIG_PATH: overlay, OPENCODE_CATALOG_SYNC_CONFIG: '/env.jsonc' }, root }),
'/env.jsonc',
);
// OPENCODE_CONFIG_PATH alone changes nothing: tracked default.
assert.equal(resolveConfigPath({ options: {}, env: { OPENCODE_CONFIG_PATH: overlay }, root }), dflt);
assert.equal(resolveConfigPath({ options: {}, env: {}, root }), dflt);
});

test('CLI: OPENCODE_CONFIG_PATH pointing at the overlay does not redirect the target (#4588)', () => {
const { configPath, sourceDir, modelsDev } = controlFixture();
const dir = path.dirname(configPath);
const overlayPath = path.join(dir, 'opencode-runtime.jsonc');
fs.writeFileSync(overlayPath, JSON.stringify({ provider: { openai: { models: { 'g-1': {} } } } }));
const r = runCli(
['--config', configPath, '--runtime-config', overlayPath, '--models-dev', modelsDev, '--source-dir', sourceDir, '--json'],
{ OPENCODE_CONFIG_PATH: overlayPath },
);
Comment on lines +374 to +377

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Preserve the environment override in the CLI regression test

The runCli helper deletes clean.OPENCODE_CONFIG_PATH after merging the supplied environment overrides, so this test never launches the CLI with the overlay variable set. Consequently, it would still pass with the original buggy config-resolution expression in main, leaving the end-to-end regression unprotected despite the helper-level unit coverage. Remove inherited values before applying explicit overrides, or spawn the CLI directly here so the intended environment reaches the child process.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 0e4afc8 — you were right, thank you. runCli spread the caller's env and then unconditionally deleted OPENCODE_CONFIG_PATH, so the CLI test never actually launched with the variable set and would have passed against the buggy code.

runCli now scrubs the ambient variable only when the test does not pass it explicitly. Verified the regression bites, running the same fixtures (tracked default with commandcode/acme-1, source snapshot adding acme-2, overlay with openai/g-1) with OPENCODE_CONFIG_PATH=<overlay> set:

  • pre-fix script (base 26ce876): exit 0, report "config": ".../overlay.jsonc" — the bug
  • fixed script: exit 3, report "config": ".../tracked.jsonc" — validated addition found

Full suite: node --test bin/provider-catalog-sync.test.mjs — 30/30 pass.

@codex review

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Codex Review: Didn't find any major issues. Hooray!

Reviewed commit: 0e4afc875f

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

// The tracked file carries commandcode/acme-1 and the snapshot adds acme-2:
// a run against the tracked default reports the validated addition (exit 3).
// Against the overlay (the bug) it would report config=overlay and exit 0.
assert.equal(r.status, 3, `stdout: ${r.stdout} stderr: ${r.stderr}`);
const report = JSON.parse(r.stdout);
assert.equal(report.config, configPath);
assert.equal(report.runtime_overlay, overlayPath);
assert.ok(report.providers.commandcode, `providers: ${Object.keys(report.providers)}`);
});

// ---------------------------------------------------------------------------
// #4544: runtime overlay (sync-models-owned catalogs outside the tracked
// configured default)
Expand Down
Loading