fix(core): forward process execArgv when using the native runner (#23195)

This commit is contained in:
Leosvel Pérez Espinosa 2024-05-06 16:00:24 +02:00 committed by GitHub
parent 0f71685b2b
commit 1ab1fa5e0b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 37 additions and 6 deletions

View File

@ -150,12 +150,12 @@ export class ChildProcess {
} }
export class RustPseudoTerminal { export class RustPseudoTerminal {
constructor() constructor()
runCommand(command: string, commandDir?: string | undefined | null, jsEnv?: Record<string, string> | undefined | null, quiet?: boolean | undefined | null, tty?: boolean | undefined | null): ChildProcess runCommand(command: string, commandDir?: string | undefined | null, jsEnv?: Record<string, string> | undefined | null, execArgv?: Array<string> | undefined | null, quiet?: boolean | undefined | null, tty?: boolean | undefined | null): ChildProcess
/** /**
* This allows us to run a pseudoterminal with a fake node ipc channel * This allows us to run a pseudoterminal with a fake node ipc channel
* this makes it possible to be backwards compatible with the old implementation * this makes it possible to be backwards compatible with the old implementation
*/ */
fork(id: string, forkScript: string, pseudoIpcPath: string, commandDir: string | undefined | null, jsEnv: Record<string, string> | undefined | null, quiet: boolean): ChildProcess fork(id: string, forkScript: string, pseudoIpcPath: string, commandDir: string | undefined | null, jsEnv: Record<string, string> | undefined | null, execArgv: Array<string> | undefined | null, quiet: boolean): ChildProcess
} }
export class HashPlanner { export class HashPlanner {
constructor(nxJson: NxJson, projectGraph: ExternalObject<ProjectGraph>) constructor(nxJson: NxJson, projectGraph: ExternalObject<ProjectGraph>)

View File

@ -24,11 +24,12 @@ impl RustPseudoTerminal {
command: String, command: String,
command_dir: Option<String>, command_dir: Option<String>,
js_env: Option<HashMap<String, String>>, js_env: Option<HashMap<String, String>>,
exec_argv: Option<Vec<String>>,
quiet: Option<bool>, quiet: Option<bool>,
tty: Option<bool>, tty: Option<bool>,
) -> napi::Result<ChildProcess> { ) -> napi::Result<ChildProcess> {
let pseudo_terminal = create_pseudo_terminal()?; let pseudo_terminal = create_pseudo_terminal()?;
run_command(&pseudo_terminal, command, command_dir, js_env, quiet, tty) run_command(&pseudo_terminal, command, command_dir, js_env, exec_argv, quiet, tty)
} }
/// This allows us to run a pseudoterminal with a fake node ipc channel /// This allows us to run a pseudoterminal with a fake node ipc channel
@ -41,6 +42,7 @@ impl RustPseudoTerminal {
pseudo_ipc_path: String, pseudo_ipc_path: String,
command_dir: Option<String>, command_dir: Option<String>,
js_env: Option<HashMap<String, String>>, js_env: Option<HashMap<String, String>>,
exec_argv: Option<Vec<String>>,
quiet: bool, quiet: bool,
) -> napi::Result<ChildProcess> { ) -> napi::Result<ChildProcess> {
let command = format!( let command = format!(
@ -51,6 +53,6 @@ impl RustPseudoTerminal {
); );
trace!("nx_fork command: {}", &command); trace!("nx_fork command: {}", &command);
self.run_command(command, command_dir, js_env, Some(quiet), Some(true)) self.run_command(command, command_dir, js_env, exec_argv, Some(quiet), Some(true))
} }
} }

View File

@ -29,6 +29,7 @@ impl RustPseudoTerminal {
command: String, command: String,
command_dir: Option<String>, command_dir: Option<String>,
js_env: Option<HashMap<String, String>>, js_env: Option<HashMap<String, String>>,
exec_argv: Option<Vec<String>>,
quiet: Option<bool>, quiet: Option<bool>,
tty: Option<bool>, tty: Option<bool>,
) -> napi::Result<ChildProcess> { ) -> napi::Result<ChildProcess> {
@ -37,6 +38,7 @@ impl RustPseudoTerminal {
command, command,
command_dir, command_dir,
js_env, js_env,
exec_argv,
quiet, quiet,
tty, tty,
) )
@ -52,6 +54,7 @@ impl RustPseudoTerminal {
pseudo_ipc_path: String, pseudo_ipc_path: String,
command_dir: Option<String>, command_dir: Option<String>,
js_env: Option<HashMap<String, String>>, js_env: Option<HashMap<String, String>>,
exec_argv: Option<Vec<String>>,
quiet: bool, quiet: bool,
) -> napi::Result<ChildProcess> { ) -> napi::Result<ChildProcess> {
let command = format!( let command = format!(
@ -62,6 +65,6 @@ impl RustPseudoTerminal {
); );
trace!("nx_fork command: {}", &command); trace!("nx_fork command: {}", &command);
self.run_command(command, command_dir, js_env, Some(quiet), Some(true)) self.run_command(command, command_dir, js_env, exec_argv, Some(quiet), Some(true))
} }
} }

