From eaca28258d086d518f46850cd74703ea23bb90c2 Mon Sep 17 00:00:00 2001 From: "Michael B. Gale" Date: Fri, 9 Oct 2026 15:13:16 +0100 Subject: [PATCH] Make `getCodeQLForCmd` use `Env` --- lib/entry-points.js | 38 ++++++++++++++++++++--------------- src/actions-util.ts | 2 +- src/cli/output-cache.ts | 4 ++-- src/codeql.ts | 44 ++++++++++++++++++++++++----------------- 4 files changed, 51 insertions(+), 37 deletions(-) diff --git a/lib/entry-points.js b/lib/entry-points.js index 79737f201..99e6fe83b 100644 --- a/lib/entry-points.js +++ b/lib/entry-points.js @@ -177046,13 +177046,13 @@ async function getCodeQL(logger2, cmd) { } return cachedCodeQL; } -async function getCodeQLForCmd(logger2, cmd, checkVersion) { +async function getCodeQLForCmd(logger2, cmd, checkVersion, env = getEnv()) { const codeql = { getPath() { return cmd; }, async getVersion() { - const cacheFilePath = getCommandCacheFilePath(getEnv()); + const cacheFilePath = getCommandCacheFilePath(env); let result = getCachedCodeQlVersion( logger2, cacheFilePath, @@ -177162,14 +177162,17 @@ async function getCodeQLForCmd(logger2, cmd, checkVersion) { } }, async runAutobuild(config, language) { - applyAutobuildAzurePipelinesTimeoutFix(); + applyAutobuildAzurePipelinesTimeoutFix(env); const autobuildCmd = path15.join( await this.resolveExtractor(language), "tools", process.platform === "win32" ? "autobuild.cmd" : "autobuild.sh" ); if (config.debugMode) { - process.env["CODEQL_VERBOSITY" /* CLI_VERBOSITY */] = process.env["CODEQL_VERBOSITY" /* CLI_VERBOSITY */] || EXTRACTION_DEBUG_MODE_VERBOSITY; + env.set( + "CODEQL_VERBOSITY" /* CLI_VERBOSITY */, + env.getOptional("CODEQL_VERBOSITY" /* CLI_VERBOSITY */) ?? EXTRACTION_DEBUG_MODE_VERBOSITY + ); } await runCli(autobuildCmd); }, @@ -177186,7 +177189,7 @@ async function getCodeQLForCmd(logger2, cmd, checkVersion) { }, async extractUsingBuildMode(config, language) { if (config.buildMode === "autobuild" /* Autobuild */) { - applyAutobuildAzurePipelinesTimeoutFix(); + applyAutobuildAzurePipelinesTimeoutFix(env); } try { await runCli(cmd, [ @@ -177288,7 +177291,7 @@ async function getCodeQLForCmd(logger2, cmd, checkVersion) { "--sarif-group-rules-by-pack", "--sarif-include-query-help=always", "--sublanguage-file-coverage", - ...await getJobRunUuidSarifOptions(), + ...await getJobRunUuidSarifOptions(env), ...getExtraOptionsFromEnv(["database", "interpret-results"]) ]; if (sarifRunPropertyFlag !== void 0) { @@ -177463,7 +177466,7 @@ Output was: ${extractorPath}` throw new ConfigurationError( `Expected a CodeQL CLI with version at least ${CODEQL_MINIMUM_VERSION} but got version ${(await codeql.getVersion()).version}` ); - } else if (checkVersion && process.env["CODEQL_ACTION_SUPPRESS_DEPRECATED_SOON_WARNING" /* SUPPRESS_DEPRECATED_SOON_WARNING */] !== "true" && !await codeQlVersionAtLeast(codeql, CODEQL_NEXT_MINIMUM_VERSION)) { + } else if (checkVersion && env.getOptional("CODEQL_ACTION_SUPPRESS_DEPRECATED_SOON_WARNING" /* SUPPRESS_DEPRECATED_SOON_WARNING */) !== "true" && !await codeQlVersionAtLeast(codeql, CODEQL_NEXT_MINIMUM_VERSION)) { const result = await codeql.getVersion(); core12.warning( `CodeQL CLI version ${result.version} was discontinued on ${GHES_MOST_RECENT_DEPRECATION_DATE} alongside GitHub Enterprise Server ${GHES_VERSION_MOST_RECENTLY_DEPRECATED} and will not be supported by the next minor release of the CodeQL Action. Please update to CodeQL CLI version ${CODEQL_NEXT_MINIMUM_VERSION} or later. For instance, if you have specified a custom version of the CLI using the 'tools' input to the 'init' Action, you can remove this input to use the default version. @@ -177570,16 +177573,19 @@ function getGeneratedCodeScanningConfigPath(config) { function getExtractionVerbosityArguments(enableDebugLogging) { return enableDebugLogging ? [`--verbosity=${EXTRACTION_DEBUG_MODE_VERBOSITY}`] : []; } -function applyAutobuildAzurePipelinesTimeoutFix() { - const javaToolOptions = process.env["JAVA_TOOL_OPTIONS"] || ""; - process.env["JAVA_TOOL_OPTIONS"] = [ - ...javaToolOptions.split(/\s+/), - "-Dhttp.keepAlive=false", - "-Dmaven.wagon.http.pool=false" - ].join(" "); +function applyAutobuildAzurePipelinesTimeoutFix(env) { + const javaToolOptions = env.getOptional("JAVA_TOOL_OPTIONS") ?? ""; + env.set( + "JAVA_TOOL_OPTIONS", + [ + ...javaToolOptions.split(/\s+/), + "-Dhttp.keepAlive=false", + "-Dmaven.wagon.http.pool=false" + ].join(" ") + ); } -async function getJobRunUuidSarifOptions() { - const jobRunUuid = process.env["CODEQL_ACTION_JOB_RUN_UUID" /* JOB_RUN_UUID */]; +async function getJobRunUuidSarifOptions(env) { + const jobRunUuid = env.getOptional("CODEQL_ACTION_JOB_RUN_UUID" /* JOB_RUN_UUID */); return jobRunUuid ? [`--sarif-run-property=jobRunUuid=${jobRunUuid}`] : []; } diff --git a/src/actions-util.ts b/src/actions-util.ts index e43abbb0b..48ff7755c 100644 --- a/src/actions-util.ts +++ b/src/actions-util.ts @@ -72,7 +72,7 @@ export const getOptionalInput = function (name: string): string | undefined { * directory that has been set in `CODEQL_ACTION_TEMP` by e.g. a previous step, or the * value of `RUNNER_TEMP` otherwise. */ -export function getTemporaryDirectory(env: Env = getEnv()): string { +export function getTemporaryDirectory(env: ReadOnlyEnv = getEnv()): string { return ( env.getOptional(EnvVar.TEMP) ?? env.getRequired(ActionsEnvVars.RUNNER_TEMP) ); diff --git a/src/cli/output-cache.ts b/src/cli/output-cache.ts index fb5deb1ad..209df575d 100644 --- a/src/cli/output-cache.ts +++ b/src/cli/output-cache.ts @@ -2,7 +2,7 @@ import * as fs from "fs"; import path from "path"; import { getTemporaryDirectory } from "../actions-util"; -import { Env } from "../environment"; +import { ReadOnlyEnv } from "../environment"; import * as json from "../json"; import { Logger } from "../logging"; @@ -51,7 +51,7 @@ export function resetCachedCodeQlVersion(): void { * Returns the path to the temporary file that backs the * on-disk cache of CLI responses between workflow steps. */ -export function getCommandCacheFilePath(env: Env): string { +export function getCommandCacheFilePath(env: ReadOnlyEnv): string { return path.join(getTemporaryDirectory(env), COMMAND_CACHE_FILENAME); } diff --git a/src/codeql.ts b/src/codeql.ts index 6a0440321..2954ce1d6 100644 --- a/src/codeql.ts +++ b/src/codeql.ts @@ -18,7 +18,7 @@ import { CliError, wrapCliConfigurationError } from "./cli-errors"; import type { ActionConfig } from "./config/action-config"; import { appendExtraQueryExclusions, type Config } from "./config-utils"; import { DocUrl } from "./doc-url"; -import { EnvVar, getEnv, exportEnvVar } from "./environment"; +import { EnvVar, getEnv, exportEnvVar, Env, ReadOnlyEnv } from "./environment"; import { CodeQLDefaultVersionInfo, Feature, @@ -495,8 +495,9 @@ export function createStubCodeQL(partialCodeql: Partial): CodeQL { export async function getCodeQLForTesting( cmd = "codeql-for-testing", logger: Logger = getRunnerLogger(true), + env: Env = getEnv(), ): Promise { - return getCodeQLForCmd(logger, cmd, false); + return getCodeQLForCmd(logger, cmd, false, env); } /** @@ -511,13 +512,14 @@ async function getCodeQLForCmd( logger: Logger, cmd: string, checkVersion: boolean, + env: Env = getEnv(), ): Promise { const codeql: CodeQL = { getPath() { return cmd; }, async getVersion() { - const cacheFilePath = outputCache.getCommandCacheFilePath(getEnv()); + const cacheFilePath = outputCache.getCommandCacheFilePath(env); let result = outputCache.getCachedCodeQlVersion( logger, cacheFilePath, @@ -643,7 +645,7 @@ async function getCodeQLForCmd( } }, async runAutobuild(config: Config, language: Language) { - applyAutobuildAzurePipelinesTimeoutFix(); + applyAutobuildAzurePipelinesTimeoutFix(env); const autobuildCmd = path.join( await this.resolveExtractor(language), @@ -653,8 +655,11 @@ async function getCodeQLForCmd( // Bump the verbosity of the autobuild command if we're in debug mode if (config.debugMode) { - process.env[EnvVar.CLI_VERBOSITY] = - process.env[EnvVar.CLI_VERBOSITY] || EXTRACTION_DEBUG_MODE_VERBOSITY; + env.set( + EnvVar.CLI_VERBOSITY, + env.getOptional(EnvVar.CLI_VERBOSITY) ?? + EXTRACTION_DEBUG_MODE_VERBOSITY, + ); } // On macOS, System Integrity Protection (SIP) typically interferes with @@ -686,7 +691,7 @@ async function getCodeQLForCmd( }, async extractUsingBuildMode(config: Config, language: Language) { if (config.buildMode === BuildMode.Autobuild) { - applyAutobuildAzurePipelinesTimeoutFix(); + applyAutobuildAzurePipelinesTimeoutFix(env); } try { await runCli(cmd, [ @@ -818,7 +823,7 @@ async function getCodeQLForCmd( "--sarif-group-rules-by-pack", "--sarif-include-query-help=always", "--sublanguage-file-coverage", - ...(await getJobRunUuidSarifOptions()), + ...(await getJobRunUuidSarifOptions(env)), ...getExtraOptionsFromEnv(["database", "interpret-results"]), ]; if (sarifRunPropertyFlag !== undefined) { @@ -1042,7 +1047,7 @@ async function getCodeQLForCmd( ); } else if ( checkVersion && - process.env[EnvVar.SUPPRESS_DEPRECATED_SOON_WARNING] !== "true" && + env.getOptional(EnvVar.SUPPRESS_DEPRECATED_SOON_WARNING) !== "true" && !(await util.codeQlVersionAtLeast(codeql, CODEQL_NEXT_MINIMUM_VERSION)) ) { const result = await codeql.getVersion(); @@ -1264,17 +1269,20 @@ function getExtractionVerbosityArguments( * Without the fix, long build processes will timeout when pulling down Java packages * https://developercommunity.visualstudio.com/content/problem/292284/maven-hosted-agent-connection-timeout.html */ -function applyAutobuildAzurePipelinesTimeoutFix() { - const javaToolOptions = process.env["JAVA_TOOL_OPTIONS"] || ""; - process.env["JAVA_TOOL_OPTIONS"] = [ - ...javaToolOptions.split(/\s+/), - "-Dhttp.keepAlive=false", - "-Dmaven.wagon.http.pool=false", - ].join(" "); +function applyAutobuildAzurePipelinesTimeoutFix(env: Env) { + const javaToolOptions = env.getOptional("JAVA_TOOL_OPTIONS") ?? ""; + env.set( + "JAVA_TOOL_OPTIONS", + [ + ...javaToolOptions.split(/\s+/), + "-Dhttp.keepAlive=false", + "-Dmaven.wagon.http.pool=false", + ].join(" "), + ); } -async function getJobRunUuidSarifOptions() { - const jobRunUuid = process.env[EnvVar.JOB_RUN_UUID]; +async function getJobRunUuidSarifOptions(env: ReadOnlyEnv) { + const jobRunUuid = env.getOptional(EnvVar.JOB_RUN_UUID); return jobRunUuid ? [`--sarif-run-property=jobRunUuid=${jobRunUuid}`] : []; }