Skip to content

Commit 03adb70

Browse files
Merge pull request #2298 from github/elena/yer-a-windows-query
Fix problem with detecting storage folder on windows
2 parents e15f01c + 7f8f065 commit 03adb70

File tree

3 files changed

+45
-22
lines changed

3 files changed

+45
-22
lines changed

extensions/ql-vscode/src/local-queries.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,13 +381,16 @@ export class LocalQueries extends DisposableObject {
381381
await withProgress(
382382
async (progress: ProgressCallback, token: CancellationToken) => {
383383
const credentials = isCanary() ? this.app.credentials : undefined;
384+
const contextStoragePath =
385+
this.app.workspaceStoragePath || this.app.globalStoragePath;
384386
const skeletonQueryWizard = new SkeletonQueryWizard(
385387
this.cliServer,
386388
progress,
387389
credentials,
388390
extLogger,
389391
this.databaseManager,
390392
token,
393+
contextStoragePath,
391394
);
392395
await skeletonQueryWizard.execute();
393396
},

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

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { join } from "path";
1+
import { join, dirname } from "path";
22
import { CancellationToken, Uri, workspace, window as Window } from "vscode";
33
import { CodeQLCliServer } from "./cli";
44
import { OutputChannelLogger } from "./common";
@@ -27,7 +27,7 @@ export const QUERY_LANGUAGE_TO_DATABASE_REPO: QueryLanguagesToDatabaseMap = {
2727
export class SkeletonQueryWizard {
2828
private language: string | undefined;
2929
private fileName = "example.ql";
30-
private storagePath: string | undefined;
30+
private qlPackStoragePath: string | undefined;
3131

3232
constructor(
3333
private readonly cliServer: CodeQLCliServer,
@@ -36,6 +36,7 @@ export class SkeletonQueryWizard {
3636
private readonly extLogger: OutputChannelLogger,
3737
private readonly databaseManager: DatabaseManager,
3838
private readonly token: CancellationToken,
39+
private readonly databaseStoragePath: string | undefined,
3940
) {}
4041

4142
private get folderName() {
@@ -49,7 +50,7 @@ export class SkeletonQueryWizard {
4950
return;
5051
}
5152

52-
this.storagePath = this.getFirstStoragePath();
53+
this.qlPackStoragePath = this.getFirstStoragePath();
5354

5455
const skeletonPackAlreadyExists = isFolderAlreadyInWorkspace(
5556
this.folderName,
@@ -72,12 +73,12 @@ export class SkeletonQueryWizard {
7273
}
7374

7475
private async openExampleFile() {
75-
if (this.folderName === undefined || this.storagePath === undefined) {
76+
if (this.folderName === undefined || this.qlPackStoragePath === undefined) {
7677
throw new Error("Path to folder is undefined");
7778
}
7879

7980
const queryFileUri = Uri.file(
80-
join(this.storagePath, this.folderName, this.fileName),
81+
join(this.qlPackStoragePath, this.folderName, this.fileName),
8182
);
8283

8384
try {
@@ -99,15 +100,16 @@ export class SkeletonQueryWizard {
99100
}
100101

101102
const firstFolder = workspaceFolders[0];
103+
const firstFolderFsPath = firstFolder.uri.fsPath;
102104

103105
// For the vscode-codeql-starter repo, the first folder will be a ql pack
104106
// so we need to get the parent folder
105-
if (firstFolder.uri.path.includes("codeql-custom-queries")) {
106-
// slice off the last part of the path and return the parent folder
107-
return firstFolder.uri.path.split("/").slice(0, -1).join("/");
107+
if (firstFolderFsPath.includes("codeql-custom-queries")) {
108+
// return the parent folder
109+
return dirname(firstFolderFsPath);
108110
} else {
109111
// if the first folder is not a ql pack, then we are in a normal workspace
110-
return firstFolder.uri.path;
112+
return firstFolderFsPath;
111113
}
112114
}
113115

@@ -137,7 +139,7 @@ export class SkeletonQueryWizard {
137139
this.folderName,
138140
this.language as QueryLanguage,
139141
this.cliServer,
140-
this.storagePath,
142+
this.qlPackStoragePath,
141143
);
142144

143145
await qlPackGenerator.generate();
@@ -165,7 +167,7 @@ export class SkeletonQueryWizard {
165167
this.folderName,
166168
this.language as QueryLanguage,
167169
this.cliServer,
168-
this.storagePath,
170+
this.qlPackStoragePath,
169171
);
170172

171173
this.fileName = await this.determineNextFileName(this.folderName);
@@ -178,11 +180,11 @@ export class SkeletonQueryWizard {
178180
}
179181

180182
private async determineNextFileName(folderName: string): Promise<string> {
181-
if (this.storagePath === undefined) {
183+
if (this.qlPackStoragePath === undefined) {
182184
throw new Error("Workspace storage path is undefined");
183185
}
184186

185-
const folderUri = Uri.file(join(this.storagePath, folderName));
187+
const folderUri = Uri.file(join(this.qlPackStoragePath, folderName));
186188
const files = await workspace.fs.readDirectory(folderUri);
187189
const qlFiles = files.filter(([filename, _fileType]) =>
188190
filename.match(/example[0-9]*.ql/),
@@ -192,10 +194,14 @@ export class SkeletonQueryWizard {
192194
}
193195

194196
private async downloadDatabase() {
195-
if (this.storagePath === undefined) {
197+
if (this.qlPackStoragePath === undefined) {
196198
throw new Error("Workspace storage path is undefined");
197199
}
198200

201+
if (this.databaseStoragePath === undefined) {
202+
throw new Error("Database storage path is undefined");
203+
}
204+
199205
if (this.language === undefined) {
200206
throw new Error("Language is undefined");
201207
}
@@ -219,7 +225,7 @@ export class SkeletonQueryWizard {
219225
await databaseFetcher.downloadGitHubDatabase(
220226
chosenRepo,
221227
this.databaseManager,
222-
this.storagePath,
228+
this.databaseStoragePath,
223229
this.credentials,
224230
this.progress,
225231
this.token,
@@ -233,7 +239,7 @@ export class SkeletonQueryWizard {
233239
throw new Error("Language is undefined");
234240
}
235241

236-
if (this.storagePath === undefined) {
242+
if (this.qlPackStoragePath === undefined) {
237243
throw new Error("Workspace storage path is undefined");
238244
}
239245

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

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,11 @@ describe("SkeletonQueryWizard", () => {
8383
jest.spyOn(workspace, "workspaceFolders", "get").mockReturnValue([
8484
{
8585
name: `codespaces-codeql`,
86-
uri: { path: storagePath },
86+
uri: { fsPath: storagePath },
8787
},
8888
{
8989
name: "/second/folder/path",
90-
uri: { path: storagePath },
90+
uri: { fsPath: storagePath },
9191
},
9292
] as WorkspaceFolder[]);
9393

@@ -114,6 +114,7 @@ describe("SkeletonQueryWizard", () => {
114114
extLogger,
115115
mockDatabaseManager,
116116
token,
117+
storagePath,
117118
);
118119

119120
askForGitHubRepoSpy = jest
@@ -244,6 +245,7 @@ describe("SkeletonQueryWizard", () => {
244245
extLogger,
245246
mockDatabaseManagerWithItems,
246247
token,
248+
storagePath,
247249
);
248250
});
249251

@@ -305,8 +307,8 @@ describe("SkeletonQueryWizard", () => {
305307
it("should return the first workspace folder", async () => {
306308
jest.spyOn(workspace, "workspaceFolders", "get").mockReturnValue([
307309
{
308-
name: "codeql-custom-queries-cpp",
309-
uri: { path: "codespaces-codeql" },
310+
name: "codespaces-codeql",
311+
uri: { fsPath: "codespaces-codeql" },
310312
},
311313
] as WorkspaceFolder[]);
312314

@@ -317,6 +319,7 @@ describe("SkeletonQueryWizard", () => {
317319
extLogger,
318320
mockDatabaseManager,
319321
token,
322+
storagePath,
320323
);
321324

322325
expect(wizard.getFirstStoragePath()).toEqual("codespaces-codeql");
@@ -327,11 +330,21 @@ describe("SkeletonQueryWizard", () => {
327330
jest.spyOn(workspace, "workspaceFolders", "get").mockReturnValue([
328331
{
329332
name: "codeql-custom-queries-cpp",
330-
uri: { path: "vscode-codeql-starter/codeql-custom-queries-cpp" },
333+
uri: {
334+
fsPath: join(
335+
"vscode-codeql-starter",
336+
"codeql-custom-queries-cpp",
337+
),
338+
},
331339
},
332340
{
333341
name: "codeql-custom-queries-csharp",
334-
uri: { path: "vscode-codeql-starter/codeql-custom-queries-csharp" },
342+
uri: {
343+
fsPath: join(
344+
"vscode-codeql-starter",
345+
"codeql-custom-queries-csharp",
346+
),
347+
},
335348
},
336349
] as WorkspaceFolder[]);
337350

@@ -342,6 +355,7 @@ describe("SkeletonQueryWizard", () => {
342355
extLogger,
343356
mockDatabaseManager,
344357
token,
358+
storagePath,
345359
);
346360

347361
expect(wizard.getFirstStoragePath()).toEqual("vscode-codeql-starter");

0 commit comments

Comments
 (0)