Skip to content

Commit 1923f01

Browse files
committed
fix: fallback to previous code if wslpath is not available
1 parent 7b7e217 commit 1923f01

File tree

3 files changed

+77
-8
lines changed

3 files changed

+77
-8
lines changed

src/chrome-finder.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,9 @@ export function linux() {
176176
export function wsl() {
177177
// Manually populate the environment variables assuming it's the default config
178178
process.env.LOCALAPPDATA = getWSLLocalAppDataPath(`${process.env.PATH}`);
179-
process.env.PROGRAMFILES = toWSLPath('C:/Program Files');
180-
process.env['PROGRAMFILES(X86)'] = toWSLPath('C:/Program Files (x86)');
179+
process.env.PROGRAMFILES = toWSLPath('C:/Program Files', '/mnt/c/Program Files');
180+
process.env['PROGRAMFILES(X86)'] =
181+
toWSLPath('C:/Program Files (x86)', '/mnt/c/Program Files (x86)');
181182

182183
return win32();
183184
}

src/utils.ts

+32-4
Original file line numberDiff line numberDiff line change
@@ -73,23 +73,51 @@ export function makeTmpDir() {
7373
}
7474
}
7575

76+
function toWinDirFormat(dir: string = ''): string {
77+
const results = /\/mnt\/([a-z])\//.exec(dir);
78+
79+
if (!results) {
80+
return dir;
81+
}
82+
83+
const driveLetter = results[1];
84+
return dir.replace(`/mnt/${driveLetter}/`, `${driveLetter.toUpperCase()}:\\`)
85+
.replace(/\//g, '\\');
86+
}
87+
7688
export function toWin32Path(dir: string = ''): string {
7789
if (/[a-z]:\\/iu.test(dir)) {
7890
return dir;
7991
}
8092

81-
return execFileSync('wslpath', ['-w', dir]).toString().trim();
93+
try {
94+
return execFileSync('wslpath', ['-w', dir]).toString().trim();
95+
} catch {
96+
return toWinDirFormat(dir);
97+
}
98+
}
99+
100+
export function toWSLPath(dir: string, fallback: string): string {
101+
try {
102+
return execFileSync('wslpath', ['-u', dir]).toString().trim();
103+
} catch {
104+
return fallback;
105+
}
82106
}
83107

84-
export function toWSLPath(dir: string = ''): string {
85-
return execFileSync('wslpath', ['-u', dir]).toString().trim();
108+
function getLocalAppDataPath(path: string): string {
109+
const userRegExp = /\/mnt\/([a-z])\/Users\/([^\/:]+)\/AppData\//;
110+
const results = userRegExp.exec(path) || [];
111+
112+
return `/mnt/${results[1]}/Users/${results[2]}/AppData/Local`;
86113
}
87114

88115
export function getWSLLocalAppDataPath(path: string): string {
89116
const userRegExp = /\/([a-z])\/Users\/([^\/:]+)\/AppData\//;
90117
const results = userRegExp.exec(path) || [];
91118

92-
return toWSLPath(`${results[1]}:\\Users\\${results[2]}\\AppData\\Local`);
119+
return toWSLPath(
120+
`${results[1]}:\\Users\\${results[2]}\\AppData\\Local`, getLocalAppDataPath(path));
93121
}
94122

95123
function makeUnixTmpDir() {

test/utils-test.ts

+42-2
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,24 @@ describe('toWin32Path', () => {
4545
assert.ok(execFileSyncStub.notCalled);
4646
});
4747
})
48+
49+
describe('when wslpath is not available', () => {
50+
beforeEach(() => execFileSyncStub.throws(new Error('oh noes!')));
51+
52+
it('falls back to the toWinDirFormat method', () => {
53+
const wsl = '/mnt/c/Users/user1/AppData/';
54+
const windows = 'C:\\Users\\user1\\AppData\\';
55+
56+
assert.strictEqual(toWin32Path(wsl), windows);
57+
});
58+
59+
it('supports the drive letter not being C', () => {
60+
const wsl = '/mnt/d/Users/user1/AppData';
61+
const windows = 'D:\\Users\\user1\\AppData';
62+
63+
assert.strictEqual(toWin32Path(wsl), windows);
64+
})
65+
});
4866
})
4967

5068
describe('toWSLPath', () => {
@@ -53,15 +71,26 @@ describe('toWSLPath', () => {
5371
it('calls wslpath -u', () => {
5472
execFileSyncStub.returns(asBuffer(''));
5573

56-
toWSLPath('');
74+
toWSLPath('', '');
5775

5876
assert.ok(execFileSyncStub.calledWith('wslpath', ['-u', '']));
5977
})
6078

6179
it('trims off the trailing newline', () => {
6280
execFileSyncStub.returns(asBuffer('the-path\n'));
6381

64-
assert.strictEqual(toWSLPath(''), 'the-path');
82+
assert.strictEqual(toWSLPath('', ''), 'the-path');
83+
})
84+
85+
describe('when wslpath is not available', () => {
86+
beforeEach(() => execFileSyncStub.throws(new Error('oh noes!')));
87+
88+
it('uses the fallback path', () => {
89+
assert.strictEqual(
90+
toWSLPath('C:/Program Files', '/mnt/c/Program Files'),
91+
'/mnt/c/Program Files'
92+
);
93+
})
6594
})
6695
})
6796

@@ -76,4 +105,15 @@ describe('getWSLLocalAppDataPath', () => {
76105
assert.strictEqual(getWSLLocalAppDataPath(path), '/c/folder/');
77106
assert.ok(execFileSyncStub.calledWith('wslpath', ['-u', 'c:\\Users\\user1\\AppData\\Local']));
78107
});
108+
109+
describe('when wslpath is not available', () => {
110+
beforeEach(() => execFileSyncStub.throws(new Error('oh noes!')));
111+
112+
it('falls back to the getLocalAppDataPath method', () => {
113+
const path = '/mnt/c/Users/user1/.bin:/mnt/c/Users/user1:/mnt/c/Users/user1/AppData/';
114+
const appDataPath = '/mnt/c/Users/user1/AppData/Local';
115+
116+
assert.strictEqual(getWSLLocalAppDataPath(path), appDataPath);
117+
});
118+
});
79119
});

0 commit comments

Comments
 (0)