Skip to content

Commit 4bc97b8

Browse files
committed
Show correct query filename on Windows
On Windows, we were showing the full path to the query, rather than just the filename. This is because the `path` package being imported was actually `path-browserify` which only claims support for POSIX. Since Windows uses backslashes rather than forward slashes for paths, this resulted in the full path being shown. This creates a new `basename` function that works on both POSIX and Windows by detecting whether a POSIX or Windows path is given. This ensures that the correct path is shown on Windows, and will also ensure that we show the correct path on Linux if the user has opened a variant analysis that was originally created on Windows.
1 parent f661386 commit 4bc97b8

3 files changed

Lines changed: 66 additions & 2 deletions

File tree

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { basename } from "../path";
2+
3+
describe(basename.name, () => {
4+
const testCases = [
5+
{ path: "test.ql", expected: "test.ql" },
6+
{ path: "PLACEHOLDER/q0.ql", expected: "q0.ql" },
7+
{
8+
path: "/home/github/projects/vscode-codeql-starter/codeql-custom-queries-javascript/example.ql",
9+
expected: "example.ql",
10+
},
11+
{
12+
path: "C:\\Users\\github\\projects\\vscode-codeql-starter\\codeql-custom-queries-javascript\\example.ql",
13+
expected: "example.ql",
14+
},
15+
{
16+
path: "/home/github/projects/vscode-codeql-starter/codeql-custom-queries-javascript//",
17+
expected: "codeql-custom-queries-javascript",
18+
},
19+
{
20+
path: "C:\\Users\\github\\projects\\vscode-codeql-starter\\codeql-custom-queries-javascript\\",
21+
expected: "codeql-custom-queries-javascript",
22+
},
23+
{
24+
path: "/etc/hosts",
25+
expected: "hosts",
26+
},
27+
{
28+
path: "/etc/hosts/",
29+
expected: "hosts",
30+
},
31+
{
32+
path: "/etc/hosts\\test",
33+
expected: "hosts\\test",
34+
},
35+
];
36+
37+
test.each(testCases)(
38+
"basename of $path is $expected",
39+
({ path, expected }) => {
40+
expect(basename(path)).toEqual(expected);
41+
},
42+
);
43+
});
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Returns the basename of a path. Trailing directory separators are ignored.
2+
// Works for both POSIX and Windows paths.
3+
export const basename = (path: string): string => {
4+
// If the path contains a forward slash, that means it's a POSIX path. Windows does not allow
5+
// forward slashes in file names.
6+
if (path.includes("/")) {
7+
// Trim trailing slashes
8+
path = path.replace(/\/+$/, "");
9+
10+
const index = path.lastIndexOf("/");
11+
return index === -1 ? path : path.slice(index + 1);
12+
}
13+
14+
// Otherwise, it's a Windows path. We can use the backslash as a separator.
15+
16+
// Trim trailing slashes
17+
path = path.replace(/\\+$/, "");
18+
19+
const index = path.lastIndexOf("\\");
20+
return index === -1 ? path : path.slice(index + 1);
21+
};

extensions/ql-vscode/src/view/variant-analysis/VariantAnalysisHeader.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import * as path from "path";
21
import * as React from "react";
32
import { useMemo } from "react";
43
import styled from "styled-components";
@@ -12,6 +11,7 @@ import { QueryDetails } from "./QueryDetails";
1211
import { VariantAnalysisActions } from "./VariantAnalysisActions";
1312
import { VariantAnalysisStats } from "./VariantAnalysisStats";
1413
import { parseDate } from "../../pure/date";
14+
import { basename } from "../common/path";
1515

1616
export type VariantAnalysisHeaderProps = {
1717
variantAnalysis: VariantAnalysis;
@@ -68,7 +68,7 @@ export const VariantAnalysisHeader = ({
6868
<Row>
6969
<QueryDetails
7070
queryName={variantAnalysis.query.name}
71-
queryFileName={path.basename(variantAnalysis.query.filePath)}
71+
queryFileName={basename(variantAnalysis.query.filePath)}
7272
onOpenQueryFileClick={onOpenQueryFileClick}
7373
onViewQueryTextClick={onViewQueryTextClick}
7474
/>

0 commit comments

Comments
 (0)