From aca005a7772c0ab164133d9f302b18078b544036 Mon Sep 17 00:00:00 2001 From: Stephan Brandauer Date: Thu, 28 Nov 2024 14:00:46 +0000 Subject: [PATCH 1/3] notify language client of updated packages from cli.ts --- extensions/ql-vscode/src/codeql-cli/cli.ts | 42 ++++++++++++++++++++-- extensions/ql-vscode/src/extension.ts | 7 ++-- 2 files changed, 43 insertions(+), 6 deletions(-) diff --git a/extensions/ql-vscode/src/codeql-cli/cli.ts b/extensions/ql-vscode/src/codeql-cli/cli.ts index 39cad098703..42caf73994d 100644 --- a/extensions/ql-vscode/src/codeql-cli/cli.ts +++ b/extensions/ql-vscode/src/codeql-cli/cli.ts @@ -8,7 +8,10 @@ import type { Log } from "sarif"; import { SemVer } from "semver"; import type { Readable } from "stream"; import tk from "tree-kill"; -import type { CancellationToken, Disposable, Uri } from "vscode"; +import type { CancellationToken, Disposable } from "vscode"; +import { Uri } from "vscode"; + +import { existsSync } from "fs"; import type { BqrsInfo, @@ -37,6 +40,11 @@ import { LOGGING_FLAGS } from "./cli-command"; import type { CliFeatures, VersionAndFeatures } from "./cli-version"; import { ExitCodeError, getCliError } from "./cli-errors"; import { UserCancellationException } from "../common/vscode/progress"; +import type { LanguageClient } from "vscode-languageclient/node"; +import { + DidChangeWatchedFilesNotification, + FileChangeType, +} from "vscode-languageclient/node"; /** * The version of the SARIF format that we are using. @@ -277,6 +285,7 @@ export class CodeQLCliServer implements Disposable { constructor( private readonly app: App, + private readonly languageClient: LanguageClient, private distributionProvider: DistributionProvider, private cliConfig: CliConfig, public readonly logger: Logger, @@ -1584,11 +1593,13 @@ export class CodeQLCliServer implements Disposable { async packAdd(dir: string, queryLanguage: QueryLanguage) { const args = ["--dir", dir]; args.push(`codeql/${queryLanguage}-all`); - return this.runCodeQlCliCommand( + const ret = await this.runCodeQlCliCommand( ["pack", "add"], args, `Adding and installing ${queryLanguage} pack dependency.`, ); + await this.notifyPackChanged(dir); + return ret; } /** @@ -1628,11 +1639,13 @@ export class CodeQLCliServer implements Disposable { ...this.getAdditionalPacksArg(workspaceFolders), ); } - return this.runJsonCodeQlCliCommandWithAuthentication( + const ret = await this.runJsonCodeQlCliCommandWithAuthentication( ["pack", "install"], args, "Installing pack dependencies", ); + await this.notifyPackChanged(dir); + return ret; } /** @@ -1750,6 +1763,29 @@ export class CodeQLCliServer implements Disposable { this._versionChangedListeners.push(listener); } + private async notifyPackChanged(packDir: string) { + const packFilePath = join(packDir, "codeql-pack.yml"); + if (!existsSync(packFilePath)) { + throw new Error(`Pack file ${packFilePath} does not exist`); + } + await this.languageClient.sendNotification( + DidChangeWatchedFilesNotification.type, + { + changes: [ + { + type: FileChangeType.Changed, + uri: Uri.file(packFilePath).toString(), + }, + ], + }, + ); + + // restarting the language client has the effect of removing compilation + // errors in open ql/qll files that are caused by the pack not having been + // installed previously: + await this.languageClient.restart(); + } + private async refreshVersion(): Promise { const distribution = await this.distributionProvider.getDistribution(); switch (distribution.kind) { diff --git a/extensions/ql-vscode/src/extension.ts b/extensions/ql-vscode/src/extension.ts index fde7cbec42a..002c773d738 100644 --- a/extensions/ql-vscode/src/extension.ts +++ b/extensions/ql-vscode/src/extension.ts @@ -748,9 +748,13 @@ async function activateWithInstalledDistribution( ); ctx.subscriptions.push(qlConfigurationListener); + void extLogger.log("Initializing CodeQL language server."); + const languageClient = createLanguageClient(qlConfigurationListener); + void extLogger.log("Initializing CodeQL cli server..."); const cliServer = new CodeQLCliServer( app, + languageClient, distributionManager, new CliConfigListener(), extLogger, @@ -961,9 +965,6 @@ async function activateWithInstalledDistribution( ctx.subscriptions.push(tmpDirDisposal); - void extLogger.log("Initializing CodeQL language server."); - const languageClient = createLanguageClient(qlConfigurationListener); - const localQueries = new LocalQueries( app, qs, From 6fd3d205a52402ae84baebadc284c4cdf3f8a9f9 Mon Sep 17 00:00:00 2001 From: Stephan Brandauer Date: Fri, 29 Nov 2024 08:31:30 +0000 Subject: [PATCH 2/3] fix typo --- extensions/ql-vscode/src/codeql-cli/cli.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/extensions/ql-vscode/src/codeql-cli/cli.ts b/extensions/ql-vscode/src/codeql-cli/cli.ts index 42caf73994d..b6155a07775 100644 --- a/extensions/ql-vscode/src/codeql-cli/cli.ts +++ b/extensions/ql-vscode/src/codeql-cli/cli.ts @@ -1634,7 +1634,7 @@ export class CodeQLCliServer implements Disposable { args.push( // Allow prerelease packs from the ql submodule. "--allow-prerelease", - // Allow the use of --additional-packs argument without issueing a warning + // Allow the use of --additional-packs argument without issuing a warning "--no-strict-mode", ...this.getAdditionalPacksArg(workspaceFolders), ); From 17542d1041470d8ee02066428b6f3bde5defb6ce Mon Sep 17 00:00:00 2001 From: Stephan Brandauer Date: Mon, 2 Dec 2024 08:16:37 +0000 Subject: [PATCH 3/3] remove notification, when we are restarting the language client anyways --- extensions/ql-vscode/src/codeql-cli/cli.ts | 41 ++++++---------------- 1 file changed, 11 insertions(+), 30 deletions(-) diff --git a/extensions/ql-vscode/src/codeql-cli/cli.ts b/extensions/ql-vscode/src/codeql-cli/cli.ts index b6155a07775..7f090eff2ec 100644 --- a/extensions/ql-vscode/src/codeql-cli/cli.ts +++ b/extensions/ql-vscode/src/codeql-cli/cli.ts @@ -8,10 +8,7 @@ import type { Log } from "sarif"; import { SemVer } from "semver"; import type { Readable } from "stream"; import tk from "tree-kill"; -import type { CancellationToken, Disposable } from "vscode"; -import { Uri } from "vscode"; - -import { existsSync } from "fs"; +import type { CancellationToken, Disposable, Uri } from "vscode"; import type { BqrsInfo, @@ -41,10 +38,6 @@ import type { CliFeatures, VersionAndFeatures } from "./cli-version"; import { ExitCodeError, getCliError } from "./cli-errors"; import { UserCancellationException } from "../common/vscode/progress"; import type { LanguageClient } from "vscode-languageclient/node"; -import { - DidChangeWatchedFilesNotification, - FileChangeType, -} from "vscode-languageclient/node"; /** * The version of the SARIF format that we are using. @@ -1598,7 +1591,7 @@ export class CodeQLCliServer implements Disposable { args, `Adding and installing ${queryLanguage} pack dependency.`, ); - await this.notifyPackChanged(dir); + await this.notifyPackInstalled(); return ret; } @@ -1644,7 +1637,7 @@ export class CodeQLCliServer implements Disposable { args, "Installing pack dependencies", ); - await this.notifyPackChanged(dir); + await this.notifyPackInstalled(); return ret; } @@ -1763,26 +1756,14 @@ export class CodeQLCliServer implements Disposable { this._versionChangedListeners.push(listener); } - private async notifyPackChanged(packDir: string) { - const packFilePath = join(packDir, "codeql-pack.yml"); - if (!existsSync(packFilePath)) { - throw new Error(`Pack file ${packFilePath} does not exist`); - } - await this.languageClient.sendNotification( - DidChangeWatchedFilesNotification.type, - { - changes: [ - { - type: FileChangeType.Changed, - uri: Uri.file(packFilePath).toString(), - }, - ], - }, - ); - - // restarting the language client has the effect of removing compilation - // errors in open ql/qll files that are caused by the pack not having been - // installed previously: + /** + * This method should be called after a pack has been installed. + * + * This restarts the language client. Restarting the language client has the + * effect of removing compilation errors in open ql/qll files that are caused + * by the pack not having been installed previously. + */ + private async notifyPackInstalled() { await this.languageClient.restart(); }