fix(server): harden sherpa native loading and centralize runtime env
This commit is contained in:
@@ -3,6 +3,7 @@ import { closeSync, existsSync, openSync, readFileSync, rmSync } from 'node:fs'
|
||||
import { createRequire } from 'node:module'
|
||||
import path from 'node:path'
|
||||
import { loadConfig, resolvePaseoHome } from '@getpaseo/server'
|
||||
import { applySherpaLoaderEnv } from './sherpa-env.js'
|
||||
|
||||
export interface DaemonStartOptions {
|
||||
port?: string
|
||||
@@ -280,6 +281,7 @@ export async function startLocalDaemonDetached(
|
||||
}
|
||||
|
||||
const childEnv: NodeJS.ProcessEnv = { ...process.env }
|
||||
applySherpaLoaderEnv(childEnv)
|
||||
if (options.home) {
|
||||
childEnv.PASEO_HOME = options.home
|
||||
}
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
import { spawnSync } from 'node:child_process'
|
||||
import {
|
||||
applySherpaLoaderEnv as applySherpaLoaderEnvFromServer,
|
||||
resolveSherpaLoaderEnv,
|
||||
} from '@getpaseo/server'
|
||||
|
||||
const SHERPA_ENV_BOOTSTRAPPED = 'PASEO_SHERPA_ENV_BOOTSTRAPPED'
|
||||
|
||||
export function applySherpaLoaderEnv(env: NodeJS.ProcessEnv): {
|
||||
changed: boolean
|
||||
key: 'LD_LIBRARY_PATH' | 'DYLD_LIBRARY_PATH' | 'PATH' | null
|
||||
libDir: string | null
|
||||
} {
|
||||
const result = applySherpaLoaderEnvFromServer(env)
|
||||
return {
|
||||
changed: result.changed,
|
||||
key: result.key,
|
||||
libDir: result.libDir,
|
||||
}
|
||||
}
|
||||
|
||||
export function reexecWithSherpaLoaderEnvIfNeeded(): never | void {
|
||||
const resolved = resolveSherpaLoaderEnv()
|
||||
if (!resolved) {
|
||||
return
|
||||
}
|
||||
if (process.env[SHERPA_ENV_BOOTSTRAPPED] === '1') {
|
||||
return
|
||||
}
|
||||
|
||||
const envCopy: NodeJS.ProcessEnv = { ...process.env }
|
||||
const applied = applySherpaLoaderEnvFromServer(envCopy)
|
||||
const updatedValue = applied.key ? envCopy[applied.key] ?? '' : ''
|
||||
if (applied.key && updatedValue === (process.env[applied.key] ?? '')) {
|
||||
return
|
||||
}
|
||||
|
||||
const env: NodeJS.ProcessEnv = {
|
||||
...envCopy,
|
||||
[SHERPA_ENV_BOOTSTRAPPED]: '1',
|
||||
}
|
||||
|
||||
const result = spawnSync(process.execPath, process.argv.slice(1), {
|
||||
env,
|
||||
stdio: 'inherit',
|
||||
})
|
||||
|
||||
if (result.error) {
|
||||
throw result.error
|
||||
}
|
||||
|
||||
process.exit(result.status ?? 1)
|
||||
}
|
||||
@@ -12,6 +12,7 @@ import {
|
||||
startLocalDaemonDetached,
|
||||
type DaemonStartOptions as StartOptions,
|
||||
} from './local-daemon.js'
|
||||
import { reexecWithSherpaLoaderEnvIfNeeded } from './sherpa-env.js'
|
||||
import { getErrorMessage } from '../../utils/errors.js'
|
||||
|
||||
export type { DaemonStartOptions as StartOptions } from './local-daemon.js'
|
||||
@@ -79,6 +80,8 @@ export async function runStart(options: StartOptions): Promise<void> {
|
||||
return
|
||||
}
|
||||
|
||||
reexecWithSherpaLoaderEnvIfNeeded()
|
||||
|
||||
if (options.home) {
|
||||
process.env.PASEO_HOME = options.home
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user