Repository navigation
[bugfix] desktop: inherit user shell PATH for Host and gate minimal-PATH launches #690
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
FayeSpica
merged 3 commits into
ThinkFlowLab:main
from
FayeSpica:fix/desktop-inherit-user-path
Dec 4, 2026
+414
−5
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,152 @@ | ||
| import { spawn } from 'node:child_process'; | ||
|
|
||
| const START_MARKER = '__OPENDIRECTOR_PATH_START__'; | ||
| const END_MARKER = '__OPENDIRECTOR_PATH_END__'; | ||
| const PROBE_COMMAND = `printf '${START_MARKER}\\n'; /usr/bin/env; printf '${END_MARKER}\\n'`; | ||
|
|
||
| export function extractShellPath(output) { | ||
| const start = output.lastIndexOf(START_MARKER); | ||
| if (start === -1) return null; | ||
| const end = output.indexOf(END_MARKER, start + START_MARKER.length); | ||
| if (end === -1) return null; | ||
| const block = output.slice(start + START_MARKER.length, end); | ||
| for (const line of block.split(/\r?\n/)) { | ||
| if (line.startsWith('PATH=')) return line.slice(5); | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| export function mergePath(segments, { platform = process.platform } = {}) { | ||
| const sep = platform === 'win32' ? ';' : ':'; | ||
| const seen = new Set(); | ||
| const entries = []; | ||
| for (const segment of segments) { | ||
| if (typeof segment !== 'string' || !segment) continue; | ||
| for (const raw of segment.split(sep)) { | ||
| if (!raw) continue; | ||
| const key = platform === 'win32' ? raw.toLowerCase() : raw; | ||
| if (seen.has(key)) continue; | ||
| seen.add(key); | ||
| entries.push(raw); | ||
| } | ||
| } | ||
| return entries.join(sep); | ||
| } | ||
|
|
||
| function candidateShells(platform, env) { | ||
| const fallback = platform === 'darwin' ? '/bin/zsh' : '/bin/sh'; | ||
| const configured = env?.SHELL; | ||
| if (typeof configured === 'string' && configured.startsWith('/') && configured !== fallback) { | ||
| return [configured, fallback]; | ||
| } | ||
| return [fallback]; | ||
| } | ||
|
|
||
| function runShellProbe(shell, args, env, timeoutMs) { | ||
| return new Promise((resolve, reject) => { | ||
| const child = spawn(shell, args, { | ||
| env: { ...env, TERM: 'dumb' }, | ||
| stdio: ['ignore', 'pipe', 'ignore'], | ||
| detached: true, | ||
| }); | ||
| let output = ''; | ||
| let settled = false; | ||
| const finish = (error, value) => { | ||
| if (settled) return; | ||
| settled = true; | ||
| clearTimeout(timer); | ||
| if (error) reject(error); | ||
| else resolve(value); | ||
| }; | ||
| const killGroup = () => { | ||
| if (child.pid) { | ||
| try { | ||
| process.kill(-child.pid, 'SIGKILL'); | ||
| return; | ||
| } catch {} | ||
| } | ||
| try { child.kill('SIGKILL'); } catch {} | ||
| }; | ||
| const timer = setTimeout(() => { | ||
| killGroup(); | ||
| finish(new Error(`Shell PATH probe timed out after ${timeoutMs}ms`)); | ||
| }, timeoutMs); | ||
| child.stdout.setEncoding('utf8'); | ||
| child.stdout.on('data', chunk => { | ||
| output += chunk; | ||
| if (output.length > 1024 * 1024) { | ||
| killGroup(); | ||
| finish(new Error('Shell PATH probe exceeded output limit')); | ||
| } | ||
| }); | ||
| child.once('error', error => finish(error)); | ||
| child.once('close', code => { | ||
| if (code !== 0) finish(new Error(`Shell PATH probe exited (${code})`)); | ||
| else finish(null, output); | ||
| }); | ||
| }); | ||
| } | ||
|
|
||
| export async function resolveUserShellPath({ | ||
| platform = process.platform, | ||
| env = process.env, | ||
| probe = runShellProbe, | ||
| timeoutMs = 3000, | ||
| log, | ||
| } = {}) { | ||
| if (platform === 'win32') return null; | ||
| let lastError; | ||
| for (const shell of candidateShells(platform, env)) { | ||
| for (const args of [['-ilc', PROBE_COMMAND], ['-lc', PROBE_COMMAND]]) { | ||
| try { | ||
| const output = await probe(shell, args, env, timeoutMs); | ||
| const resolved = extractShellPath(output); | ||
| if (resolved) return resolved; | ||
| lastError = new Error('Shell PATH probe returned no PATH entry'); | ||
| } catch (error) { | ||
| lastError = error; | ||
| } | ||
| } | ||
| } | ||
| if (lastError) log?.write(`User shell PATH probe failed: ${lastError.message}\n`); | ||
| return null; | ||
| } | ||
|
|
||
| export function buildHostEnv({ | ||
| runtimeBin, | ||
| workspace, | ||
| platform = process.platform, | ||
| env = process.env, | ||
| userPath = null, | ||
| } = {}) { | ||
| const result = {}; | ||
| const sep = platform === 'win32' ? ';' : ':'; | ||
| let existingPath = ''; | ||
| let pathKey = 'PATH'; | ||
| for (const [key, value] of Object.entries(env ?? {})) { | ||
| if (platform === 'win32' ? key.toLowerCase() === 'path' : key === 'PATH') { | ||
| if (typeof value === 'string' && value) { | ||
| existingPath = existingPath ? `${existingPath}${sep}${value}` : value; | ||
| } | ||
| if (platform === 'win32') pathKey = key; | ||
| continue; | ||
| } | ||
| result[key] = value; | ||
| } | ||
| result.OPENDIRECTOR_ROOT = workspace; | ||
| result[pathKey] = mergePath([runtimeBin, userPath, existingPath], { platform }); | ||
| return result; | ||
| } | ||
|
|
||
| export async function resolveHostEnv({ | ||
| runtimeBin, | ||
| workspace, | ||
| platform = process.platform, | ||
| env = process.env, | ||
| probe = runShellProbe, | ||
| timeoutMs = 3000, | ||
| log, | ||
| } = {}) { | ||
| const userPath = await resolveUserShellPath({ platform, env, probe, timeoutMs, log }); | ||
| return buildHostEnv({ runtimeBin, workspace, platform, env, userPath }); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里的 3 秒是每次 probe 的上限。自定义 shell 和 fallback 各试两种模式,Host 启动前最坏会阻塞约 12 秒。建议共用一个总 deadline,到点直接走原 PATH,并加一个连续超时仍能及时启动的测试。 (Use one total startup deadline.)