diff --git a/lib/entry-points.js b/lib/entry-points.js index 8768a09aa..f6b126c84 100644 --- a/lib/entry-points.js +++ b/lib/entry-points.js @@ -145838,7 +145838,10 @@ function cacheCommandOutput(key, cmd, output) { function getCachedCommandOutput(key, cmd) { const memoized = inMemoryCache.get(key); if (memoized !== void 0) { - return memoized.output; + if (cmd === void 0 || memoized.cmd === cmd) { + return memoized.output; + } + inMemoryCache.delete(key); } const entry = readCommandCacheFile()[key]; if (!isObject(entry) || !isString(entry.cmd) || cmd !== void 0 && entry.cmd !== cmd || !commandCacheValidators[key](entry.output)) { diff --git a/src/cli/output-cache.test.ts b/src/cli/output-cache.test.ts index c66af1491..e88e798cf 100644 --- a/src/cli/output-cache.test.ts +++ b/src/cli/output-cache.test.ts @@ -160,6 +160,21 @@ test.serial("cacheCommandOutput persists the output to the memo", async (t) => { }); }); +test.serial( + "getCachedCommandOutput treats a memoized output from another CLI as a miss", + async (t) => { + await withCacheDir(() => { + cacheCommandOutput(CommandCacheKey.Version, "/path/to/other-codeql", { + version: "2.20.0", + }); + t.is( + getCachedCommandOutput(CommandCacheKey.Version, "/path/to/codeql"), + undefined, + ); + }); + }, +); + test.serial( "getCachedCommandOutput prefers the in-memory memo over the file", async (t) => { @@ -181,3 +196,25 @@ test.serial( }); }, ); + +test.serial( + "getCachedCommandOutput falls back to file when memoized output comes from another CLI", + async (t) => { + await withCacheDir((cacheFilePath) => { + cacheCommandOutput(CommandCacheKey.Version, "/path/to/other-codeql", { + version: "2.19.0", + }); + writeCacheFile(cacheFilePath, { + [CommandCacheKey.Version]: { + cmd: "/path/to/codeql", + output: { version: "2.20.0", overlayVersion: 1 }, + }, + }); + + t.deepEqual( + getCachedCommandOutput(CommandCacheKey.Version, "/path/to/codeql"), + { version: "2.20.0", overlayVersion: 1 }, + ); + }); + }, +); diff --git a/src/cli/output-cache.ts b/src/cli/output-cache.ts index 144e27fd1..dd7685148 100644 --- a/src/cli/output-cache.ts +++ b/src/cli/output-cache.ts @@ -195,7 +195,12 @@ export function getCachedCommandOutput( // Tier 1: the in-memory variable. const memoized = inMemoryCache.get(key); if (memoized !== undefined) { - return memoized.output as CommandCacheKeyOutputMap[K]; + if (cmd === undefined || memoized.cmd === cmd) { + return memoized.output as CommandCacheKeyOutputMap[K]; + } + // If the memoized entry doesn't match the requested CLI, + // treat it as a miss and fall back to tier 2, the file. + inMemoryCache.delete(key); } // Tier 2: the temporary file persisted by an earlier step, if any.