Does Theia or Electron reorder the process.env.PATH?

Hello:

I’m trying to add some integrated tools such as cmake to my app. I added the bin path of my tools to the process.env.PATH by adding the following code to the end of theia-electron-main.js (I tested it with Theia-blueprint 1.20.0):

// ...

console.log(process.env.PATH);
process.env.PATH = `/test/bin:${process.env.PATH}`;
console.log(process.env.PATH);

// Handover to the auto-generated electron application handler.
require('../src-gen/frontend/electron-main.js')

I added /test/bin before ${process.env.PATH} because I want to use my integrated cmake, instead of the /usr/local/bin/cmake under the system path.

And it works when I start the electron app by running yarn electron start(you can see the code snippet or screen shot):

mac@Titan theia-blueprint-master % yarn electron start
yarn run v1.22.10
$ yarn --cwd applications/electron start
$ electron scripts/theia-electron-main.js
/var/folders/0g/2qt7mmyn00l8xrhbw4p0rjyc0000gn/T/yarn--1643508123977-0.1000540058537931:/Users/mac/Downloads/theia-blueprint-master/applications/electron/node_modules/.bin:/Users/mac/.config/yarn/link/node_modules/.bin:/Users/mac/Downloads/theia-blueprint-master/node_modules/.bin:/usr/local/libexec/lib/node_modules/npm/bin/node-gyp-bin:/usr/local/lib/node_modules/npm/bin/node-gyp-bin:/usr/local/bin/node_modules/npm/bin/node-gyp-bin:/var/folders/0g/2qt7mmyn00l8xrhbw4p0rjyc0000gn/T/yarn--1643508123702-0.3603632870707387:/Users/mac/Downloads/theia-blueprint-master/node_modules/.bin:/Users/mac/.config/yarn/link/node_modules/.bin:/Users/mac/Downloads/theia-blueprint-master/node_modules/.bin:/usr/local/libexec/lib/node_modules/npm/bin/node-gyp-bin:/usr/local/lib/node_modules/npm/bin/node-gyp-bin:/usr/local/bin/node_modules/npm/bin/node-gyp-bin:/Users/mac/.rvm/gems/ruby-3.0.0/bin:/Users/mac/.rvm/gems/ruby-3.0.0@global/bin:/Users/mac/.rvm/rubies/ruby-3.0.0/bin:/usr/local/opt/ruby/bin:/Users/mac/opt/anaconda3/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Applications/VMware Fusion.app/Contents/Public:/usr/local/share/dotnet:~/.dotnet/tools:/Library/Apple/usr/bin:/Library/Frameworks/Mono.framework/Versions/Current/Commands:/Users/mac/.rvm/bin
/test/bin:/var/folders/0g/2qt7mmyn00l8xrhbw4p0rjyc0000gn/T/yarn--1643508123977-0.1000540058537931:/Users/mac/Downloads/theia-blueprint-master/applications/electron/node_modules/.bin:/Users/mac/.config/yarn/link/node_modules/.bin:/Users/mac/Downloads/theia-blueprint-master/node_modules/.bin:/usr/local/libexec/lib/node_modules/npm/bin/node-gyp-bin:/usr/local/lib/node_modules/npm/bin/node-gyp-bin:/usr/local/bin/node_modules/npm/bin/node-gyp-bin:/var/folders/0g/2qt7mmyn00l8xrhbw4p0rjyc0000gn/T/yarn--1643508123702-0.3603632870707387:/Users/mac/Downloads/theia-blueprint-master/node_modules/.bin:/Users/mac/.config/yarn/link/node_modules/.bin:/Users/mac/Downloads/theia-blueprint-master/node_modules/.bin:/usr/local/libexec/lib/node_modules/npm/bin/node-gyp-bin:/usr/local/lib/node_modules/npm/bin/node-gyp-bin:/usr/local/bin/node_modules/npm/bin/node-gyp-bin:/Users/mac/.rvm/gems/ruby-3.0.0/bin:/Users/mac/.rvm/gems/ruby-3.0.0@global/bin:/Users/mac/.rvm/rubies/ruby-3.0.0/bin:/usr/local/opt/ruby/bin:/Users/mac/opt/anaconda3/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Applications/VMware Fusion.app/Contents/Public:/usr/local/share/dotnet:~/.dotnet/tools:/Library/Apple/usr/bin:/Library/Frameworks/Mono.framework/Versions/Current/Commands:/Users/mac/.rvm/bin
Configuring to accept webviews on '^.+\.webview\..+$' hostname.

But when I tried to echo the $PATH in my app terminal, the $PATH has been reordered:

