Skip to content

Commit 6e40cad

Browse files
authored
fix: weaudit files from different OSes fail to load due to path differences (#41) (#94)
* fix: weaudit config files from different OSes are not cross-platform (#41) * prettier * fix docstr
1 parent 98b9a96 commit 6e40cad

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

src/codeMarker.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ import {
4747
configEntryEquals,
4848
RootPathAndLabel,
4949
} from "./types";
50+
import { normalizePathForOS } from "./utilities/normalizePath";
5051

5152
export const SERIALIZED_FILE_EXTENSION = ".weaudit";
5253
const DAY_LOG_FILENAME = ".weauditdaylog";
@@ -3315,6 +3316,28 @@ export class CodeMarker implements vscode.TreeDataProvider<TreeEntry> {
33153316
),
33163317
} as FullSerializedData;
33173318

3319+
// Normalize all the paths from loaded files. These can come from different OSes with different path
3320+
// conventions. We do a best effort to match them to the current OS format.
3321+
fullParsedEntries.treeEntries.forEach((entry) => {
3322+
entry.locations.forEach((loc) => {
3323+
loc.path = normalizePathForOS(rootPath, loc.path);
3324+
});
3325+
});
3326+
3327+
fullParsedEntries.resolvedEntries.forEach((entry) => {
3328+
entry.locations.forEach((loc) => {
3329+
loc.path = normalizePathForOS(rootPath, loc.path);
3330+
});
3331+
});
3332+
3333+
fullParsedEntries.auditedFiles.forEach((auditedFile) => {
3334+
auditedFile.path = normalizePathForOS(rootPath, auditedFile.path);
3335+
});
3336+
3337+
fullParsedEntries.partiallyAuditedFiles?.forEach((partiallyAuditedFile) => {
3338+
partiallyAuditedFile.path = normalizePathForOS(rootPath, partiallyAuditedFile.path);
3339+
});
3340+
33183341
if (update) {
33193342
if (add) {
33203343
// Remove potential entries of username which appear on the tree.

src/utilities/normalizePath.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import * as path from "path";
2+
import * as fs from "fs";
3+
4+
/**
5+
* A method that normalizes the slashes in a path based on current operating system.
6+
*
7+
* @param wsRoot - The workspace root path to resolve relative paths
8+
* @param filePath - The file path string to normalize
9+
* @returns The path with slashes normalized for the current operating system
10+
*/
11+
export function normalizePathForOS(wsRoot: string, filePath: string): string {
12+
if (process.platform === "win32" && filePath.includes("/")) {
13+
// Unix-style paths on Windows
14+
return path.normalize(filePath);
15+
} else if (process.platform !== "win32" && filePath.includes("\\")) {
16+
if (fs.existsSync(path.join(wsRoot, filePath))) {
17+
// Hateful edge case, this is a unix-style path with backslashes in the file name.
18+
return filePath;
19+
} else {
20+
// Windows path on non-windows OS
21+
return filePath.replace(/\\/g, "/");
22+
}
23+
}
24+
return filePath;
25+
}

0 commit comments

Comments
 (0)