-
Notifications
You must be signed in to change notification settings - Fork 479
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,11 +9,12 @@ import getFolderSize from "get-folder-size"; | |
| import * as yaml from "js-yaml"; | ||
| import * as semver from "semver"; | ||
|
|
||
| import { getTemporaryDirectory } from "./actions-util"; | ||
| import * as apiCompatibility from "./api-compatibility.json"; | ||
| import type { CodeQL, VersionInfo } from "./codeql"; | ||
| import type { Pack } from "./config/db-config"; | ||
| import type { Config } from "./config-utils"; | ||
| import { EnvVar, getRequiredEnvParam } from "./environment"; | ||
| import { Env, EnvVar, getEnv, getRequiredEnvParam } from "./environment"; | ||
| import * as json from "./json"; | ||
| import { Language } from "./languages"; | ||
| import { Logger } from "./logging"; | ||
|
|
@@ -638,7 +639,25 @@ function isPersistedVersionInfo(x: unknown): x is PersistedVersionInfo { | |
| ); | ||
| } | ||
|
|
||
| export function cacheCodeQlVersion(cmd: string, version: VersionInfo): void { | ||
| /** | ||
| * Returns the file path to the `codeql version` output cache. | ||
| * @param env The environment variables to use—only necessary for testing. | ||
|
mario-campos marked this conversation as resolved.
|
||
| */ | ||
| function getPathToCodeQLVersionCacheFile(env: Env): string { | ||
| return path.join(getTemporaryDirectory(env), "version.json"); | ||
| } | ||
|
Comment on lines
+646
to
+648
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are you planning to have a separate file for each command? If so, why?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, with this PR, I'm simply trying to do a direct environment-variable-to-file translation. In subsequent PRs, I plan to generalize the file to include additional commands. The only reason that the file's name is associated with the command is to keep this PR focused on this one change, without scope creep of future intentions. |
||
|
|
||
| /** | ||
| * 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—only necessary for testing. | ||
| */ | ||
| export function cacheCodeQlVersion( | ||
| cmd: string, | ||
| version: VersionInfo, | ||
| env: Env = getEnv(), | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Avoid the default |
||
| ): void { | ||
| if (cachedCodeQlVersion !== undefined) { | ||
| throw new Error("cacheCodeQlVersion() should be called only once"); | ||
| } | ||
|
|
@@ -647,21 +666,32 @@ export function cacheCodeQlVersion(cmd: string, version: VersionInfo): void { | |
| // 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. | ||
| core.exportVariable( | ||
| EnvVar.CODEQL_VERSION_INFO, | ||
| fs.writeFileSync( | ||
| getPathToCodeQLVersionCacheFile(env), | ||
| JSON.stringify({ cmd, version }), | ||
| "utf8", | ||
| ); | ||
| } | ||
|
|
||
| export function getCachedCodeQlVersion(cmd?: string): undefined | VersionInfo { | ||
| /** | ||
| * Returns the cached CodeQL CLI version, if any. | ||
| * @param cmd The path to the CodeQL CLI. | ||
| * @param env The environment variables to use—only necessary for testing. | ||
| */ | ||
| export function getCachedCodeQlVersion( | ||
| cmd?: string, | ||
| env: Env = getEnv(), | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Avoid the default |
||
| ): 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. | ||
| const serialized = process.env[EnvVar.CODEQL_VERSION_INFO]; | ||
| if (!serialized) { | ||
| let serialized: string; | ||
| try { | ||
| serialized = fs.readFileSync(getPathToCodeQLVersionCacheFile(env), "utf8"); | ||
| } catch { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The error here is swallowed, which might make it hard to troubleshoot issues. Consider logging it at debug-level. |
||
| return undefined; | ||
| } | ||
| let persisted: unknown; | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test, and others, probably don't need to be
serialanymore sinceprocess.envis no longer mutated.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I had the same thought, but Copilot assured me it's still necessary:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The point about
cachedCodeQlVersionis fair. That said, I wouldn't be opposed to refactoring that away so that the state is threaded to these functions rather than global as part of this PR.