Skip to content

Commit

Permalink
fix(core): handle spaces when launching nxFork
Browse files Browse the repository at this point in the history
  • Loading branch information
Cammisuli committed Feb 2, 2024
1 parent cb3da6c commit c693cbf
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 6 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions packages/nx/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ swc_ecma_visit = "0.93.0"
swc_ecma_ast = "0.107.0"
crossterm = "0.27.0"

[target.'cfg(windows)'.dependencies]
winapi = { version = "0.3", features = ["fileapi"] }

[lib]
crate-type = ['cdylib']

Expand Down
50 changes: 44 additions & 6 deletions packages/nx/src/native/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use napi::threadsafe_function::ThreadsafeFunction;
use napi::threadsafe_function::ThreadsafeFunctionCallMode::NonBlocking;
use napi::{Env, JsFunction};
use portable_pty::{ChildKiller, CommandBuilder, NativePtySystem, PtySize, PtySystem};
use tracing::trace;

#[cfg(target_os = "windows")]
static CURSOR_POSITION: std::sync::OnceLock<regex::Regex> = std::sync::OnceLock::new();
Expand Down Expand Up @@ -234,10 +235,47 @@ pub fn nx_fork(
js_env: Option<HashMap<String, String>>,
quiet: bool,
) -> napi::Result<ChildProcess> {
run_command(
format!("node {} {} {}", fork_script, psuedo_ipc_path, id),
command_dir,
js_env,
Some(quiet),
)
let command = format!(
"node {} {} {}",
handle_path_space(fork_script),
psuedo_ipc_path,
id
);

trace!("nx_fork command: {}", &command);
run_command(command, command_dir, js_env, Some(quiet))
}

#[cfg(target_os = "windows")]
pub fn handle_path_space(path: String) -> String {
use std::os::windows::ffi::OsStrExt;
use std::{ffi::OsString, os::windows::ffi::OsStringExt};

use winapi::um::fileapi::GetShortPathNameW;
let wide: Vec<u16> = std::path::PathBuf::from(&path)
.as_os_str()
.encode_wide()
.chain(Some(0))
.collect();
let mut buffer: Vec<u16> = vec![0; wide.len() * 2];
let result =
unsafe { GetShortPathNameW(wide.as_ptr(), buffer.as_mut_ptr(), buffer.len() as u32) };
if result == 0 {
path
} else {
let len = buffer.iter().position(|&x| x == 0).unwrap();
let short_path: String = OsString::from_wide(&buffer[..len])
.to_string_lossy()
.into_owned();
short_path
}
}

#[cfg(not(target_os = "windows"))]
fn handle_path_space(path: String) -> String {
if path.contains(' ') {
format!("'{}'", path)
} else {
path
}
}

0 comments on commit c693cbf

Please sign in to comment.