From 07458065b265d68620f92191b2b7e20f9787ddc8 Mon Sep 17 00:00:00 2001 From: root Date: Sat, 24 Jan 2026 21:12:14 +0800 Subject: [PATCH] fix(tests): prevent tests from overwriting user auth.json - Issue #10370 - Move Global import to after XDG env vars are set - Fixes critical bug where tests overwrote ~/.local/share/opencode/auth.json - xdg-basedir reads env vars at module import time - Tests now properly isolated in temp directory Root cause: Global was imported before XDG_DATA_HOME and related env vars were set, causing paths to resolve to real user home directory. This fix ensures all test data is written to the temp test directory instead of the user's actual OpenCode data directory. --- packages/opencode/test/preload.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/packages/opencode/test/preload.ts b/packages/opencode/test/preload.ts index 1cb7778623e2..681561c3a609 100644 --- a/packages/opencode/test/preload.ts +++ b/packages/opencode/test/preload.ts @@ -22,7 +22,12 @@ process.env["XDG_CACHE_HOME"] = path.join(dir, "cache") process.env["XDG_CONFIG_HOME"] = path.join(dir, "config") process.env["XDG_STATE_HOME"] = path.join(dir, "state") -// Write the cache version file to prevent global/index.ts from clearing the cache +// Import Global to trigger module initialization (creates data/cache/config directories) +// This must happen AFTER XDG env vars are set so paths resolve correctly +await import("../src/global") + +// Pre-fetch models.json so tests don't need the macro fallback +// Also write the cache version file to prevent global/index.ts from clearing the cache const cacheDir = path.join(dir, "cache", "opencode") await fs.mkdir(cacheDir, { recursive: true }) await fs.writeFile(path.join(cacheDir, "version"), "14")