View File

@ -117,6 +117,7 @@ pub fn run_command(
command: String, command: String,
command_dir: Option<String>, command_dir: Option<String>,
js_env: Option<HashMap<String, String>>, js_env: Option<HashMap<String, String>>,
exec_argv: Option<Vec<String>>,
quiet: Option<bool>, quiet: Option<bool>,
tty: Option<bool>, tty: Option<bool>,
) -> napi::Result<ChildProcess> { ) -> napi::Result<ChildProcess> {
@ -138,6 +139,10 @@ pub fn run_command(
} }
} }
if let Some(exec_argv) = exec_argv {
cmd.env("NX_PSEUDO_TERMINAL_EXEC_ARGV", exec_argv.join("|"));
}
let (exit_to_process_tx, exit_to_process_rx) = bounded(1); let (exit_to_process_tx, exit_to_process_rx) = bounded(1);
let mut child = pair.slave.spawn_command(cmd)?; let mut child = pair.slave.spawn_command(cmd)?;
pseudo_terminal.running.store(true, Ordering::SeqCst); pseudo_terminal.running.store(true, Ordering::SeqCst);

View File

@ -7,8 +7,16 @@ const forkId = process.argv[3];
const script = join(__dirname, '../../bin/run-executor.js'); const script = join(__dirname, '../../bin/run-executor.js');
let execArgv: string[] | undefined;
if (process.env['NX_PSEUDO_TERMINAL_EXEC_ARGV']) {
execArgv = process.env['NX_PSEUDO_TERMINAL_EXEC_ARGV'].split('|');
delete process.env['NX_PSEUDO_TERMINAL_EXEC_ARGV'];
}
const childProcess = fork(script, { const childProcess = fork(script, {
stdio: ['inherit', 'inherit', 'inherit', 'ipc'], stdio: ['inherit', 'inherit', 'inherit', 'ipc'],
env: process.env,
execArgv,
}); });
const pseudoIPC = new PseudoIPCClient(pseudoIPCPath); const pseudoIPC = new PseudoIPCClient(pseudoIPCPath);

View File

@ -220,6 +220,7 @@ export class ForkedProcessTaskRunner {
const childId = task.id; const childId = task.id;
const p = await this.pseudoTerminal.fork(childId, forkScript, { const p = await this.pseudoTerminal.fork(childId, forkScript, {
cwd: process.cwd(), cwd: process.cwd(),
execArgv: process.execArgv,
jsEnv: env, jsEnv: env,
quiet: !streamOutput, quiet: !streamOutput,
}); });

View File

@ -41,18 +41,27 @@ export class PseudoTerminal {
command: string, command: string,
{ {
cwd, cwd,
execArgv,
jsEnv, jsEnv,
quiet, quiet,
tty, tty,
}: { }: {
cwd?: string; cwd?: string;
execArgv?: string[];
jsEnv?: Record<string, string>; jsEnv?: Record<string, string>;
quiet?: boolean; quiet?: boolean;
tty?: boolean; tty?: boolean;
} = {} } = {}
) { ) {
return new PseudoTtyProcess( return new PseudoTtyProcess(
this.rustPseudoTerminal.runCommand(command, cwd, jsEnv, quiet, tty) this.rustPseudoTerminal.runCommand(
command,
cwd,
jsEnv,
execArgv,
quiet,
tty
)
); );
} }
@ -61,10 +70,12 @@ export class PseudoTerminal {
script: string, script: string,
{ {
cwd, cwd,
execArgv,
jsEnv, jsEnv,
quiet, quiet,
}: { }: {
cwd?: string; cwd?: string;
execArgv?: string[];
jsEnv?: Record<string, string>; jsEnv?: Record<string, string>;
quiet?: boolean; quiet?: boolean;
} }
@ -79,6 +90,7 @@ export class PseudoTerminal {
this.pseudoIPCPath, this.pseudoIPCPath,
cwd, cwd,
jsEnv, jsEnv,
execArgv,
quiet quiet
), ),
id, id,