Skip to content

Commit bc4d742

Browse files
authored
Stop allowing running MRVA with query outside of workspace (#3302)
1 parent 7017180 commit bc4d742

5 files changed

Lines changed: 36 additions & 23 deletions

File tree

extensions/ql-vscode/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
## [UNRELEASED]
44

5+
- Stop allowing running MRVA with a query outside of the workspace. [#3302](https://github.com/github/vscode-codeql/pull/3302)
6+
57
## 1.12.1 - 31 January 2024
68

79
- Enable collection of telemetry for the `codeQL.addingDatabases.addDatabaseSourceToWorkspace` setting. [#3238](https://github.com/github/vscode-codeql/pull/3238)

extensions/ql-vscode/src/common/files.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { pathExists, stat, readdir, opendir } from "fs-extra";
1+
import { pathExists, stat, readdir, opendir, lstatSync } from "fs-extra";
22
import { dirname, isAbsolute, join, relative, resolve } from "path";
33
import { tmpdir as osTmpdir } from "os";
44

@@ -149,6 +149,11 @@ export function findCommonParentDir(...paths: string[]): string {
149149

150150
paths = paths.map((path) => normalizePath(path));
151151

152+
// If there's only one path and it's a file, return its dirname
153+
if (paths.length === 1) {
154+
return lstatSync(paths[0]).isFile() ? dirname(paths[0]) : paths[0];
155+
}
156+
152157
let commonDir = paths[0];
153158
while (!paths.every((path) => containsPath(commonDir, path))) {
154159
if (isTopLevelPath(commonDir)) {

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

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { dirname } from "path";
21
import { containsPath, findCommonParentDir } from "../common/files";
32
import { findPackRoot } from "../common/ql";
43

@@ -9,8 +8,8 @@ import { findPackRoot } from "../common/ql";
98
* - If all query files are in the same QL pack, it returns the root directory of that pack.
109
* - If the query files are in different QL packs, it throws an error.
1110
* - If some query files are in a QL pack and some aren't, it throws an error.
12-
* - If none of the query files are in a QL pack, it returns the common parent directory of the query files. However,
13-
* if there are more than one query files and they're not in the same workspace folder, it throws an error.
11+
* - If none of the query files are in a QL pack, it returns the common parent directory of the query
12+
* files. However, if the common parent directory is not in a workspace folder, it throws an error.
1413
*
1514
* @param queryFiles - An array of file paths for the query files.
1615
* @param workspaceFolders - An array of workspace folder paths.
@@ -31,10 +30,6 @@ export async function findVariantAnalysisQlPackRoot(
3130
packRoots.push(packRoot);
3231
}
3332

34-
if (queryFiles.length === 1) {
35-
return packRoots[0] ?? dirname(queryFiles[0]);
36-
}
37-
3833
const uniquePackRoots = Array.from(new Set(packRoots));
3934

4035
if (uniquePackRoots.length > 1) {

extensions/ql-vscode/test/unit-tests/common/files.test.ts

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -541,14 +541,6 @@ describe("findCommonParentDir", () => {
541541
expect(commonDir).toEqualPath(rootDir);
542542
});
543543

544-
it("should handle a single path", async () => {
545-
const paths = [join("/foo", "bar", "baz")];
546-
547-
const commonDir = findCommonParentDir(...paths);
548-
549-
expect(commonDir).toEqualPath(join("/foo", "bar", "baz"));
550-
});
551-
552544
it("should return the same path if all paths are identical", () => {
553545
const paths = [
554546
join("/foo", "bar", "baz"),
@@ -580,4 +572,23 @@ describe("findCommonParentDir", () => {
580572

581573
expect(commonDir).toEqualPath(rootDir);
582574
});
575+
576+
it("should return the parent dir of a single file", async () => {
577+
const dataDir = join(__dirname, "../../data");
578+
const paths = [dataDir];
579+
580+
const commonDir = findCommonParentDir(...paths);
581+
582+
expect(commonDir).toEqualPath(dataDir);
583+
});
584+
585+
it("should return the dir if a single dir is provided", async () => {
586+
const dataDir = join(__dirname, "../../data");
587+
const filePath = join(dataDir, "query.ql");
588+
const paths = [filePath];
589+
590+
const commonDir = findCommonParentDir(...paths);
591+
592+
expect(commonDir).toEqualPath(dataDir);
593+
});
583594
});

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,15 @@ describe("findVariantAnalysisQlPackRoot", () => {
4545
expect(packRoot).toEqualPath(getFullPath("workspace1"));
4646
});
4747

48-
it("should return the pack root of a single query not in a pack or workspace", async () => {
49-
const queryFiles = [getFullPath("dir1/query1.ql")];
48+
it("should fail if single query not in a pack or workspace", async () => {
49+
const queryFiles = [getFullPath("workspace1/query1.ql")];
50+
const workspaceFolders = [getFullPath("workspace2")];
5051

51-
const packRoot = await findVariantAnalysisQlPackRoot(
52-
queryFiles,
53-
workspaceFolders,
52+
await expect(
53+
findVariantAnalysisQlPackRoot(queryFiles, workspaceFolders),
54+
).rejects.toThrow(
55+
"All queries must be within the workspace and within the same workspace root",
5456
);
55-
56-
expect(packRoot).toEqualPath(getFullPath("dir1"));
5757
});
5858

5959
it("should throw an error if some queries are in a pack and some are not", async () => {

0 commit comments

Comments
 (0)