Skip to content

Commit

Permalink
fix spurious compilation errors by notifying language server of newly…
Browse files Browse the repository at this point in the history
… created packs
  • Loading branch information
kaeluka committed Nov 27, 2024
1 parent 94434f4 commit c0ed920
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import { telemetryListener } from "../../common/vscode/telemetry";
import type { LanguageContextStore } from "../../language-context-store";
import type { DatabaseOrigin } from "./database-origin";
import { ensureZippedSourceLocation } from "./database-contents";
import type { LanguageClient } from "vscode-languageclient/node";

/**
* The name of the key in the workspaceState dictionary in which we
Expand Down Expand Up @@ -118,6 +119,7 @@ export class DatabaseManager extends DisposableObject {
private readonly app: App,
private readonly qs: QueryRunner,
private readonly cli: CodeQLCliServer,
private readonly langClient: LanguageClient,
private readonly languageContext: LanguageContextStore,
public logger: Logger,
) {
Expand Down Expand Up @@ -407,6 +409,7 @@ export class DatabaseManager extends DisposableObject {
const qlPackGenerator = new QlPackGenerator(
databaseItem.language,
this.cli,
this.langClient,
qlpackStoragePath,
qlpackStoragePath,
);
Expand Down
8 changes: 5 additions & 3 deletions extensions/ql-vscode/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -792,12 +792,16 @@ async function activateWithInstalledDistribution(
const languageSelectionPanel = new LanguageSelectionPanel(languageContext);
ctx.subscriptions.push(languageSelectionPanel);

void extLogger.log("Initializing CodeQL language server.");
const languageClient = createLanguageClient(qlConfigurationListener);

void extLogger.log("Initializing database manager.");
const dbm = new DatabaseManager(
ctx,
app,
qs,
cliServer,
languageClient,
languageContext,
extLogger,
);
Expand Down Expand Up @@ -961,16 +965,14 @@ async function activateWithInstalledDistribution(

ctx.subscriptions.push(tmpDirDisposal);

void extLogger.log("Initializing CodeQL language server.");
const languageClient = createLanguageClient(qlConfigurationListener);

const localQueries = new LocalQueries(
app,
qs,
qhm,
dbm,
databaseFetcher,
cliServer,
languageClient,
databaseUI,
localQueryResultsView,
queryStorageDir,
Expand Down
3 changes: 3 additions & 0 deletions extensions/ql-vscode/src/local-queries/local-queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ import { tryGetQueryLanguage } from "../common/query-language";
import type { LanguageContextStore } from "../language-context-store";
import type { ExtensionApp } from "../common/vscode/extension-app";
import type { DatabaseFetcher } from "../databases/database-fetcher";
import type { LanguageClient } from "vscode-languageclient/node";

export enum QuickEvalType {
None,
Expand All @@ -72,6 +73,7 @@ export class LocalQueries extends DisposableObject {
private readonly databaseManager: DatabaseManager,
private readonly databaseFetcher: DatabaseFetcher,
private readonly cliServer: CodeQLCliServer,
private readonly langClient: LanguageClient,
private readonly databaseUI: DatabaseUI,
private readonly localQueryResultsView: ResultsView,
private readonly queryStorageDir: string,
Expand Down Expand Up @@ -324,6 +326,7 @@ export class LocalQueries extends DisposableObject {
const language = this.languageContextStore.selectedLanguage;
const skeletonQueryWizard = new SkeletonQueryWizard(
this.cliServer,
this.langClient,
progress,
this.app,
this.databaseManager,
Expand Down
28 changes: 28 additions & 0 deletions extensions/ql-vscode/src/local-queries/qlpack-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ import type { CodeQLCliServer } from "../codeql-cli/cli";
import type { QueryLanguage } from "../common/query-language";
import { getOnDiskWorkspaceFolders } from "../common/vscode/workspace-folders";
import { basename } from "../common/path";
import {
DidChangeWatchedFilesNotification,
FileChangeType,
} from "vscode-languageclient";
import type { LanguageClient } from "vscode-languageclient/node";

export class QlPackGenerator {
private qlpackName: string | undefined;
Expand All @@ -17,6 +22,7 @@ export class QlPackGenerator {
constructor(
private readonly queryLanguage: QueryLanguage,
private readonly cliServer: CodeQLCliServer,
private readonly langClient: LanguageClient,
private readonly storagePath: string,
private readonly queryStoragePath: string,
private readonly includeFolderNameInQlpackName: boolean = false,
Expand Down Expand Up @@ -114,5 +120,27 @@ select f, "Hello, world!"

private async createCodeqlPackLockYaml() {
await this.cliServer.packAdd(this.folderUri.fsPath, this.queryLanguage);
// when the language pack has not been available locally before, the
// packAdd command will download it. This will trigger a pack change that
// the language server needs to be notified of:
await this.notifyPackChanged(this.folderUri.fsPath, this.langClient);
}

private async notifyPackChanged(
packFileDir: string,
ideServer: LanguageClient,
) {
const packFilePath = join(packFileDir, this.qlpackFileName);
await this.cliServer.logger.log(
`Notifying pack change for ${packFilePath}`,
);
await ideServer.sendNotification(DidChangeWatchedFilesNotification.type, {
changes: [
{
type: FileChangeType.Changed,
uri: Uri.file(packFilePath).toString(),
},
],
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import type { QueryTreeViewItem } from "../queries-panel/query-tree-view-item";
import { containsPath, pathsEqual } from "../common/files";
import { getQlPackFilePath } from "../common/ql";
import { getQlPackLanguage } from "../common/qlpack-language";
import type { LanguageClient } from "vscode-languageclient/node";

type QueryLanguagesToDatabaseMap = Record<string, string>;

Expand All @@ -56,6 +57,7 @@ export class SkeletonQueryWizard {

constructor(
private readonly cliServer: CodeQLCliServer,
private readonly langClient: LanguageClient,
private readonly progress: ProgressCallback,
private readonly app: App,
private readonly databaseManager: DatabaseManager,
Expand Down Expand Up @@ -443,6 +445,7 @@ export class SkeletonQueryWizard {
return new QlPackGenerator(
this.language,
this.cliServer,
this.langClient,
this.qlPackStoragePath,
this.queryStoragePath,
includeFolderNameInQlpackName,
Expand Down

0 comments on commit c0ed920

Please sign in to comment.