Skip to content

Commit d7e4c10

Browse files
authored
bugfix: Correctly parse the WSL path to Windows path (LeetCode-OpenSource#510)
1 parent 1a129db commit d7e4c10

File tree

2 files changed

+8
-14
lines changed

2 files changed

+8
-14
lines changed

src/leetCodeExecutor.ts

+4-13
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,11 @@ import { toWslPath, useWsl } from "./utils/wslUtils";
1414

1515
class LeetCodeExecutor implements Disposable {
1616
private leetCodeRootPath: string;
17-
private leetCodeRootPathInWsl: string;
1817
private nodeExecutable: string;
1918
private configurationChangeListener: Disposable;
2019

2120
constructor() {
2221
this.leetCodeRootPath = path.join(__dirname, "..", "..", "node_modules", "vsc-leetcode-cli");
23-
this.leetCodeRootPathInWsl = "";
2422
this.nodeExecutable = this.getNodePath();
2523
this.configurationChangeListener = workspace.onDidChangeConfiguration((event: ConfigurationChangeEvent) => {
2624
if (event.affectsConfiguration("leetcode.nodePath")) {
@@ -29,18 +27,11 @@ class LeetCodeExecutor implements Disposable {
2927
}, this);
3028
}
3129

32-
public async getLeetCodeRootPath(): Promise<string> { // not wrapped by ""
30+
public async getLeetCodeBinaryPath(): Promise<string> {
3331
if (wsl.useWsl()) {
34-
if (!this.leetCodeRootPathInWsl) {
35-
this.leetCodeRootPathInWsl = `${await wsl.toWslPath(this.leetCodeRootPath)}`;
36-
}
37-
return `${this.leetCodeRootPathInWsl}`;
32+
return `${await wsl.toWslPath(`"${path.join(this.leetCodeRootPath, "bin", "leetcode")}"`)}`;
3833
}
39-
return `${this.leetCodeRootPath}`;
40-
}
41-
42-
public async getLeetCodeBinaryPath(): Promise<string> { // wrapped by ""
43-
return `"${path.join(await this.getLeetCodeRootPath(), "bin", "leetcode")}"`;
34+
return `"${path.join(this.leetCodeRootPath, "bin", "leetcode")}"`;
4435
}
4536

4637
public async meetRequirements(): Promise<boolean> {
@@ -168,7 +159,7 @@ class LeetCodeExecutor implements Disposable {
168159

169160
public async getCompaniesAndTags(): Promise<{ companies: { [key: string]: string[] }, tags: { [key: string]: string[] } }> {
170161
// preprocess the plugin source
171-
const companiesTagsPath: string = path.join(await leetCodeExecutor.getLeetCodeRootPath(), "lib", "plugins", "company.js");
162+
const companiesTagsPath: string = path.join(this.leetCodeRootPath, "lib", "plugins", "company.js");
172163
const companiesTagsSrc: string = (await fse.readFile(companiesTagsPath, "utf8")).replace(
173164
"module.exports = plugin",
174165
"module.exports = { COMPONIES, TAGS }",

src/utils/wslUtils.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,8 @@ export async function toWslPath(path: string): Promise<string> {
1515
}
1616

1717
export async function toWinPath(path: string): Promise<string> {
18-
return (await executeCommand("wsl", ["wslpath", "-w", `"${path}"`])).trim();
18+
if (path.startsWith("\\mnt\\")) {
19+
return (await executeCommand("wsl", ["wslpath", "-w", `"${path.replace(/\\/g, "/").substr(0, 6)}"`])).trim() + path.substr(7);
20+
}
21+
return (await executeCommand("wsl", ["wslpath", "-w", "/"])).trim() + path;
1922
}

0 commit comments

Comments
 (0)