Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,8 @@ export async function runAutoModelQueries({
);

// Generate a pack containing the candidate filters
const filterPackDir = await generateCandidateFilterPack(
databaseItem.language,
candidateMethods,
);
const { packDir: filterPackDir, cleanup: cleanupFilterPack } =
await generateCandidateFilterPack(databaseItem.language, candidateMethods);

const additionalPacks = [...getOnDiskWorkspaceFolders(), filterPackDir];
const extensionPacks = Object.keys(
Expand All @@ -85,6 +83,8 @@ export async function runAutoModelQueries({
token: cancellationTokenSource.token,
});

await cleanupFilterPack();

if (!completedQuery) {
return undefined;
}
Expand Down Expand Up @@ -155,6 +155,11 @@ async function resolveAutomodelQuery(
return queries[0];
}

type CandidateFilterPackResult = {
packDir: string;
cleanup: () => Promise<void>;
};

/**
* generateCandidateFilterPack will create a temporary extension pack.
* This pack will contain a filter that will restrict the automodel queries
Expand All @@ -167,9 +172,9 @@ async function resolveAutomodelQuery(
export async function generateCandidateFilterPack(
language: string,
candidateMethods: MethodSignature[],
): Promise<string> {
): Promise<CandidateFilterPackResult> {
// Pack resides in a temporary directory, to not pollute the workspace.
const packDir = (await dir({ unsafeCleanup: true })).path;
const { path: packDir, cleanup } = await dir({ unsafeCleanup: true });

const syntheticConfigPack = {
name: "codeql/automodel-filter",
Expand Down Expand Up @@ -208,7 +213,10 @@ export async function generateCandidateFilterPack(
const filterFile = join(packDir, "filter.yml");
await writeFile(filterFile, dumpYaml(filter), "utf8");

return packDir;
return {
packDir,
cleanup,
};
}

async function interpretAutomodelResults(
Expand Down
18 changes: 16 additions & 2 deletions extensions/ql-vscode/src/model-editor/model-editor-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,12 @@ export class ModelEditorModule extends DisposableObject {
});

// Create new temporary directory for query files and pack dependencies
const queryDir = (await dir({ unsafeCleanup: true })).path;
const { path: queryDir, cleanup: cleanupQueryDir } = await dir({
unsafeCleanup: true,
});
const success = await setUpPack(this.cliServer, queryDir, language);
if (!success) {
await cleanupQueryDir();
return;
}

Expand All @@ -161,9 +164,20 @@ export class ModelEditorModule extends DisposableObject {
this.methodsUsagePanel.setState.bind(this.methodsUsagePanel),
this.showMethod.bind(this),
this.handleViewBecameActive.bind(this),
this.handleViewWasDisposed.bind(this),
(view) => {
this.handleViewWasDisposed(view);
void cleanupQueryDir();
},
this.isMostRecentlyActiveView.bind(this),
);

this.push(view);
this.push({
dispose(): void {
void cleanupQueryDir();
},
});

await view.openView();
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { QueryRunner } from "../../../../src/query-server";
import * as queryResolver from "../../../../src/local-queries/query-resolver";
import { MethodSignature } from "../../../../src/model-editor/method";
import { join } from "path";
import { exists, readFile } from "fs-extra";
import { pathExists, readFile } from "fs-extra";
import { load as loadYaml } from "js-yaml";
import { CancellationTokenSource } from "vscode-jsonrpc";
import { QueryOutputDir } from "../../../../src/run-queries-shared";
Expand Down Expand Up @@ -176,12 +176,15 @@ describe("generateCandidateFilterPack", () => {
methodParameters: "()",
},
];
const packDir = await generateCandidateFilterPack("java", candidateMethods);
const { packDir, cleanup } = await generateCandidateFilterPack(
"java",
candidateMethods,
);
expect(packDir).not.toBeUndefined();
const qlpackFile = join(packDir, "codeql-pack.yml");
expect(await exists(qlpackFile)).toBe(true);
expect(await pathExists(qlpackFile)).toBe(true);
const filterFile = join(packDir, "filter.yml");
expect(await exists(filterFile)).toBe(true);
expect(await pathExists(filterFile)).toBe(true);
// Read the contents of filterFile and parse as yaml
const yaml = await loadYaml(await readFile(filterFile, "utf8"));
const extensions = yaml.extensions;
Expand All @@ -193,5 +196,8 @@ describe("generateCandidateFilterPack", () => {
expect(extension.data).toBeInstanceOf(Array);
expect(extension.data).toHaveLength(1);
expect(extension.data[0]).toEqual(["org.my", "A", "x", "()"]);

await cleanup();
expect(await pathExists(packDir)).toBe(false);
});
});