-
Notifications
You must be signed in to change notification settings - Fork 480
Persist CodeQL version output to file rather than environment #4081
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
mario-campos
wants to merge
19
commits into
main
Choose a base branch
from
mario-campos/version-cache-to-disk
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
5f8c44b
Persist CodeQL version output to file rather than environment
mario-campos 9183a7b
Handle file-read errors as cache misses
mario-campos 208a88a
Simplify JSDoc of `getCachedCodeQlVersion`
mario-campos bfcd769
Fix JSDoc of `env` param
mario-campos 0e85c0e
Refactor unit test to extract testing values
mario-campos bb19330
Add test of `getCachedCodeQlVersion` with no file
mario-campos 4dc327a
Introduce basic `cli/output-cache.ts` module
mario-campos 1332611
Move cache-related util functions into dedicated module
mario-campos 246018e
Move `VersionInfo` to dedicated module
mario-campos 0a99875
Move `VersionInfo`-related types to `cli/output-cache.ts`
mario-campos 11569df
Update JSDoc of `getCachedCodeQlVersion`
mario-campos b222c3a
Generalize file cache data structure
mario-campos 40f80a8
Rename type to better match generic intention
mario-campos 33d7086
Pass environment explicitly to CLI caching functions
mario-campos a9baab8
Export CLI cache types
mario-campos 337136a
Rename `CommandCacheRecord` -> `OutputCache`
mario-campos bf96b0d
Expand test to ensure it does not throw an exception
mario-campos 6c0d901
Change `OutputCache` to use object for `entries`
mario-campos 6dc6332
Bolster output-cache unit tests with more test cases
mario-campos File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,121 @@ | ||
| import * as fs from "fs"; | ||
| import path from "path"; | ||
|
|
||
| import test from "ava"; | ||
|
|
||
| import { EnvVar } from "../environment"; | ||
| import { getTestEnv, setupTests } from "../testing-utils"; | ||
| import * as util from "../util"; | ||
|
|
||
| import * as outputCache from "./output-cache"; | ||
|
|
||
| setupTests(test); | ||
|
|
||
| test.serial( | ||
| "getCachedCodeQlVersion reuses a version persisted by an earlier step", | ||
| async (t) => { | ||
| await util.withTmpDir(async (tmpDir: string) => { | ||
| const cacheFile = path.join(tmpDir, "codeql-action-command-cache.json"); | ||
| fs.writeFileSync( | ||
| cacheFile, | ||
| JSON.stringify({ | ||
| cmd: "/path/to/codeql", | ||
| entries: { version: { version: "2.20.0" } }, | ||
| }), | ||
| "utf8", | ||
| ); | ||
| const env = getTestEnv({ [EnvVar.TEMP]: tmpDir }); | ||
| t.deepEqual(outputCache.getCachedCodeQlVersion(env, "/path/to/codeql"), { | ||
| version: "2.20.0", | ||
| }); | ||
| }); | ||
| }, | ||
| ); | ||
|
|
||
| test.serial( | ||
| "getCachedCodeQlVersion ignores a persisted version from a different CLI", | ||
| async (t) => { | ||
| await util.withTmpDir(async (tmpDir: string) => { | ||
| const cacheFile = path.join(tmpDir, "version.json"); | ||
| fs.writeFileSync( | ||
| cacheFile, | ||
| JSON.stringify({ | ||
| cmd: "/path/to/other-codeql", | ||
| version: { version: "2.20.0" }, | ||
| }), | ||
| "utf8", | ||
| ); | ||
| const env = getTestEnv({ [EnvVar.TEMP]: tmpDir }); | ||
| t.is( | ||
| outputCache.getCachedCodeQlVersion(env, "/path/to/codeql"), | ||
| undefined, | ||
| ); | ||
| }); | ||
| }, | ||
| ); | ||
|
|
||
| test.serial( | ||
| "getCachedCodeQlVersion ignores a malformed persisted value", | ||
| async (t) => { | ||
| await util.withTmpDir(async (tmpDir: string) => { | ||
| const cacheFile = path.join(tmpDir, "version.json"); | ||
| fs.writeFileSync(cacheFile, "not valid json", "utf8"); | ||
| const env = getTestEnv({ [EnvVar.TEMP]: tmpDir }); | ||
| t.is( | ||
| outputCache.getCachedCodeQlVersion(env, "/path/to/codeql"), | ||
| undefined, | ||
| ); | ||
| }); | ||
| }, | ||
| ); | ||
|
|
||
| test.serial( | ||
| "getCachedCodeQlVersion ignores a persisted value with the wrong structure", | ||
| async (t) => { | ||
| await util.withTmpDir(async (tmpDir: string) => { | ||
| const cacheFile = path.join(tmpDir, "version.json"); | ||
| const env = getTestEnv({ [EnvVar.TEMP]: tmpDir }); | ||
|
|
||
| const testValues = [ | ||
| { cmd: "/path/to/codeql" }, | ||
| { entries: { version: { version: "2.20.0" } } }, | ||
| { cmd: "/path/to/codeql", entries: {} }, | ||
| { cmd: "/path/to/codeql", entries: { version: {} } }, | ||
| { cmd: "/path/to/codeql", entries: { version: null } }, | ||
| { cmd: "/path/to/codeql", entries: { version: "2.20.0" } }, | ||
| { cmd: "/path/to/codeql", entries: { version: { version: null } } }, | ||
| { cmd: "/path/to/codeql", entries: { version: { version: 2.2 } } }, | ||
| { cmd: "/path/to/codeql", entries: { version: { version: 2 } } }, | ||
| { | ||
| cmd: "/path/to/codeql", | ||
| entries: { version: { version: "2.20.0", overlayVersion: "1" } }, | ||
| }, | ||
| { | ||
| cmd: "/path/to/codeql", | ||
| entries: { version: { version: "2.20.0", features: "nope" } }, | ||
| }, | ||
| ].map((v) => JSON.stringify(v)); | ||
|
|
||
| for (const value of testValues) { | ||
| fs.writeFileSync(cacheFile, value, "utf8"); | ||
| t.is( | ||
| outputCache.getCachedCodeQlVersion(env, "/path/to/codeql"), | ||
| undefined, | ||
| value, | ||
| ); | ||
| } | ||
| }); | ||
| }, | ||
| ); | ||
|
|
||
| test.serial("getCachedCodeQlVersion ignores non-existent file", async (t) => { | ||
| await util.withTmpDir(async (tmpDir: string) => { | ||
| const env = getTestEnv({ [EnvVar.TEMP]: tmpDir }); | ||
| t.notThrows(() => { | ||
| t.is( | ||
| outputCache.getCachedCodeQlVersion(env, "/path/to/codeql"), | ||
| undefined, | ||
| ); | ||
| }); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,160 @@ | ||
| import * as fs from "fs"; | ||
| import path from "path"; | ||
|
|
||
| import { getTemporaryDirectory } from "../actions-util"; | ||
| import { Env } from "../environment"; | ||
|
|
||
| import type { VersionInfo } from "./types"; | ||
|
|
||
| /** | ||
| * The keys of the command cache. Each key corresponds to a command whose output we cache. | ||
| */ | ||
| export enum CommandCacheKey { | ||
| Version = "version", | ||
| } | ||
|
|
||
| /** | ||
| * The mapping of CLI commands to the types of the output of each command that we cache. | ||
| */ | ||
| export type CommandCacheKeyOutputMap = { | ||
| [CommandCacheKey.Version]: VersionInfo; | ||
| }; | ||
|
|
||
| /** | ||
| * The type of the command cache that is persisted to disk. | ||
| */ | ||
| export interface OutputCache<K extends CommandCacheKey> { | ||
| cmd: string; | ||
| entries: { | ||
| [P in K]: CommandCacheKeyOutputMap[K]; | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * The name of the temporary file that backs the on-disk cache of | ||
| * CLI responses between workflow steps. | ||
| */ | ||
| const COMMAND_CACHE_FILENAME = "codeql-action-command-cache.json"; | ||
|
|
||
| /** | ||
| * The module-global variable that caches the CodeQL CLI version in-memory. | ||
| */ | ||
| let cachedCodeQlVersion: undefined | VersionInfo = undefined; | ||
|
|
||
| /** | ||
| * Resets the in-process cache of the CodeQL CLI version. Only for use in tests, | ||
| * which exercise multiple "steps" within a single process. | ||
| */ | ||
| export function resetCachedCodeQlVersion(): void { | ||
| cachedCodeQlVersion = undefined; | ||
| } | ||
|
|
||
| /** | ||
| * Returns the path to the temporary file that backs the | ||
| * on-disk cache of CLI responses between workflow steps. | ||
| */ | ||
| function getCommandCacheFilePath(env: Env): string { | ||
| return path.join(getTemporaryDirectory(env), COMMAND_CACHE_FILENAME); | ||
| } | ||
|
|
||
| /** | ||
| * Caches the CodeQL CLI version both in-memory and on disk. | ||
| * @param cmd The path to the CodeQL CLI. | ||
| * @param version The version information to cache. | ||
| * @param env The environment variables to use. | ||
| */ | ||
| export function cacheCodeQlVersion( | ||
| cmd: string, | ||
| version: VersionInfo, | ||
| env: Env, | ||
| ): void { | ||
| if (cachedCodeQlVersion !== undefined) { | ||
| throw new Error("cacheCodeQlVersion() should be called only once"); | ||
| } | ||
| cachedCodeQlVersion = version; | ||
| const outputCache = { | ||
| cmd, | ||
| entries: { [CommandCacheKey.Version]: version }, | ||
| } satisfies OutputCache<CommandCacheKey.Version>; | ||
| // Persist the version so that subsequent Actions steps, which run in separate | ||
| // processes, can reuse it rather than invoking `codeql version` again. We | ||
| // record the CLI path so that a different step using a different CodeQL bundle | ||
| // doesn't pick up a stale version. | ||
| fs.writeFileSync( | ||
| getCommandCacheFilePath(env), | ||
| JSON.stringify(outputCache), | ||
| "utf8", | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Returns the cached CodeQL CLI version, if any. | ||
| * @param env The environment variables to use. | ||
| * @param cmd The path to the CodeQL CLI. | ||
| */ | ||
| export function getCachedCodeQlVersion( | ||
| env: Env, | ||
| cmd?: string, | ||
| ): undefined | VersionInfo { | ||
| if (cachedCodeQlVersion !== undefined) { | ||
| return cachedCodeQlVersion; | ||
| } | ||
| // Fall back to the value persisted by an earlier Actions step, if any. This is | ||
| // best-effort: any malformed or mismatched value is ignored so that the caller | ||
| // invokes `codeql version` instead. | ||
| let serialized: string; | ||
| try { | ||
| serialized = fs.readFileSync(getCommandCacheFilePath(env), "utf8"); | ||
| } catch { | ||
| return undefined; | ||
| } | ||
| let persisted: unknown; | ||
| try { | ||
| persisted = JSON.parse(serialized); | ||
| } catch { | ||
| return undefined; | ||
| } | ||
| if ( | ||
| !isOutputCache(persisted) || | ||
| (cmd !== undefined && persisted.cmd !== cmd) | ||
| ) { | ||
| return undefined; | ||
| } | ||
| // Memoize the parsed value so that subsequent calls in this process don't | ||
| // re-parse the environment variable. | ||
| cachedCodeQlVersion = persisted.entries[CommandCacheKey.Version]; | ||
| return cachedCodeQlVersion; | ||
| } | ||
|
|
||
| /** | ||
| * Determines whether a value is a `VersionInfo` object. | ||
| * @param x The value to test | ||
| */ | ||
| function isVersionInfo(x: unknown): x is VersionInfo { | ||
| const candidate = x as Partial<VersionInfo> | null; | ||
| return ( | ||
| typeof candidate === "object" && | ||
| candidate !== null && | ||
| typeof candidate.version === "string" && | ||
| (candidate.features === undefined || | ||
| (typeof candidate.features === "object" && | ||
| candidate.features !== null)) && | ||
| (candidate.overlayVersion === undefined || | ||
| typeof candidate.overlayVersion === "number") | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Determines whether a value is a `OutputCache` object. | ||
| * @param x The value to test | ||
| */ | ||
| function isOutputCache(x: unknown): x is OutputCache<CommandCacheKey.Version> { | ||
| const candidate = x as Partial<OutputCache<CommandCacheKey.Version>> | null; | ||
| return ( | ||
| typeof candidate === "object" && | ||
| candidate !== null && | ||
| typeof candidate.cmd === "string" && | ||
| candidate.entries !== undefined && | ||
| isVersionInfo(candidate.entries[CommandCacheKey.Version]) | ||
| ); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| export interface VersionInfo { | ||
| version: string; | ||
| features?: { [name: string]: boolean }; | ||
| /** | ||
| * The overlay version helps deal with backward incompatible changes for | ||
| * overlay analysis. When a precompiled query pack reports the same overlay | ||
| * version as the CodeQL CLI, we can use the CodeQL CLI to perform overlay | ||
| * analysis with that pack. Otherwise, if the overlay versions are different, | ||
| * or if either the pack or the CLI does not report an overlay version, | ||
| * we need to revert to non-overlay analysis. | ||
| */ | ||
| overlayVersion?: number; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.