Skip to content

Commit a3fad49

Browse files
committed
Fix creation of nested query packs
Before, if you had selected a folder or file within for example `codeql-custom-queries-java` and selected `java` as the language, it would create a nested folder within `codeql-custom-queries-java` with the name `codeql-custom-queries-java`. This is unexpected for the user, who would expect a new query to be created within `codeql-custom-queries-java`. This fixes that by checking for this specific condition. It does not fix it for all scenarios, such as where the selected file/folder is nested multiple levels deep within the `codeql-custom-queries-java` folder.
1 parent 68ab2fd commit a3fad49

File tree

2 files changed

+52
-2
lines changed

2 files changed

+52
-2
lines changed

extensions/ql-vscode/src/local-queries/skeleton-query-wizard.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { dirname, join } from "path";
1+
import { basename, dirname, join } from "path";
22
import { Uri, window as Window, workspace } from "vscode";
33
import { CodeQLCliServer } from "../codeql-cli/cli";
44
import { BaseLogger } from "../common/logging";
@@ -116,6 +116,18 @@ export class SkeletonQueryWizard {
116116
return this.determineRootStoragePath();
117117
}
118118

119+
const storagePath = await this.determineStoragePathFromSelection();
120+
121+
// If the user has selected a folder or file within a folder that matches the current
122+
// folder name, we should create a query rather than a query pack
123+
if (basename(storagePath) === this.folderName) {
124+
return dirname(storagePath);
125+
}
126+
127+
return storagePath;
128+
}
129+
130+
private async determineStoragePathFromSelection(): Promise<string> {
119131
// Just like VS Code's "New File" command, if the user has selected multiple files/folders in the queries panel,
120132
// we will create the new file in the same folder as the first selected item.
121133
// See https://github.com/microsoft/vscode/blob/a8b7239d0311d4915b57c837972baf4b01394491/src/vs/workbench/contrib/files/browser/fileActions.ts#L893-L900
@@ -232,7 +244,7 @@ export class SkeletonQueryWizard {
232244
await qlPackGenerator.createExampleQlFile(this.fileName);
233245
} catch (e: unknown) {
234246
void this.logger.log(
235-
`Could not create skeleton QL pack: ${getErrorMessage(e)}`,
247+
`Could not create query example file: ${getErrorMessage(e)}`,
236248
);
237249
}
238250
}

extensions/ql-vscode/test/vscode-tests/cli-integration/local-queries/skeleton-query-wizard.test.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,9 @@ describe("SkeletonQueryWizard", () => {
442442

443443
await ensureDir(join(queriesDir.name, "folder"));
444444
await ensureFile(join(queriesDir.name, "queries-java", "example.ql"));
445+
await ensureFile(
446+
join(queriesDir.name, "codeql-custom-queries-swift", "example.ql"),
447+
);
445448
});
446449

447450
describe("with selected folder", () => {
@@ -510,6 +513,41 @@ describe("SkeletonQueryWizard", () => {
510513
});
511514
});
512515

516+
describe("with selected file with same name", () => {
517+
let selectedItems: QueryTreeViewItem[];
518+
519+
beforeEach(async () => {
520+
selectedItems = [
521+
createQueryTreeFileItem(
522+
"example.ql",
523+
join(
524+
queriesDir.name,
525+
"codeql-custom-queries-swift",
526+
"example.ql",
527+
),
528+
"java",
529+
),
530+
];
531+
532+
wizard = new SkeletonQueryWizard(
533+
mockCli,
534+
jest.fn(),
535+
credentials,
536+
extLogger,
537+
mockDatabaseManager,
538+
storagePath,
539+
selectedItems,
540+
QueryLanguage.Swift,
541+
);
542+
});
543+
544+
it("returns the parent path", async () => {
545+
const chosenPath = await wizard.determineStoragePath();
546+
547+
expect(chosenPath).toEqual(queriesDir.name);
548+
});
549+
});
550+
513551
describe("with multiple selected items", () => {
514552
let selectedItems: QueryTreeViewItem[];
515553

0 commit comments

Comments
 (0)