diff --git a/extensions/ql-vscode/src/databases/local-databases/database-item-impl.ts b/extensions/ql-vscode/src/databases/local-databases/database-item-impl.ts index 1c4adfc7e04..fa37938fd39 100644 --- a/extensions/ql-vscode/src/databases/local-databases/database-item-impl.ts +++ b/extensions/ql-vscode/src/databases/local-databases/database-item-impl.ts @@ -207,7 +207,6 @@ export class DatabaseItemImpl implements DatabaseItem { return pathsEqual( databasePath, join(testdir, `${testdirbase}.testproj`), - process.platform, ); } } catch { diff --git a/extensions/ql-vscode/src/databases/local-databases/database-manager.ts b/extensions/ql-vscode/src/databases/local-databases/database-manager.ts index 68e2b53191b..d57f0b73101 100644 --- a/extensions/ql-vscode/src/databases/local-databases/database-manager.ts +++ b/extensions/ql-vscode/src/databases/local-databases/database-manager.ts @@ -652,7 +652,7 @@ export class DatabaseManager extends DisposableObject { private isExtensionControlledLocation(uri: vscode.Uri) { const storageUri = this.ctx.storageUri || this.ctx.globalStorageUri; if (storageUri) { - return containsPath(storageUri.fsPath, uri.fsPath, process.platform); + return containsPath(storageUri.fsPath, uri.fsPath); } return false; } diff --git a/extensions/ql-vscode/src/pure/files.ts b/extensions/ql-vscode/src/pure/files.ts index 5b3d38d0d69..67dd837b3f5 100644 --- a/extensions/ql-vscode/src/pure/files.ts +++ b/extensions/ql-vscode/src/pure/files.ts @@ -1,5 +1,5 @@ import { pathExists, stat, readdir, opendir } from "fs-extra"; -import { join, resolve } from "path"; +import { isAbsolute, join, relative, resolve } from "path"; /** * Recursively finds all .ql files in this set of Uris. @@ -51,36 +51,32 @@ export async function getDirectoryNamesInsidePath( return dirNames; } -function normalizePath(path: string, platform: NodeJS.Platform): string { +function normalizePath(path: string): string { // On Windows, "C:/", "C:\", and "c:/" are all equivalent. We need // to normalize the paths to ensure they all get resolved to the // same format. On Windows, we also need to do the comparison // case-insensitively. path = resolve(path); - if (platform === "win32") { + if (process.platform === "win32") { path = path.toLowerCase(); } return path; } -export function pathsEqual( - path1: string, - path2: string, - platform: NodeJS.Platform, -): boolean { - return normalizePath(path1, platform) === normalizePath(path2, platform); +export function pathsEqual(path1: string, path2: string): boolean { + return normalizePath(path1) === normalizePath(path2); } /** - * Returns true if path1 contains path2. + * Returns true if `parent` contains `child`, or if they are equal. */ -export function containsPath( - path1: string, - path2: string, - platform: NodeJS.Platform, -): boolean { - return normalizePath(path2, platform).startsWith( - normalizePath(path1, platform), +export function containsPath(parent: string, child: string): boolean { + const relativePath = relative(parent, child); + return ( + !relativePath.startsWith("..") && + // On windows, if the two paths are in different drives, then the + // relative path will be an absolute path to the other drive. + !isAbsolute(relativePath) ); } diff --git a/extensions/ql-vscode/test/matchers/toEqualPath.ts b/extensions/ql-vscode/test/matchers/toEqualPath.ts index d598fcf981a..2e828cb7b03 100644 --- a/extensions/ql-vscode/test/matchers/toEqualPath.ts +++ b/extensions/ql-vscode/test/matchers/toEqualPath.ts @@ -10,7 +10,7 @@ const toEqualPath: MatcherFunction<[expectedPath: unknown]> = function ( throw new Error("These must be of type string!"); } - const pass = pathsEqual(actual, expectedPath, process.platform); + const pass = pathsEqual(actual, expectedPath); if (pass) { return { message: () => diff --git a/extensions/ql-vscode/test/unit-tests/pure/files.test.ts b/extensions/ql-vscode/test/unit-tests/pure/files.test.ts index 0b51ab88cd0..3e2a5a65d49 100644 --- a/extensions/ql-vscode/test/unit-tests/pure/files.test.ts +++ b/extensions/ql-vscode/test/unit-tests/pure/files.test.ts @@ -204,102 +204,142 @@ describe("pathsEqual", () => { return; } - expect(pathsEqual(path1, path2, platform)).toEqual(expected); + expect(pathsEqual(path1, path2)).toEqual(expected); }, ); }); describe("containsPath", () => { const testCases: Array<{ - path1: string; - path2: string; + parent: string; + child: string; platform: NodeJS.Platform; expected: boolean; }> = [ { - path1: + parent: "/home/github/projects/vscode-codeql-starter/codeql-custom-queries-javascript", - path2: + child: "/home/github/projects/vscode-codeql-starter/codeql-custom-queries-javascript/example.ql", platform: "linux", expected: true, }, { - path1: + parent: "/HOME/github/projects/vscode-codeql-starter/codeql-custom-queries-javascript", - path2: + child: "/home/github/projects/vscode-codeql-starter/codeql-custom-queries-javascript/example.ql", platform: "linux", expected: false, }, { - path1: + parent: "/home/github/projects/vscode-codeql-starter/codeql-custom-queries-javascript/example.ql", - path2: + child: "/home/github/projects/vscode-codeql-starter/codeql-custom-queries-javascript", platform: "linux", expected: false, }, { - path1: + parent: + "/home/github/projects/vscode-codeql-starter/codeql-custom-queries-java", + child: + "/home/github/projects/vscode-codeql-starter/codeql-custom-queries-javascript", + platform: "linux", + expected: false, + }, + { + parent: + "/home/github/projects/vscode-codeql-starter/codeql-custom-queries-java", + child: + "/home/github/projects/vscode-codeql-starter/codeql-custom-queries-javascript/example.ql", + platform: "linux", + expected: false, + }, + { + parent: "C:/Users/github/projects/vscode-codeql-starter/codeql-custom-queries-javascript", - path2: + child: "C:/Users/github/projects/vscode-codeql-starter/codeql-custom-queries-javascript/example.ql", platform: "win32", expected: true, }, { - path1: + parent: "C:/Users/github/projects/vscode-codeql-starter/codeql-custom-queries-javascript", - path2: + child: "c:/Users/github/projects/vscode-codeql-starter/codeql-custom-queries-javascript/example.ql", platform: "win32", expected: true, }, { - path1: + parent: "C:/Users/github/projects/vscode-codeql-starter/codeql-custom-queries-javascript", - path2: + child: + "C:/USERS/GITHUB/PROJECTS/VSCODE-CODEQL-STARTER/CODEQL-CUSTOM-QUERIES-JAVASCRIPT/EXAMPLE.QL", + platform: "win32", + expected: true, + }, + { + parent: + "C:/Users/github/projects/vscode-codeql-starter/codeql-custom-queries-javascript", + child: "D:/Users/github/projects/vscode-codeql-starter/codeql-custom-queries-javascript/example.ql", platform: "win32", expected: false, }, { - path1: + parent: "C:/Users/github/projects/vscode-codeql-starter/codeql-custom-queries-javascript/example.ql", - path2: + child: "C:/Users/github/projects/vscode-codeql-starter/codeql-custom-queries-javascript", platform: "win32", expected: false, }, { - path1: + parent: "C:/Users/github/projects/vscode-codeql-starter/codeql-custom-queries-javascript", - path2: + child: "C:\\Users\\github\\projects\\vscode-codeql-starter\\codeql-custom-queries-javascript\\example.ql", platform: "win32", expected: true, }, { - path1: + parent: "C:/Users/github/projects/vscode-codeql-starter/codeql-custom-queries-javascript", - path2: + child: "D:\\Users\\github\\projects\\vscode-codeql-starter\\codeql-custom-queries-javascript\\example.ql", platform: "win32", expected: false, }, + { + parent: + "C:/Users/github/projects/vscode-codeql-starter/codeql-custom-queries-java", + child: + "C:/Users/github/projects/vscode-codeql-starter/codeql-custom-queries-javascript", + platform: "win32", + expected: false, + }, + { + parent: + "C:/Users/github/projects/vscode-codeql-starter/codeql-custom-queries-java", + child: + "C:/Users/github/projects/vscode-codeql-starter/codeql-custom-queries-javascript/example.ql", + platform: "win32", + expected: false, + }, ]; test.each(testCases)( - "$path1 contains $path2 on $platform = $expected", - ({ path1, path2, platform, expected }) => { + "$parent contains $child on $platform = $expected", + ({ parent, child, platform, expected }) => { if (platform !== process.platform) { // We're using the platform-specific path.resolve, so we can't really run // these tests on all platforms. return; } - expect(containsPath(path1, path2, platform)).toEqual(expected); + expect(containsPath(parent, child)).toEqual(expected); }, ); });