Repository: electron/fiddle
Pull request #663 (2021-04-20 06:18:54)

fix(linux): use shell path, same as macOS
## Problem

On Linux, when you run Fiddle from an app launcher (e.g., via the Activities Launcher in Gnome), it uses the `PATH` environment variable as defined when X11/Wayland is started, that is, usually without all of the PATH changes from shellrc (i.e., your Bash/ZSH/etc user config). On macOS, this is fixed with the `fix-path` Node module. Linux should be fixed similarly, but `fix-path` is [weirdly limited to macOS](https://github.com/sindresorhus/fix-path/blob/c7578359558831402413d5d8323057406bfdf40d/index.js#L5-L7).

## Solution

Drop `fix-path`, replace with `shell-path` (which is a direct dependency of `fix-path`), and self-implement a subset of `fix-path`.

coveralls: 
[![Coverage Status](https://coveralls.io/builds/38958619/badge)](https://coveralls.io/builds/38958619)

Coverage decreased (-0.04%) to 95.177% when pulling **37c29c160a0c3de8a55180f4343d9e196d6dc8de on malept/fix/shell-path-on-linux** into **8fea02de016f19dcfe9d859a876772088b24a397 on master**.


Patch:
diff --git a/package.json b/package.json
index e634a462..f656f857 100644
--- a/package.json
+++ b/package.json
@@ -50,7 +50,6 @@
     "electron-devtools-installer": "^3.1.1",
     "electron-squirrel-startup": "^1.0.0",
     "extract-zip": "^2.0.1",
-    "fix-path": "^3.0.0",
     "fs-extra": "^9.1.0",
     "mobx": "^5.15.7",
     "mobx-react": "^6.3.1",
@@ -61,6 +60,7 @@
     "react-dom": "^16.14.0",
     "react-mosaic-component": "^4.1.1",
     "semver": "^7.3.4",
+    "shell-path": "^2.1.0",
     "tmp": "0.2.1",
     "tslib": "^2.1.0",
     "update-electron-app": "^2.0.1",
diff --git a/src/ambient-modules.d.ts b/src/ambient-modules.d.ts
index 9c2b7bda..4ec6894d 100644
--- a/src/ambient-modules.d.ts
+++ b/src/ambient-modules.d.ts
@@ -1,2 +1,2 @@
-declare module 'fix-path';
 declare module 'namor';
+declare module 'shell-path';
diff --git a/src/utils/exec.ts b/src/utils/exec.ts
index 98678740..610aee85 100644
--- a/src/utils/exec.ts
+++ b/src/utils/exec.ts
@@ -1,5 +1,5 @@
 // Singleton: We don't want to do this more than once
-let _fixPathCalled = false;
+let _shellPathCalled = false;
 
 /**
  * Execute a command in a directory.
@@ -32,16 +32,19 @@ export async function exec(dir: string, cliArgs: string): Promise<string> {
 }
 
 /**
- * On macOS, we need to fix the $PATH environment variable
+ * On macOS & Linux, we need to fix the $PATH environment variable
  * so that we can call `npm`.
  *
  * @returns {Promise<void>}
  */
 export async function maybeFixPath(): Promise<void> {
-  if (!_fixPathCalled && process.platform !== 'win32') {
-    const fixPaths = require('fix-path');
-    fixPaths();
+  if (!_shellPathCalled && process.platform !== 'win32') {
+    const shellPaths = require('shell-path');
+    const paths = await shellPaths();
+    if (paths) {
+      process.env.PATH = paths;
+    }
   }
 
-  _fixPathCalled = true;
+  _shellPathCalled = true;
 }
diff --git a/yarn.lock b/yarn.lock
index c2c2fc23..c5a41226 100644
--- a/yarn.lock
+++ b/yarn.lock
@@ -5788,13 +5788,6 @@ find-up@^5.0.0:
     locate-path "^6.0.0"
     path-exists "^4.0.0"
 
-fix-path@^3.0.0:
-  version "3.0.0"
-  resolved "https://registry.yarnpkg.com/fix-path/-/fix-path-3.0.0.tgz#c6b82fd5f5928e520b392a63565ebfef0ddf037e"
-  integrity sha512-opGAl4+ip5jUikHR2C8Jo7czZ80pz8EK/0gMlAZu7xgDmBqIynlX8SMYg9KowYjAU6HT0nxsSJEWru0u+n+N2Q==
-  dependencies:
-    shell-path "^2.1.0"
-
 flat-cache@^2.0.1:
   version "2.0.1"
   resolved "https://registry.yarnpkg.com/flat-cache/-/flat-cache-2.0.1.tgz#5d296d6f04bda44a4630a301413bdbc2ec085ec0"


Test patch:
diff --git a/tests/utils/exec-spec.ts b/tests/utils/exec-spec.ts
index db362de3..57abfa79 100644
--- a/tests/utils/exec-spec.ts
+++ b/tests/utils/exec-spec.ts
@@ -1,7 +1,7 @@
 jest.mock('child_process');
 
-const mockFixPath = jest.fn();
-jest.mock('fix-path', () => mockFixPath);
+const mockShellPath = jest.fn();
+jest.mock('shell-path', () => mockShellPath);
 
 describe('exec', () => {
   const oldPlatform = process.platform;
@@ -68,17 +68,27 @@ describe('exec', () => {
 
       await execModule.maybeFixPath();
 
-      expect(mockFixPath).toHaveBeenCalledTimes(0);
+      expect(mockShellPath).toHaveBeenCalledTimes(0);
     });
 
-    it('does call fix-path on macOS', async () => {
+    it('calls shell-path on macOS', async () => {
       Object.defineProperty(process, 'platform', {
         value: 'darwin',
       });
 
       await execModule.maybeFixPath();
 
-      expect(mockFixPath).toHaveBeenCalledTimes(1);
+      expect(mockShellPath).toHaveBeenCalledTimes(1);
+    });
+
+    it('calls shell-path on Linux', async () => {
+      Object.defineProperty(process, 'platform', {
+        value: 'linux',
+      });
+
+      await execModule.maybeFixPath();
+
+      expect(mockShellPath).toHaveBeenCalledTimes(1);
     });
   });
 });