From 1923f010749fbce49eb46dc8802a39bd23412f91 Mon Sep 17 00:00:00 2001 From: Gareth Jones Date: Sun, 30 May 2021 14:49:36 +1200 Subject: [PATCH] fix: fallback to previous code if `wslpath` is not available --- src/chrome-finder.ts | 5 +++-- src/utils.ts | 36 ++++++++++++++++++++++++++++++++---- test/utils-test.ts | 44 ++++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 77 insertions(+), 8 deletions(-) diff --git a/src/chrome-finder.ts b/src/chrome-finder.ts index 06c7f3f65..4e0548cd9 100644 --- a/src/chrome-finder.ts +++ b/src/chrome-finder.ts @@ -176,8 +176,9 @@ export function linux() { export function wsl() { // Manually populate the environment variables assuming it's the default config process.env.LOCALAPPDATA = getWSLLocalAppDataPath(`${process.env.PATH}`); - process.env.PROGRAMFILES = toWSLPath('C:/Program Files'); - process.env['PROGRAMFILES(X86)'] = toWSLPath('C:/Program Files (x86)'); + process.env.PROGRAMFILES = toWSLPath('C:/Program Files', '/mnt/c/Program Files'); + process.env['PROGRAMFILES(X86)'] = + toWSLPath('C:/Program Files (x86)', '/mnt/c/Program Files (x86)'); return win32(); } diff --git a/src/utils.ts b/src/utils.ts index b6e837eca..1492705af 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -73,23 +73,51 @@ export function makeTmpDir() { } } +function toWinDirFormat(dir: string = ''): string { + const results = /\/mnt\/([a-z])\//.exec(dir); + + if (!results) { + return dir; + } + + const driveLetter = results[1]; + return dir.replace(`/mnt/${driveLetter}/`, `${driveLetter.toUpperCase()}:\\`) + .replace(/\//g, '\\'); +} + export function toWin32Path(dir: string = ''): string { if (/[a-z]:\\/iu.test(dir)) { return dir; } - return execFileSync('wslpath', ['-w', dir]).toString().trim(); + try { + return execFileSync('wslpath', ['-w', dir]).toString().trim(); + } catch { + return toWinDirFormat(dir); + } +} + +export function toWSLPath(dir: string, fallback: string): string { + try { + return execFileSync('wslpath', ['-u', dir]).toString().trim(); + } catch { + return fallback; + } } -export function toWSLPath(dir: string = ''): string { - return execFileSync('wslpath', ['-u', dir]).toString().trim(); +function getLocalAppDataPath(path: string): string { + const userRegExp = /\/mnt\/([a-z])\/Users\/([^\/:]+)\/AppData\//; + const results = userRegExp.exec(path) || []; + + return `/mnt/${results[1]}/Users/${results[2]}/AppData/Local`; } export function getWSLLocalAppDataPath(path: string): string { const userRegExp = /\/([a-z])\/Users\/([^\/:]+)\/AppData\//; const results = userRegExp.exec(path) || []; - return toWSLPath(`${results[1]}:\\Users\\${results[2]}\\AppData\\Local`); + return toWSLPath( + `${results[1]}:\\Users\\${results[2]}\\AppData\\Local`, getLocalAppDataPath(path)); } function makeUnixTmpDir() { diff --git a/test/utils-test.ts b/test/utils-test.ts index f11b55fd2..72cb0dd66 100644 --- a/test/utils-test.ts +++ b/test/utils-test.ts @@ -45,6 +45,24 @@ describe('toWin32Path', () => { assert.ok(execFileSyncStub.notCalled); }); }) + + describe('when wslpath is not available', () => { + beforeEach(() => execFileSyncStub.throws(new Error('oh noes!'))); + + it('falls back to the toWinDirFormat method', () => { + const wsl = '/mnt/c/Users/user1/AppData/'; + const windows = 'C:\\Users\\user1\\AppData\\'; + + assert.strictEqual(toWin32Path(wsl), windows); + }); + + it('supports the drive letter not being C', () => { + const wsl = '/mnt/d/Users/user1/AppData'; + const windows = 'D:\\Users\\user1\\AppData'; + + assert.strictEqual(toWin32Path(wsl), windows); + }) + }); }) describe('toWSLPath', () => { @@ -53,7 +71,7 @@ describe('toWSLPath', () => { it('calls wslpath -u', () => { execFileSyncStub.returns(asBuffer('')); - toWSLPath(''); + toWSLPath('', ''); assert.ok(execFileSyncStub.calledWith('wslpath', ['-u', ''])); }) @@ -61,7 +79,18 @@ describe('toWSLPath', () => { it('trims off the trailing newline', () => { execFileSyncStub.returns(asBuffer('the-path\n')); - assert.strictEqual(toWSLPath(''), 'the-path'); + assert.strictEqual(toWSLPath('', ''), 'the-path'); + }) + + describe('when wslpath is not available', () => { + beforeEach(() => execFileSyncStub.throws(new Error('oh noes!'))); + + it('uses the fallback path', () => { + assert.strictEqual( + toWSLPath('C:/Program Files', '/mnt/c/Program Files'), + '/mnt/c/Program Files' + ); + }) }) }) @@ -76,4 +105,15 @@ describe('getWSLLocalAppDataPath', () => { assert.strictEqual(getWSLLocalAppDataPath(path), '/c/folder/'); assert.ok(execFileSyncStub.calledWith('wslpath', ['-u', 'c:\\Users\\user1\\AppData\\Local'])); }); + + describe('when wslpath is not available', () => { + beforeEach(() => execFileSyncStub.throws(new Error('oh noes!'))); + + it('falls back to the getLocalAppDataPath method', () => { + const path = '/mnt/c/Users/user1/.bin:/mnt/c/Users/user1:/mnt/c/Users/user1/AppData/'; + const appDataPath = '/mnt/c/Users/user1/AppData/Local'; + + assert.strictEqual(getWSLLocalAppDataPath(path), appDataPath); + }); + }); });