Skip to content
This repository was archived by the owner on Apr 1, 2026. It is now read-only.

Commit e22af25

Browse files
committed
ci: fix test failures in CI by pre-populating models cache
Tests were failing in CI because the models.json cache file doesn't exist and the data() macro fallback only works at build time, not runtime. The preload now pre-fetches models.json and disables the background refresh to prevent race conditions during test execution.
1 parent 622caae commit e22af25

3 files changed

Lines changed: 15 additions & 0 deletions

File tree

packages/opencode/src/flag/flag.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export namespace Flag {
1111
export const OPENCODE_DISABLE_LSP_DOWNLOAD = truthy("OPENCODE_DISABLE_LSP_DOWNLOAD")
1212
export const OPENCODE_ENABLE_EXPERIMENTAL_MODELS = truthy("OPENCODE_ENABLE_EXPERIMENTAL_MODELS")
1313
export const OPENCODE_DISABLE_AUTOCOMPACT = truthy("OPENCODE_DISABLE_AUTOCOMPACT")
14+
export const OPENCODE_DISABLE_MODELS_FETCH = truthy("OPENCODE_DISABLE_MODELS_FETCH")
1415
export const OPENCODE_FAKE_VCS = process.env["OPENCODE_FAKE_VCS"]
1516
export const OPENCODE_CLIENT = process.env["OPENCODE_CLIENT"] ?? "cli"
1617

packages/opencode/src/provider/models.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import path from "path"
44
import z from "zod"
55
import { data } from "./models-macro" with { type: "macro" }
66
import { Installation } from "../installation"
7+
import { Flag } from "../flag/flag"
78

89
export namespace ModelsDev {
910
const log = Log.create({ service: "models.dev" })
@@ -83,6 +84,7 @@ export namespace ModelsDev {
8384
}
8485

8586
export async function refresh() {
87+
if (Flag.OPENCODE_DISABLE_MODELS_FETCH) return
8688
const file = Bun.file(filepath)
8789
log.info("refreshing", {
8890
file,

packages/opencode/test/preload.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,18 @@ process.env["XDG_CACHE_HOME"] = path.join(dir, "cache")
1111
process.env["XDG_CONFIG_HOME"] = path.join(dir, "config")
1212
process.env["XDG_STATE_HOME"] = path.join(dir, "state")
1313

14+
// Pre-fetch models.json so tests don't need the macro fallback
15+
// Also write the cache version file to prevent global/index.ts from clearing the cache
16+
const cacheDir = path.join(dir, "cache", "opencode")
17+
await fs.mkdir(cacheDir, { recursive: true })
18+
await fs.writeFile(path.join(cacheDir, "version"), "14")
19+
const response = await fetch("https://models.dev/api.json")
20+
if (response.ok) {
21+
await fs.writeFile(path.join(cacheDir, "models.json"), await response.text())
22+
}
23+
// Disable models.dev refresh to avoid race conditions during tests
24+
process.env["OPENCODE_DISABLE_MODELS_FETCH"] = "true"
25+
1426
// Clear provider env vars to ensure clean test state
1527
delete process.env["ANTHROPIC_API_KEY"]
1628
delete process.env["OPENAI_API_KEY"]

0 commit comments

Comments
 (0)