Skip to content

Commit 6d448e0

Browse files
committed
Allow runTool to inherit env
1 parent 7cd2203 commit 6d448e0

4 files changed

Lines changed: 76 additions & 32 deletions

File tree

‎lib/entry-points.js‎

Lines changed: 35 additions & 17 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/actions-util.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -361,9 +361,10 @@ const MAX_STDERR_BUFFER_SIZE = 20000;
361361
* Runs a CLI tool.
362362
*
363363
* @returns Standard output produced by the tool.
364-
* @throws A `CommandInvocationError` if the tool exits with a non-zero status code.
364+
* @throws A {@link CommandInvocationError} if the tool exits with a non-zero status code.
365365
*/
366366
export async function runTool(
367+
env: Env,
367368
cmd: string,
368369
args: string[] = [],
369370
opts: { stdin?: string; noStreamStdout?: boolean } = {},
@@ -374,6 +375,7 @@ export async function runTool(
374375
process.stdout.write(`[command]${cmd} ${args.join(" ")}\n`);
375376
}
376377
const exitCode = await new toolrunner.ToolRunner(cmd, args, {
378+
env: env.getDefined(),
377379
ignoreReturnCode: true,
378380
listeners: {
379381
stdout: (data: Buffer) => {

‎src/codeql.ts‎

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,7 @@ export async function getCodeQLForCmd(
514514
);
515515
if (result === undefined) {
516516
result = await runCliJson<VersionInfo>(
517+
env,
517518
cmd,
518519
["version", "--format=json"],
519520
{
@@ -597,6 +598,7 @@ export async function getCodeQLForCmd(
597598
: ["--no-calculate-baseline"];
598599

599600
await runCli(
601+
env,
600602
cmd,
601603
[
602604
"database",
@@ -663,10 +665,10 @@ export async function getCodeQLForCmd(
663665
// When `DYLD_INSERT_LIBRARIES` is set in the environment for a step,
664666
// the Actions runtime introduces its own workaround for SIP
665667
// (https://github.com/actions/runner/pull/416).
666-
await runCli(autobuildCmd);
668+
await runCli(env, autobuildCmd);
667669
},
668670
async extractScannedLanguage(config: Config, language: Language) {
669-
await runCli(cmd, [
671+
await runCli(env, cmd, [
670672
"database",
671673
"trace-command",
672674
"--index-traceless-dbs",
@@ -681,7 +683,7 @@ export async function getCodeQLForCmd(
681683
applyAutobuildAzurePipelinesTimeoutFix(env);
682684
}
683685
try {
684-
await runCli(cmd, [
686+
await runCli(env, cmd, [
685687
"database",
686688
"trace-command",
687689
"--use-build-mode",
@@ -720,7 +722,7 @@ export async function getCodeQLForCmd(
720722
...getExtraOptionsFromEnv(["database", "finalize"]),
721723
databasePath,
722724
];
723-
await runCli(cmd, args);
725+
await runCli(env, cmd, args);
724726
},
725727
async resolveLanguages(
726728
{
@@ -729,7 +731,7 @@ export async function getCodeQLForCmd(
729731
filterToLanguagesWithQueries: boolean;
730732
} = { filterToLanguagesWithQueries: false },
731733
) {
732-
return runCliJson<ResolveLanguagesOutput>(cmd, [
734+
return runCliJson<ResolveLanguagesOutput>(env, cmd, [
733735
"resolve",
734736
"languages",
735737
"--format=betterjson",
@@ -758,7 +760,11 @@ export async function getCodeQLForCmd(
758760
if (workingDir !== undefined) {
759761
codeqlArgs.push("--working-dir", workingDir);
760762
}
761-
return await runCliJson<ResolveBuildEnvironmentOutput>(cmd, codeqlArgs);
763+
return await runCliJson<ResolveBuildEnvironmentOutput>(
764+
env,
765+
cmd,
766+
codeqlArgs,
767+
);
762768
},
763769
async databaseRunQueries(
764770
databasePath: string,
@@ -777,7 +783,7 @@ export async function getCodeQLForCmd(
777783
ignoringOptions: ["--expect-discarded-cache"],
778784
}),
779785
];
780-
await runCli(cmd, codeqlArgs);
786+
await runCli(env, cmd, codeqlArgs);
781787
},
782788
async databaseInterpretResults(
783789
databasePath: string,
@@ -830,7 +836,7 @@ export async function getCodeQLForCmd(
830836
}
831837
// Capture the stdout, which contains the analysis summary. Don't stream it to the Actions
832838
// logs to avoid printing it twice.
833-
return await runCli(cmd, codeqlArgs, {
839+
return await runCli(env, cmd, codeqlArgs, {
834840
noStreamStdout: true,
835841
});
836842
},
@@ -847,7 +853,7 @@ export async function getCodeQLForCmd(
847853
`--cache-cleanup=${cleanupLevel}`,
848854
...getExtraOptionsFromEnv(["database", "cleanup"]),
849855
];
850-
await runCli(cmd, codeqlArgs);
856+
await runCli(env, cmd, codeqlArgs);
851857
}
852858
},
853859
async databaseBundle(
@@ -995,7 +1001,7 @@ export async function getCodeQLForCmd(
9951001
...getExtraOptionsFromEnv(["resolve", "queries"]),
9961002
...queries,
9971003
];
998-
return await runCliJson<string[]>(cmd, codeqlArgs, {
1004+
return await runCliJson<string[]>(env, cmd, codeqlArgs, {
9991005
noStreamStdout: true,
10001006
});
10011007
},
@@ -1009,7 +1015,7 @@ export async function getCodeQLForCmd(
10091015
"--format=json",
10101016
...getExtraOptionsFromEnv(["resolve", "database"]),
10111017
];
1012-
return await runCliJson<ResolveDatabaseOutput>(cmd, codeqlArgs, {
1018+
return await runCliJson<ResolveDatabaseOutput>(env, cmd, codeqlArgs, {
10131019
noStreamStdout: true,
10141020
});
10151021
},
@@ -1036,7 +1042,7 @@ export async function getCodeQLForCmd(
10361042
args.push("--sarif-merge-runs-from-equal-category");
10371043
}
10381044

1039-
await runCli(cmd, args);
1045+
await runCli(env, cmd, args);
10401046
},
10411047
};
10421048
// To ensure that status reports include the CodeQL CLI version wherever
@@ -1153,12 +1159,13 @@ export function getExtraOptions(
11531159
}
11541160

11551161
async function runCli(
1162+
env: Env,
11561163
cmd: string,
11571164
args: string[] = [],
11581165
opts: { stdin?: string; noStreamStdout?: boolean } = {},
11591166
): Promise<string> {
11601167
try {
1161-
return await runTool(cmd, args, opts);
1168+
return await runTool(env, cmd, args, opts);
11621169
} catch (e) {
11631170
if (e instanceof CommandInvocationError) {
11641171
throw wrapCliConfigurationError(new CliError(e));
@@ -1169,6 +1176,8 @@ async function runCli(
11691176

11701177
/**
11711178
* Wraps the command executor {@link runCli} and tries to parse the output as JSON.
1179+
*
1180+
* @param env The environment variables.
11721181
* @param cmd The command to run.
11731182
* @param args The arguments to pass to the command.
11741183
* @param opts The options for running the command.
@@ -1177,11 +1186,12 @@ async function runCli(
11771186
* @returns The parsed JSON output from the command.
11781187
*/
11791188
async function runCliJson<T>(
1189+
env: Env,
11801190
cmd: string,
11811191
args: string[] = [],
11821192
opts: { stdin?: string; noStreamStdout?: boolean } = {},
11831193
): Promise<T> {
1184-
const output = await runCli(cmd, args, opts);
1194+
const output = await runCli(env, cmd, args, opts);
11851195
try {
11861196
return JSON.parse(output) as T;
11871197
} catch (e) {

‎src/environment.ts‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,20 @@ export class ReadOnlyEnv {
306306
return { ...this.vars };
307307
}
308308

309+
/** Gets a copy of the underlying environment, where the values are not `undefined`. */
310+
public getDefined(): Record<string, string> {
311+
const result: Record<string, string> = {};
312+
313+
for (const [key, val] of Object.entries(this.vars)) {
314+
if (val === undefined) {
315+
continue;
316+
}
317+
result[key] = val;
318+
}
319+
320+
return result;
321+
}
322+
309323
/** Tries to get the value for `name` and throws if there isn't one. */
310324
public getRequired(name: string): string {
311325
return getRequiredEnvVar(this.vars, name);

0 commit comments

Comments
 (0)