Skip to content

Commit 46de74c

Browse files
committed
view-autofixes/overrideQueryHelp: recompile autofix if query help has changed.
1 parent 99feda8 commit 46de74c

File tree

2 files changed

+44
-13
lines changed

2 files changed

+44
-13
lines changed

extensions/ql-vscode/src/codeql-cli/cli.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1158,23 +1158,22 @@ export class CodeQLCliServer implements Disposable {
11581158
/**
11591159
* Uses a .qhelp file to generate Query Help documentation in a specified format.
11601160
* @param pathToQhelp The path to the .qhelp file
1161-
* @param format The format in which the query help should be generated {@link https://codeql.github.com/docs/codeql-cli/manual/generate-query-help/#cmdoption-codeql-generate-query-help-format}
1162-
* @param outputDirectory The output directory for the generated file
1161+
* @param outputFileOrDirectory The output directory for the generated file
11631162
*/
11641163
async generateQueryHelp(
11651164
pathToQhelp: string,
1166-
outputDirectory?: string,
1165+
outputFileOrDirectory?: string,
11671166
): Promise<string> {
11681167
const subcommandArgs = ["--format=markdown"];
1169-
if (outputDirectory) {
1170-
subcommandArgs.push("--output", outputDirectory);
1168+
if (outputFileOrDirectory) {
1169+
subcommandArgs.push("--output", outputFileOrDirectory);
11711170
}
11721171
subcommandArgs.push(pathToQhelp);
11731172

11741173
return await this.runCodeQlCliCommand(
11751174
["generate", "query-help"],
11761175
subcommandArgs,
1177-
`Generating qhelp in markdown format at ${outputDirectory}`,
1176+
`Generating qhelp in markdown format at ${outputFileOrDirectory}`,
11781177
);
11791178
}
11801179

extensions/ql-vscode/src/variant-analysis/view-autofixes.ts

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -245,9 +245,9 @@ async function overrideQueryHelp(
245245
// Use `replaceAll` since some query IDs have multiple slashes.
246246
const queryIdWithDash = queryId.replaceAll("/", "-");
247247

248-
// Get the path to the output directory for overriding the query help.
249-
// Note: the path to this directory may change in the future.
250-
const queryHelpOverrideDirectory = join(
248+
// Get the path to the output file for overriding the query help.
249+
// Note: the path to this file may change in the future.
250+
const queryHelpOverrideFile = join(
251251
localAutofixPath,
252252
"pkg",
253253
"autofix",
@@ -256,10 +256,26 @@ async function overrideQueryHelp(
256256
`${queryIdWithDash}.md`,
257257
);
258258

259-
await cliServer.generateQueryHelp(
260-
queryHelpFilePath,
261-
queryHelpOverrideDirectory,
262-
);
259+
// If the file already exists, slurp it so that we can check if it has changed.
260+
let existingContents: string | null = null;
261+
if (await pathExists(queryHelpOverrideFile)) {
262+
existingContents = await readFile(queryHelpOverrideFile, "utf8");
263+
}
264+
265+
// Generate the query help and output it to the override directory.
266+
await cliServer.generateQueryHelp(queryHelpFilePath, queryHelpOverrideFile);
267+
268+
// If the contents of `queryHelpOverrideFile` have changed, recompile autofix
269+
// to include the new query help.
270+
if (existingContents !== null) {
271+
const newContents = await readFile(queryHelpOverrideFile, "utf8");
272+
if (existingContents !== newContents) {
273+
void extLogger.log(
274+
`Query help for query ID ${queryId} has changed. Recompiling autofix...`,
275+
);
276+
await recompileAutofix(localAutofixPath);
277+
}
278+
}
263279
}
264280

265281
/**
@@ -907,6 +923,22 @@ async function opRead(secretReference: string): Promise<string> {
907923
}
908924
}
909925

926+
/** Recompile the Autofix binary. */
927+
async function recompileAutofix(localAutofixPath: string): Promise<void> {
928+
const { code, stderr } = await execCommand(
929+
"make",
930+
["build"],
931+
{ cwd: localAutofixPath },
932+
false,
933+
);
934+
935+
if (code !== 0) {
936+
throw new Error(
937+
`Failed to recompile autofix after query help change. Exit code: ${code}. Stderr: ${stderr}`,
938+
);
939+
}
940+
}
941+
910942
/**
911943
* Creates a new file path by appending the given suffix.
912944
* @param filePath The original file path.

0 commit comments

Comments
 (0)