mac@Titan cpp_test % echo $PATH
/Users/mac/.rvm/gems/ruby-3.0.0/bin:/Users/mac/.rvm/gems/ruby-3.0.0@global/bin:/Users/mac/.rvm/rubies/ruby-3.0.0/bin:/usr/local/opt/ruby/bin:/Users/mac/opt/anaconda3/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Applications/VMware Fusion.app/Contents/Public:/usr/local/share/dotnet:~/.dotnet/tools:/Library/Apple/usr/bin:/Library/Frameworks/Mono.framework/Versions/Current/Commands:/Users/mac/.rvm/gems/ruby-3.0.0/bin:/Users/mac/.rvm/gems/ruby-3.0.0@global/bin:/Users/mac/.rvm/rubies/ruby-3.0.0/bin:/usr/local/opt/ruby/bin:/Users/mac/opt/anaconda3/bin:/Users/mac/Downloads/theia-blueprint-master/applications/electron/node_modules/.bin:/Users/mac/Downloads/theia-blueprint-master/applications/node_modules/.bin:/Users/mac/Downloads/theia-blueprint-master/node_modules/.bin:/Users/mac/Downloads/node_modules/.bin:/Users/mac/node_modules/.bin:/Users/node_modules/.bin:/node_modules/.bin:/Users/mac/Downloads/theia-blueprint-master/node_modules/electron/dist/Electron.app/Contents/MacOS:/test/bin:/var/folders/0g/2qt7mmyn00l8xrhbw4p0rjyc0000gn/T/yarn--1643508123977-0.1000540058537931:/Users/mac/.config/yarn/link/node_modules/.bin:/usr/local/libexec/lib/node_modules/npm/bin/node-gyp-bin:/usr/local/lib/node_modules/npm/bin/node-gyp-bin:/usr/local/bin/node_modules/npm/bin/node-gyp-bin:/var/folders/0g/2qt7mmyn00l8xrhbw4p0rjyc0000gn/T/yarn--1643508123702-0.3603632870707387:/Users/mac/.rvm/bin:/Users/mac/.rvm/bin
mac@Titan cpp_test % 

I think this is probably the protection mechanism for Node.js child_process. So I tested it, but it seems to work fine:

mac@Titan theia-blueprint-master % node
Welcome to Node.js v12.16.3.
Type ".help" for more information.
> process.env.PATH
'/Users/mac/.rvm/gems/ruby-3.0.0/bin:/Users/mac/.rvm/gems/ruby-3.0.0@global/bin:/Users/mac/.rvm/rubies/ruby-3.0.0/bin:/usr/local/opt/ruby/bin:/Users/mac/opt/anaconda3/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Applications/VMware Fusion.app/Contents/Public:/usr/local/share/dotnet:~/.dotnet/tools:/Library/Apple/usr/bin:/Library/Frameworks/Mono.framework/Versions/Current/Commands:/Users/mac/.rvm/bin'
> process.env.PATH = `/test/bin:${process.env.PATH}`
'/test/bin:/Users/mac/.rvm/gems/ruby-3.0.0/bin:/Users/mac/.rvm/gems/ruby-3.0.0@global/bin:/Users/mac/.rvm/rubies/ruby-3.0.0/bin:/usr/local/opt/ruby/bin:/Users/mac/opt/anaconda3/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Applications/VMware Fusion.app/Contents/Public:/usr/local/share/dotnet:~/.dotnet/tools:/Library/Apple/usr/bin:/Library/Frameworks/Mono.framework/Versions/Current/Commands:/Users/mac/.rvm/bin'
> process.env.PATH
'/test/bin:/Users/mac/.rvm/gems/ruby-3.0.0/bin:/Users/mac/.rvm/gems/ruby-3.0.0@global/bin:/Users/mac/.rvm/rubies/ruby-3.0.0/bin:/usr/local/opt/ruby/bin:/Users/mac/opt/anaconda3/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Applications/VMware Fusion.app/Contents/Public:/usr/local/share/dotnet:~/.dotnet/tools:/Library/Apple/usr/bin:/Library/Frameworks/Mono.framework/Versions/Current/Commands:/Users/mac/.rvm/bin'
> const cp = require('child_process')
undefined
> cp.exec
cp.exec          cp.execFile      cp.execFileSync  cp.execSync      

> cp.execSync('echo $PATH').toString();
'/test/bin:/Users/mac/.rvm/gems/ruby-3.0.0/bin:/Users/mac/.rvm/gems/ruby-3.0.0@global/bin:/Users/mac/.rvm/rubies/ruby-3.0.0/bin:/usr/local/opt/ruby/bin:/Users/mac/opt/anaconda3/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Applications/VMware Fusion.app/Contents/Public:/usr/local/share/dotnet:~/.dotnet/tools:/Library/Apple/usr/bin:/Library/Frameworks/Mono.framework/Versions/Current/Commands:/Users/mac/.rvm/bin\n'
> 

I’m not sure it’s reordered by Theia or Electron.

Any help? Really appreciate :slight_smile:

Hi @inlann,

in the generated electron-main.js, the fix-path npm package is invoked:

I think this might potentially change the process.env.PATH variable of your electron app. Can you try to remove the fix-path call in the generated code and test whether the issue persists after an application restart?

Hello @msujew,

Thanks for response!

I tried to remove the requirement of fix-path but it does not work:

My /test/bin is still placed behind /usr/local/bin so that I still couldn’t call my integrated tools in the /test/bin first. It seems like the fix-path is not the key :cry: