Skip to content

Commit 8179476

Browse files
committed
Some improvements to findQlPackRootForQueriesWithNoPack
1 parent 840bc72 commit 8179476

File tree

2 files changed

+11
-22
lines changed
  • extensions/ql-vscode

2 files changed

+11
-22
lines changed

extensions/ql-vscode/src/variant-analysis/ql.ts

+9-20
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { dirname } from "path";
2-
import { findCommonParentDir } from "../common/files";
2+
import { containsPath, findCommonParentDir } from "../common/files";
33
import { findPackRoot } from "../common/ql";
44

55
/**
@@ -62,26 +62,15 @@ function findQlPackRootForQueriesWithNoPack(
6262
queryFiles: string[],
6363
workspaceFolders: string[],
6464
): string {
65-
const queryFileWorkspaceFolders = queryFiles.map((queryFile) =>
66-
workspaceFolders.find((workspaceFolder) =>
67-
queryFile.startsWith(workspaceFolder),
68-
),
69-
);
70-
71-
// Check if any query file is not part of the workspace.
72-
if (queryFileWorkspaceFolders.includes(undefined)) {
73-
throw Error(
74-
"Queries that are not part of a pack need to be part of the workspace",
75-
);
76-
}
65+
const commonParentDir = findCommonParentDir(...queryFiles);
7766

78-
// Check if query files are not in the same workspace folder.
79-
if (new Set(queryFileWorkspaceFolders).size > 1) {
80-
throw Error(
81-
"Queries that are not part of a pack need to be in the same workspace folder",
82-
);
67+
for (const workspaceFolder of workspaceFolders) {
68+
if (containsPath(workspaceFolder, commonParentDir)) {
69+
return commonParentDir;
70+
}
8371
}
8472

85-
// They're in the same workspace folder, so we can find a common parent dir.
86-
return findCommonParentDir(...queryFiles);
73+
throw Error(
74+
"All queries must be within the workspace and within the same workspace root",
75+
);
8776
}

extensions/ql-vscode/test/unit-tests/variant-analysis/ql.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ describe("findVariantAnalysisQlPackRoot", () => {
8787
await expect(
8888
findVariantAnalysisQlPackRoot(queryFiles, workspaceFolders),
8989
).rejects.toThrow(
90-
"Queries that are not part of a pack need to be in the same workspace folder",
90+
"All queries must be within the workspace and within the same workspace root",
9191
);
9292
});
9393

@@ -100,7 +100,7 @@ describe("findVariantAnalysisQlPackRoot", () => {
100100
await expect(
101101
findVariantAnalysisQlPackRoot(queryFiles, workspaceFolders),
102102
).rejects.toThrow(
103-
"Queries that are not part of a pack need to be part of the workspace",
103+
"All queries must be within the workspace and within the same workspace root",
104104
);
105105
});
106106

0 commit comments

Comments
 (0)