Skip to content

Commit c4fb451

Browse files
authored
Merge pull request #2245 from github/henrymercer/ignore-already-specified-flags
Ensure `--overwrite` flag is only passed once
2 parents 9b87e0a + 556b3bc commit c4fb451

File tree

7 files changed

+84
-10
lines changed

7 files changed

+84
-10
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ Note that the only difference between `v2` and `v3` of the CodeQL Action is the
77
## [UNRELEASED]
88

99
- We are rolling out a feature in April/May 2024 that improves the reliability and performance of analyzing code when analyzing a compiled language with the `autobuild` [build mode](https://docs.github.com/en/code-security/code-scanning/creating-an-advanced-setup-for-code-scanning/codeql-code-scanning-for-compiled-languages#codeql-build-modes). [#2235](https://github.com/github/codeql-action/pull/2235)
10+
- Fix a bug where the `init` Action would fail if `--overwrite` was specified in `CODEQL_ACTION_EXTRA_OPTIONS`. [#2245](https://github.com/github/codeql-action/pull/2245)
1011

1112
## 3.25.0 - 15 Apr 2024
1213

lib/codeql.js

+11-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/codeql.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/codeql.test.js

+18
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/codeql.test.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/codeql.test.ts

+36
Original file line numberDiff line numberDiff line change
@@ -989,6 +989,42 @@ test("runTool outputs last line of stderr if fatal error could not be found", as
989989
);
990990
});
991991

992+
test("Avoids duplicating --overwrite flag if specified in CODEQL_ACTION_EXTRA_OPTIONS", async (t) => {
993+
const runnerConstructorStub = stubToolRunnerConstructor();
994+
const codeqlObject = await codeql.getCodeQLForTesting();
995+
sinon.stub(codeqlObject, "getVersion").resolves(makeVersionInfo("2.12.6"));
996+
// safeWhich throws because of the test CodeQL object.
997+
sinon.stub(safeWhich, "safeWhich").resolves("");
998+
999+
process.env["CODEQL_ACTION_EXTRA_OPTIONS"] =
1000+
'{ "database": { "init": ["--overwrite"] } }';
1001+
1002+
await codeqlObject.databaseInitCluster(
1003+
stubConfig,
1004+
"sourceRoot",
1005+
undefined,
1006+
undefined,
1007+
createFeatures([]),
1008+
getRunnerLogger(false),
1009+
);
1010+
1011+
t.true(runnerConstructorStub.calledOnce);
1012+
const args = runnerConstructorStub.firstCall.args[1] as string[];
1013+
t.is(
1014+
args.filter((option: string) => option === "--overwrite").length,
1015+
1,
1016+
"--overwrite should only be passed once",
1017+
);
1018+
1019+
// Clean up
1020+
const configArg = args.find((arg: string) =>
1021+
arg.startsWith("--codescanning-config="),
1022+
);
1023+
t.truthy(configArg, "Should have injected a codescanning config");
1024+
const configFile = configArg!.split("=")[1];
1025+
await del(configFile, { force: true });
1026+
});
1027+
9921028
export function stubToolRunnerConstructor(
9931029
exitCode: number = 0,
9941030
stderr?: string,

src/codeql.ts

+16-4
Original file line numberDiff line numberDiff line change
@@ -627,7 +627,9 @@ export async function getCodeQLForCmd(
627627
`--source-root=${sourceRoot}`,
628628
...(await getLanguageAliasingArguments(this)),
629629
...extraArgs,
630-
...getExtraOptionsFromEnv(["database", "init"]),
630+
...getExtraOptionsFromEnv(["database", "init"], {
631+
ignoringOptions: ["--overwrite"],
632+
}),
631633
],
632634
{ stdin: externalRepositoryToken },
633635
);
@@ -835,7 +837,9 @@ export async function getCodeQLForCmd(
835837
"--expect-discarded-cache",
836838
"--min-disk-free=1024", // Try to leave at least 1GB free
837839
"-v",
838-
...getExtraOptionsFromEnv(["database", "run-queries"]),
840+
...getExtraOptionsFromEnv(["database", "run-queries"], {
841+
ignoringOptions: ["--expect-discarded-cache"],
842+
}),
839843
];
840844
if (
841845
await util.codeQlVersionAbove(
@@ -1174,10 +1178,18 @@ export async function getCodeQLForCmd(
11741178

11751179
/**
11761180
* Gets the options for `path` of `options` as an array of extra option strings.
1181+
*
1182+
* @param ignoringOptions Options that should be ignored, for example because they have already
1183+
* been passed and it is an error to pass them more than once.
11771184
*/
1178-
function getExtraOptionsFromEnv(paths: string[]) {
1185+
function getExtraOptionsFromEnv(
1186+
paths: string[],
1187+
{ ignoringOptions }: { ignoringOptions?: string[] } = {},
1188+
) {
11791189
const options: ExtraOptions = util.getExtraOptionsEnvParam();
1180-
return getExtraOptions(options, paths, []);
1190+
return getExtraOptions(options, paths, []).filter(
1191+
(option) => !ignoringOptions?.includes(option),
1192+
);
11811193
}
11821194

11831195
/**

0 commit comments

Comments
 (0)