Skip to content

Commit aca005a

Browse files
author
Stephan Brandauer
committed
notify language client of updated packages from cli.ts
1 parent 94434f4 commit aca005a

File tree

2 files changed

+43
-6
lines changed

2 files changed

+43
-6
lines changed

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

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@ import type { Log } from "sarif";
88
import { SemVer } from "semver";
99
import type { Readable } from "stream";
1010
import tk from "tree-kill";
11-
import type { CancellationToken, Disposable, Uri } from "vscode";
11+
import type { CancellationToken, Disposable } from "vscode";
12+
import { Uri } from "vscode";
13+
14+
import { existsSync } from "fs";
1215

1316
import type {
1417
BqrsInfo,
@@ -37,6 +40,11 @@ import { LOGGING_FLAGS } from "./cli-command";
3740
import type { CliFeatures, VersionAndFeatures } from "./cli-version";
3841
import { ExitCodeError, getCliError } from "./cli-errors";
3942
import { UserCancellationException } from "../common/vscode/progress";
43+
import type { LanguageClient } from "vscode-languageclient/node";
44+
import {
45+
DidChangeWatchedFilesNotification,
46+
FileChangeType,
47+
} from "vscode-languageclient/node";
4048

4149
/**
4250
* The version of the SARIF format that we are using.
@@ -277,6 +285,7 @@ export class CodeQLCliServer implements Disposable {
277285

278286
constructor(
279287
private readonly app: App,
288+
private readonly languageClient: LanguageClient,
280289
private distributionProvider: DistributionProvider,
281290
private cliConfig: CliConfig,
282291
public readonly logger: Logger,
@@ -1584,11 +1593,13 @@ export class CodeQLCliServer implements Disposable {
15841593
async packAdd(dir: string, queryLanguage: QueryLanguage) {
15851594
const args = ["--dir", dir];
15861595
args.push(`codeql/${queryLanguage}-all`);
1587-
return this.runCodeQlCliCommand(
1596+
const ret = await this.runCodeQlCliCommand(
15881597
["pack", "add"],
15891598
args,
15901599
`Adding and installing ${queryLanguage} pack dependency.`,
15911600
);
1601+
await this.notifyPackChanged(dir);
1602+
return ret;
15921603
}
15931604

15941605
/**
@@ -1628,11 +1639,13 @@ export class CodeQLCliServer implements Disposable {
16281639
...this.getAdditionalPacksArg(workspaceFolders),
16291640
);
16301641
}
1631-
return this.runJsonCodeQlCliCommandWithAuthentication(
1642+
const ret = await this.runJsonCodeQlCliCommandWithAuthentication(
16321643
["pack", "install"],
16331644
args,
16341645
"Installing pack dependencies",
16351646
);
1647+
await this.notifyPackChanged(dir);
1648+
return ret;
16361649
}
16371650

16381651
/**
@@ -1750,6 +1763,29 @@ export class CodeQLCliServer implements Disposable {
17501763
this._versionChangedListeners.push(listener);
17511764
}
17521765

1766+
private async notifyPackChanged(packDir: string) {
1767+
const packFilePath = join(packDir, "codeql-pack.yml");
1768+
if (!existsSync(packFilePath)) {
1769+
throw new Error(`Pack file ${packFilePath} does not exist`);
1770+
}
1771+
await this.languageClient.sendNotification(
1772+
DidChangeWatchedFilesNotification.type,
1773+
{
1774+
changes: [
1775+
{
1776+
type: FileChangeType.Changed,
1777+
uri: Uri.file(packFilePath).toString(),
1778+
},
1779+
],
1780+
},
1781+
);
1782+
1783+
// restarting the language client has the effect of removing compilation
1784+
// errors in open ql/qll files that are caused by the pack not having been
1785+
// installed previously:
1786+
await this.languageClient.restart();
1787+
}
1788+
17531789
private async refreshVersion(): Promise<VersionAndFeatures> {
17541790
const distribution = await this.distributionProvider.getDistribution();
17551791
switch (distribution.kind) {

extensions/ql-vscode/src/extension.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -748,9 +748,13 @@ async function activateWithInstalledDistribution(
748748
);
749749
ctx.subscriptions.push(qlConfigurationListener);
750750

751+
void extLogger.log("Initializing CodeQL language server.");
752+
const languageClient = createLanguageClient(qlConfigurationListener);
753+
751754
void extLogger.log("Initializing CodeQL cli server...");
752755
const cliServer = new CodeQLCliServer(
753756
app,
757+
languageClient,
754758
distributionManager,
755759
new CliConfigListener(),
756760
extLogger,
@@ -961,9 +965,6 @@ async function activateWithInstalledDistribution(
961965

962966
ctx.subscriptions.push(tmpDirDisposal);
963967

964-
void extLogger.log("Initializing CodeQL language server.");
965-
const languageClient = createLanguageClient(qlConfigurationListener);
966-
967968
const localQueries = new LocalQueries(
968969
app,
969970
qs,

0 commit comments

Comments
 (0)