fix(desktop): keep PM store dirs first on PATH for Hermes's own children
The login-shell PATH merge (shell-path.ts) writes the user's interactive PATH into process.env ahead of the inherited entries. When Desktop is started by `hermes desktop`, the inherited PATH is where the PM store's node/npm/uv dirs sit, so every Hermes-toolchain child built from process.env (the `hermes serve` backend, backend probes, the source/posix update hand-offs via sourceUpdateEnvironment, the bootstrap installer) resolved the user's nvm/Homebrew/~/.local/bin copies first. buildDesktopBackendEnv and the bootstrap installer env now move PATH entries under the store root (HERMES_RUNTIME_DIR, else <hermes home>/tools, matching pm.environments.store_root) to the front, other entries in their existing order. process.env itself is unchanged, so the embedded terminal and external terminal keep the user's own PATH order.
This commit is contained in:
@@ -13,6 +13,7 @@ import {
|
||||
POSIX_SANE_PATH_ENTRIES,
|
||||
profileBackendParentEnv
|
||||
} from './backend-env'
|
||||
import { applyLoginShellPath } from './shell-path'
|
||||
|
||||
test('backend env scrubs PYTHONPATH and PYTHONHOME', () => {
|
||||
const env = buildDesktopBackendEnv({
|
||||
@@ -43,6 +44,55 @@ test('POSIX backend PATH keeps the inherited PATH first and appends missing sane
|
||||
}
|
||||
})
|
||||
|
||||
test('backend runs the store toolchain even after the login-shell PATH is merged in front of it', async () => {
|
||||
// `hermes desktop` hands Electron a PATH with the PM store first; the
|
||||
// login-shell merge then puts nvm/Homebrew ahead of it in process.env.
|
||||
const env: Record<string, string> = {
|
||||
PATH: '/Users/u/.hermes/tools/node-26.7.0-darwin-arm64/bin:/Users/u/.hermes/tools/uv-0.12.3-darwin-arm64:/usr/bin:/bin'
|
||||
}
|
||||
|
||||
const loginPath = '/Users/u/.nvm/versions/node/v20.0.0/bin:/opt/homebrew/bin:/usr/bin'
|
||||
|
||||
const execFileFn = (_file, _args, _options, callback) => {
|
||||
queueMicrotask(() => callback(null, `__HERMES_LOGIN_PATH_START__${loginPath}__HERMES_LOGIN_PATH_END__`, ''))
|
||||
|
||||
return { stdin: { end() {} } }
|
||||
}
|
||||
|
||||
await applyLoginShellPath({ env, platform: 'darwin', execFileFn })
|
||||
assert.equal(env.PATH.split(':')[0], '/Users/u/.nvm/versions/node/v20.0.0/bin', 'user-facing env keeps login order')
|
||||
|
||||
const backend = buildDesktopBackendEnv({ currentEnv: env, platform: 'darwin', homedir: '/Users/u' })
|
||||
|
||||
assert.deepEqual(backend.PATH.split(':').slice(0, 5), [
|
||||
'/Users/u/.hermes/tools/node-26.7.0-darwin-arm64/bin',
|
||||
'/Users/u/.hermes/tools/uv-0.12.3-darwin-arm64',
|
||||
'/Users/u/.nvm/versions/node/v20.0.0/bin',
|
||||
'/opt/homebrew/bin',
|
||||
'/usr/bin'
|
||||
])
|
||||
})
|
||||
|
||||
test('HERMES_RUNTIME_DIR names the store; look-alike prefixes are not Hermes-owned', () => {
|
||||
const store = '/Applications/Hermes.app/Contents/Resources/agent-payload/tools'
|
||||
|
||||
const backend = buildDesktopBackendEnv({
|
||||
currentEnv: {
|
||||
HERMES_RUNTIME_DIR: store,
|
||||
PATH: `/opt/homebrew/bin:/Users/u/.hermes/tools-old/bin:${store}/npm-12.0.2-darwin-arm64/bin:/usr/bin`
|
||||
},
|
||||
platform: 'darwin',
|
||||
homedir: '/Users/u'
|
||||
})
|
||||
|
||||
assert.deepEqual(backend.PATH.split(':').slice(0, 4), [
|
||||
`${store}/npm-12.0.2-darwin-arm64/bin`,
|
||||
'/opt/homebrew/bin',
|
||||
'/Users/u/.hermes/tools-old/bin',
|
||||
'/usr/bin'
|
||||
])
|
||||
})
|
||||
|
||||
test('Windows PATH casing and delimiter are preserved without POSIX sane entries', () => {
|
||||
const env = buildDesktopBackendEnv({
|
||||
currentEnv: { Path: 'C:\\Windows\\System32;C:\\Windows' },
|
||||
|
||||
@@ -2,6 +2,8 @@ import fs from 'node:fs'
|
||||
import os from 'node:os'
|
||||
import path from 'node:path'
|
||||
|
||||
import { resolveDesktopHermesHome } from './data-paths'
|
||||
|
||||
// macOS apps launched from Finder/Dock inherit only /usr/bin:/bin:/usr/sbin:/sbin,
|
||||
// which misses Homebrew and user-installed CLI tools (codex, git credential
|
||||
// helpers). Hermes' own managed tools need no PATH help — the backend composes
|
||||
@@ -181,17 +183,52 @@ function profileBackendParentEnv({
|
||||
return env
|
||||
}
|
||||
|
||||
/**
|
||||
* PATH with the entries under the PM store (HERMES_RUNTIME_DIR, else
|
||||
* <hermes home>/tools, as pm.environments.store_root resolves it) moved to the
|
||||
* front, every other entry kept in order. Hermes's own children must run the
|
||||
* store's uv/node/npm, but shell-path.ts puts the user's login-shell entries
|
||||
* (nvm, Homebrew, ~/.local/bin) ahead of the inherited PATH, which is where
|
||||
* `hermes desktop` put the store dirs.
|
||||
*/
|
||||
function storeFirstPath(
|
||||
pathValue: string,
|
||||
{ currentEnv = process.env, platform = process.platform, homedir = os.homedir() }: any = {}
|
||||
) {
|
||||
const pathModule = pathModuleForPlatform(platform)
|
||||
const delimiter = delimiterForPlatform(platform)
|
||||
const hermesHome = resolveDesktopHermesHome({ home: homedir, env: currentEnv, platform })
|
||||
const roots = [currentEnv?.HERMES_RUNTIME_DIR, pathModule.join(hermesHome, 'tools')].filter(Boolean)
|
||||
|
||||
const owned = (entry: string) =>
|
||||
roots.some(root => {
|
||||
const relative = pathModule.relative(pathModule.resolve(root), pathModule.resolve(entry))
|
||||
|
||||
return !relative.startsWith('..') && !pathModule.isAbsolute(relative)
|
||||
})
|
||||
|
||||
const entries = String(pathValue || '').split(delimiter)
|
||||
|
||||
return appendUniquePathEntries([entries.filter(entry => entry && owned(entry)), entries], { delimiter })
|
||||
}
|
||||
|
||||
/**
|
||||
* The environment for the spawned Python backend. Electron knows ONE thing:
|
||||
* where the interpreter is (by convention). Everything else — managed tool
|
||||
* PATHs, browser paths, node — is composed in-process by pm when the backend
|
||||
* spawns tools. PYTHONPATH/PYTHONHOME are scrubbed so an inherited value
|
||||
* can't make the backend import modules from another checkout.
|
||||
* can't make the backend import modules from another checkout. Store dirs
|
||||
* already on the inherited PATH stay first (storeFirstPath).
|
||||
*/
|
||||
function buildDesktopBackendEnv({ currentEnv = process.env, platform = process.platform }: any = {}) {
|
||||
function buildDesktopBackendEnv({
|
||||
currentEnv = process.env,
|
||||
platform = process.platform,
|
||||
homedir = os.homedir()
|
||||
}: any = {}) {
|
||||
const delimiter = delimiterForPlatform(platform)
|
||||
const key = pathEnvKey(currentEnv, platform)
|
||||
const saneEntries = platform === 'win32' ? [] : POSIX_SANE_PATH_ENTRIES
|
||||
const inherited = storeFirstPath(currentEnv?.[key] || '', { currentEnv, platform, homedir })
|
||||
|
||||
return {
|
||||
PYTHONPATH: '',
|
||||
@@ -203,7 +240,7 @@ function buildDesktopBackendEnv({ currentEnv = process.env, platform = process.p
|
||||
// pre-bootstrap tracebacks) still decodes with the locale default without
|
||||
// this. User's explicit setting wins. Re-port of PR #56499 (echoriver89).
|
||||
PYTHONUTF8: currentEnv?.PYTHONUTF8 ?? '1',
|
||||
[key]: appendUniquePathEntries([currentEnv?.[key] || '', saneEntries], { delimiter })
|
||||
[key]: appendUniquePathEntries([inherited, saneEntries], { delimiter })
|
||||
}
|
||||
}
|
||||
|
||||
@@ -214,5 +251,6 @@ export {
|
||||
normalizeHermesHomeRoot,
|
||||
pathEnvKey,
|
||||
POSIX_SANE_PATH_ENTRIES,
|
||||
profileBackendParentEnv
|
||||
profileBackendParentEnv,
|
||||
storeFirstPath
|
||||
}
|
||||
|
||||
@@ -42,6 +42,7 @@ import path from 'node:path'
|
||||
// with no tsconfig path resolution (see scripts/bundle-electron-main.mjs).
|
||||
import { stripAnsi } from '../../shared/src/ansi'
|
||||
|
||||
import { pathEnvKey, storeFirstPath } from './backend-env'
|
||||
import { hiddenWindowsChildOptions } from './windows-child-options'
|
||||
|
||||
const IS_WINDOWS = process.platform === 'win32'
|
||||
@@ -479,6 +480,18 @@ function cleanInstallerLogLine(raw: string): string {
|
||||
return frames.length ? frames[frames.length - 1] : ''
|
||||
}
|
||||
|
||||
// The installer drives Hermes's own toolchain (install.sh takes a uv from PATH
|
||||
// when it is new enough), so store dirs already on PATH stay ahead of the
|
||||
// login-shell entries shell-path.ts merged in front of them.
|
||||
function installerEnv(hermesHome) {
|
||||
const env = { ...process.env, HERMES_HOME: hermesHome || process.env.HERMES_HOME || '' }
|
||||
const key = pathEnvKey(env)
|
||||
|
||||
env[key] = storeFirstPath(env[key] || '', { currentEnv: env })
|
||||
|
||||
return env
|
||||
}
|
||||
|
||||
function spawnPowerShell(scriptPath, args, { emit, stageName, abortSignal, hermesHome }: any = {}) {
|
||||
return new Promise<any>((resolve, reject) => {
|
||||
const ps = process.platform === 'win32' ? resolveWindowsPowerShell() : 'pwsh'
|
||||
@@ -489,12 +502,9 @@ function spawnPowerShell(scriptPath, args, { emit, stageName, abortSignal, herme
|
||||
fullArgs,
|
||||
hiddenWindowsChildOptions({
|
||||
stdio: ['ignore', 'pipe', 'pipe'],
|
||||
env: {
|
||||
...process.env,
|
||||
// Pass HERMES_HOME through so install.ps1 respects the caller's
|
||||
// choice rather than re-computing the default.
|
||||
HERMES_HOME: hermesHome || process.env.HERMES_HOME || ''
|
||||
}
|
||||
// Pass HERMES_HOME through so install.ps1 respects the caller's
|
||||
// choice rather than re-computing the default.
|
||||
env: installerEnv(hermesHome)
|
||||
})
|
||||
)
|
||||
|
||||
@@ -590,10 +600,7 @@ function spawnBash(scriptPath, args, { emit, stageName, abortSignal, hermesHome
|
||||
return new Promise<any>((resolve, reject) => {
|
||||
const child = spawn('bash', [scriptPath, ...args], {
|
||||
stdio: ['ignore', 'pipe', 'pipe'],
|
||||
env: {
|
||||
...process.env,
|
||||
HERMES_HOME: hermesHome || process.env.HERMES_HOME || ''
|
||||
}
|
||||
env: installerEnv(hermesHome)
|
||||
})
|
||||
|
||||
let stdout = ''
|
||||
|
||||
Reference in New Issue
Block a user