Skip to content

Commit f32a240

Browse files
committed
Exclude workspace folders in the system temp dir
1 parent 1cc6aa5 commit f32a240

File tree

3 files changed

+51
-2
lines changed

3 files changed

+51
-2
lines changed

extensions/ql-vscode/src/data-extensions-editor/extensions-workspace-folder.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Uri, window, workspace, WorkspaceFolder } from "vscode";
22
import { getOnDiskWorkspaceFoldersObjects } from "../common/vscode/workspace-folders";
33
import { extLogger } from "../common";
4+
import { tmpdir } from "../pure/files";
45

56
/**
67
* Returns the ancestors of this path in order from furthest to closest (i.e. root of filesystem to parent directory)
@@ -26,7 +27,15 @@ function getRootWorkspaceDirectory(): Uri | undefined {
2627
return Uri.joinPath(workspaceFile, "..");
2728
}
2829

29-
const workspaceFolders = getOnDiskWorkspaceFoldersObjects();
30+
const allWorkspaceFolders = getOnDiskWorkspaceFoldersObjects();
31+
32+
// Get the system temp directory and convert it to a URI so it's normalized
33+
const systemTmpdir = Uri.file(tmpdir());
34+
35+
const workspaceFolders = allWorkspaceFolders.filter((folder) => {
36+
// Never use a workspace folder that is in the system temp directory
37+
return !folder.uri.fsPath.startsWith(systemTmpdir.fsPath);
38+
});
3039

3140
// Find the common root directory of all workspace folders by finding the longest common prefix
3241
const commonRoot = workspaceFolders.reduce((commonRoot, folder) => {

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { pathExists, stat, readdir, opendir } from "fs-extra";
22
import { isAbsolute, join, relative, resolve } from "path";
3+
import { tmpdir as osTmpdir } from "os";
34

45
/**
56
* Recursively finds all .ql files in this set of Uris.
@@ -121,3 +122,8 @@ export interface IOError {
121122
export function isIOError(e: any): e is IOError {
122123
return e.code !== undefined && typeof e.code === "string";
123124
}
125+
126+
// This function is a wrapper around `os.tmpdir()` to make it easier to mock in tests.
127+
export function tmpdir(): string {
128+
return osTmpdir();
129+
}

extensions/ql-vscode/test/vscode-tests/no-workspace/data-extensions-editor/extensions-workspace-folder.test.ts

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { Uri, workspace, WorkspaceFolder } from "vscode";
22
import { dir, DirectoryResult } from "tmp-promise";
3+
import { join } from "path";
34
import { autoPickExtensionsDirectory } from "../../../../src/data-extensions-editor/extensions-workspace-folder";
5+
import * as files from "../../../../src/pure/files";
46

57
describe("autoPickExtensionsDirectory", () => {
68
let tmpDir: DirectoryResult;
@@ -15,20 +17,24 @@ describe("autoPickExtensionsDirectory", () => {
1517
let updateWorkspaceFoldersSpy: jest.SpiedFunction<
1618
typeof workspace.updateWorkspaceFolders
1719
>;
20+
let mockedTmpDirUri: Uri;
1821

1922
beforeEach(async () => {
2023
tmpDir = await dir({
2124
unsafeCleanup: true,
2225
});
2326

24-
rootDirectory = Uri.file(tmpDir.path);
27+
rootDirectory = Uri.joinPath(Uri.file(tmpDir.path), "root");
2528
extensionsDirectory = Uri.joinPath(
2629
rootDirectory,
2730
".github",
2831
"codeql",
2932
"extensions",
3033
);
3134

35+
const mockedTmpDir = join(tmpDir.path, ".tmp", "tmp");
36+
mockedTmpDirUri = Uri.file(mockedTmpDir);
37+
3238
workspaceFoldersSpy = jest
3339
.spyOn(workspace, "workspaceFolders", "get")
3440
.mockReturnValue([]);
@@ -38,6 +44,8 @@ describe("autoPickExtensionsDirectory", () => {
3844
updateWorkspaceFoldersSpy = jest
3945
.spyOn(workspace, "updateWorkspaceFolders")
4046
.mockReturnValue(true);
47+
48+
jest.spyOn(files, "tmpdir").mockReturnValue(mockedTmpDir);
4149
});
4250

4351
afterEach(async () => {
@@ -136,6 +144,32 @@ describe("autoPickExtensionsDirectory", () => {
136144
});
137145
});
138146

147+
it("when a workspace file does not exist and there is a temp dir as workspace folder", async () => {
148+
workspaceFoldersSpy.mockReturnValue([
149+
{
150+
uri: Uri.joinPath(rootDirectory, "codeql-custom-queries-java"),
151+
name: "codeql-custom-queries-java",
152+
index: 0,
153+
},
154+
{
155+
uri: Uri.joinPath(rootDirectory, "codeql-custom-queries-python"),
156+
name: "codeql-custom-queries-python",
157+
index: 1,
158+
},
159+
{
160+
uri: Uri.joinPath(mockedTmpDirUri, "quick-queries"),
161+
name: "quick-queries",
162+
index: 2,
163+
},
164+
]);
165+
166+
expect(await autoPickExtensionsDirectory()).toEqual(extensionsDirectory);
167+
expect(updateWorkspaceFoldersSpy).toHaveBeenCalledWith(3, 0, {
168+
name: "CodeQL Extension Packs",
169+
uri: extensionsDirectory,
170+
});
171+
});
172+
139173
it("when a workspace file does not exist and there is no common root directory", async () => {
140174
workspaceFoldersSpy.mockReturnValue([
141175
{

0 commit comments

Comments
 (0)