Skip to main content

nu_command/system/
run_external.rs

1use itertools::Itertools;
2use nu_cmd_base::hook::eval_hook;
3use nu_engine::{command_prelude::*, env_to_strings};
4use nu_path::{AbsolutePath, dots::expand_ndots_safe, expand_tilde};
5use nu_protocol::{
6    ByteStream, NuGlob, OutDest, Signals, UseAnsiColoring, did_you_mean,
7    process::{ChildProcess, PostWaitCallback},
8    shell_error::io::IoError,
9};
10use nu_system::{ForegroundChild, kill_by_pid};
11use nu_utils::IgnoreCaseExt;
12use pathdiff::diff_paths;
13#[cfg(windows)]
14use std::os::windows::process::CommandExt;
15use std::{
16    borrow::Cow,
17    ffi::{OsStr, OsString},
18    io::Write,
19    path::{Path, PathBuf},
20    process::Stdio,
21    sync::Arc,
22    thread,
23};
24
25#[derive(Clone)]
26pub struct External;
27
28impl Command for External {
29    fn name(&self) -> &str {
30        "run-external"
31    }
32
33    fn description(&self) -> &str {
34        "Runs external command."
35    }
36
37    fn extra_description(&self) -> &str {
38        "All externals are run with this command, whether you call it directly with `run-external external` or use `external` or `^external`.
39If you create a custom command with this name, that will be used instead."
40    }
41
42    fn signature(&self) -> nu_protocol::Signature {
43        Signature::build(self.name())
44            .input_output_types(vec![(Type::Any, Type::Any)])
45            .rest(
46                "command",
47                SyntaxShape::OneOf(vec![SyntaxShape::GlobPattern, SyntaxShape::Any]),
48                "External command to run, with arguments.",
49            )
50            .category(Category::System)
51    }
52
53    fn run(
54        &self,
55        engine_state: &EngineState,
56        stack: &mut Stack,
57        call: &Call,
58        input: PipelineData,
59    ) -> Result<PipelineData, ShellError> {
60        let cwd = engine_state.cwd(Some(stack))?;
61        let rest = call.rest::<Value>(engine_state, stack, 0)?;
62        let name_args = rest.split_first().map(|(x, y)| (x, y.to_vec()));
63
64        let Some((name, mut call_args)) = name_args else {
65            return Err(ShellError::MissingParameter {
66                param_name: "no command given".into(),
67                span: call.head,
68            });
69        };
70
71        let name_str: Cow<str> = match &name {
72            Value::Glob { val, .. } => Cow::Borrowed(val),
73            Value::String { val, .. } => Cow::Borrowed(val),
74            Value::List { vals, .. } => {
75                let Some((first, args)) = vals.split_first() else {
76                    return Err(ShellError::MissingParameter {
77                        param_name: "external command given as list empty".into(),
78                        span: call.head,
79                    });
80                };
81                // Prepend elements in command list to the list of arguments except the first
82                call_args.splice(0..0, args.to_vec());
83                first.coerce_str()?
84            }
85            _ => Cow::Owned(name.clone().coerce_into_string()?),
86        };
87
88        let expanded_name = match &name {
89            // Expand tilde and ndots on the name if it's a bare string / glob (#13000)
90            Value::Glob { no_expand, .. } if !*no_expand => {
91                expand_ndots_safe(expand_tilde(&*name_str))
92            }
93            _ => Path::new(&*name_str).to_owned(),
94        };
95
96        let paths = nu_engine::env::path_str(engine_state, stack, call.head).unwrap_or_default();
97
98        // On Windows, the user could have run the cmd.exe built-in commands "assoc"
99        // and "ftype" to create a file association for an arbitrary file extension.
100        // They then could have added that extension to the PATHEXT environment variable.
101        // For example, a nushell script with extension ".nu" can be set up with
102        // "assoc .nu=nuscript" and "ftype nuscript=C:\path\to\nu.exe '%1' %*",
103        // and then by adding ".NU" to PATHEXT. In this case we use the which command,
104        // which will find the executable with or without the extension. If "which"
105        // returns true, that means that we've found the script and we believe the
106        // user wants to use the windows association to run the script. The only
107        // easy way to do this is to run cmd.exe with the script as an argument.
108        // File extensions of .COM, .EXE, .BAT, and .CMD are ignored because Windows
109        // can run those files directly. PS1 files are also ignored and that
110        // extension is handled in a separate block below.
111        let pathext_script_in_windows = if cfg!(windows) {
112            if let Some(executable) = which(&expanded_name, &paths, cwd.as_ref()) {
113                let ext = executable
114                    .extension()
115                    .unwrap_or_default()
116                    .to_string_lossy()
117                    .to_uppercase();
118
119                !["COM", "EXE", "BAT", "CMD", "PS1"]
120                    .iter()
121                    .any(|c| *c == ext)
122            } else {
123                false
124            }
125        } else {
126            false
127        };
128
129        // let's make sure it's a .ps1 script, but only on Windows
130        let (potential_powershell_script, path_to_ps1_executable) = if cfg!(windows) {
131            if let Some(executable) = which(&expanded_name, &paths, cwd.as_ref()) {
132                let ext = executable
133                    .extension()
134                    .unwrap_or_default()
135                    .to_string_lossy()
136                    .to_uppercase();
137                (ext == "PS1", Some(executable))
138            } else {
139                (false, None)
140            }
141        } else {
142            (false, None)
143        };
144
145        // Find the absolute path to the executable. On Windows, set the
146        // executable to "cmd.exe" if it's a CMD internal command. If the
147        // command is not found, display a helpful error message.
148        let executable = if cfg!(windows)
149            && (is_cmd_internal_command(&name_str) || pathext_script_in_windows)
150        {
151            PathBuf::from("cmd.exe")
152        } else if cfg!(windows) && potential_powershell_script && path_to_ps1_executable.is_some() {
153            // If we're on Windows and we're trying to run a PowerShell script, we'll use
154            // `powershell.exe` to run it. We shouldn't have to check for powershell.exe because
155            // it's automatically installed on all modern windows systems.
156            PathBuf::from("powershell.exe")
157        } else {
158            // Determine the PATH to be used and then use `which` to find it - though this has no
159            // effect if it's an absolute path already
160            let Some(executable) = which(&expanded_name, &paths, cwd.as_ref()) else {
161                return Err(command_not_found(
162                    &name_str,
163                    call.head,
164                    engine_state,
165                    stack,
166                    &cwd,
167                ));
168            };
169            executable
170        };
171
172        // Create the command.
173        let mut command = std::process::Command::new(&executable);
174
175        // Configure PWD.
176        command.current_dir(cwd);
177
178        // Configure environment variables.
179        let envs = env_to_strings(engine_state, stack)?;
180        command.env_clear();
181        command.envs(envs);
182
183        // Configure args.
184        let args = eval_external_arguments(engine_state, stack, call_args)?;
185        #[cfg(windows)]
186        if is_cmd_internal_command(&name_str) || pathext_script_in_windows {
187            // The /D flag disables execution of AutoRun commands from registry.
188            // The /C flag followed by a command name instructs CMD to execute
189            // that command and quit.
190            command.args(["/D", "/C", &expanded_name.to_string_lossy()]);
191            for arg in &args {
192                command.raw_arg(escape_cmd_argument(arg)?);
193            }
194        } else if potential_powershell_script {
195            command.args([
196                "-File",
197                &path_to_ps1_executable.unwrap_or_default().to_string_lossy(),
198            ]);
199            command.args(args.into_iter().map(|s| s.item));
200        } else {
201            command.args(args.into_iter().map(|s| s.item));
202        }
203        #[cfg(not(windows))]
204        command.args(args.into_iter().map(|s| s.item));
205
206        // Configure stdout and stderr. If both are set to `OutDest::Pipe`,
207        // we'll set up a pipe that merges two streams into one.
208        let stdout = stack.stdout();
209        let stderr = stack.stderr();
210        let merged_stream = if matches!(stdout, OutDest::Pipe) && matches!(stderr, OutDest::Pipe) {
211            let (reader, writer) =
212                os_pipe::pipe().map_err(|err| IoError::new(err, call.head, None))?;
213            command.stdout(
214                writer
215                    .try_clone()
216                    .map_err(|err| IoError::new(err, call.head, None))?,
217            );
218            command.stderr(writer);
219            Some(reader)
220        } else {
221            if engine_state.is_background_job()
222                && matches!(stdout, OutDest::Inherit | OutDest::Print)
223            {
224                command.stdout(Stdio::null());
225            } else {
226                command.stdout(
227                    Stdio::try_from(stdout).map_err(|err| IoError::new(err, call.head, None))?,
228                );
229            }
230
231            if engine_state.is_background_job()
232                && matches!(stderr, OutDest::Inherit | OutDest::Print)
233            {
234                command.stderr(Stdio::null());
235            } else {
236                command.stderr(
237                    Stdio::try_from(stderr).map_err(|err| IoError::new(err, call.head, None))?,
238                );
239            }
240
241            None
242        };
243
244        // Configure stdin. We'll try connecting input to the child process
245        // directly. If that's not possible, we'll set up a pipe and spawn a
246        // thread to copy data into the child process.
247        let data_to_copy_into_stdin = match input {
248            PipelineData::ByteStream(stream, metadata) => match stream.into_stdio() {
249                Ok(stdin) => {
250                    command.stdin(stdin);
251                    None
252                }
253                Err(stream) => {
254                    command.stdin(Stdio::piped());
255                    Some(PipelineData::byte_stream(stream, metadata))
256                }
257            },
258            PipelineData::Empty => {
259                // MCP servers run non-interactively - use null stdin to prevent commands
260                // from hanging when they prompt for passwords or other input.
261                // In the future, this may become a more general option (e.g., no_stdin)
262                // but needs more testing first. See:
263                // https://github.com/nushell/nushell/pull/17161#discussion_r2761243143
264                if engine_state.is_mcp {
265                    command.stdin(Stdio::null());
266                } else {
267                    command.stdin(Stdio::inherit());
268                }
269                None
270            }
271            value => {
272                command.stdin(Stdio::piped());
273                Some(value)
274            }
275        };
276
277        // Log the command we're about to run in case it's useful for debugging purposes.
278        log::trace!("run-external spawning: {command:?}");
279
280        // Spawn the child process. On Unix, also put the child process to
281        // foreground if we're in an interactive session.
282        #[cfg(windows)]
283        let child = ForegroundChild::spawn(command);
284        #[cfg(unix)]
285        let child = ForegroundChild::spawn(
286            command,
287            engine_state.is_interactive,
288            engine_state.is_background_job(),
289            &engine_state.pipeline_externals_state,
290        );
291
292        let mut child = child.map_err(|err| {
293            let context = format!("Could not spawn foreground child: {err}");
294            IoError::new_internal(err, context)
295        })?;
296
297        if let Some(thread_job) = engine_state.current_thread_job()
298            && !thread_job.try_add_pid(child.pid())
299        {
300            kill_by_pid(child.pid().into()).map_err(|err| {
301                ShellError::Io(IoError::new_internal(
302                    err,
303                    "Could not spawn external stdin worker",
304                ))
305            })?;
306        }
307
308        // If we need to copy data into the child process, do it now.
309        if let Some(data) = data_to_copy_into_stdin {
310            let stdin = child.as_mut().stdin.take().expect("stdin is piped");
311            let engine_state = engine_state.clone();
312            let stack = stack.clone();
313            thread::Builder::new()
314                .name("external stdin worker".into())
315                .spawn(move || {
316                    let _ = write_pipeline_data(engine_state, stack, data, stdin);
317                })
318                .map_err(|err| {
319                    IoError::new_with_additional_context(
320                        err,
321                        call.head,
322                        None,
323                        "Could not spawn external stdin worker",
324                    )
325                })?;
326        }
327
328        let child_pid = child.pid();
329
330        // Wrap the output into a `PipelineData::byte_stream`.
331        let child = ChildProcess::new(
332            child,
333            merged_stream,
334            matches!(stderr, OutDest::Pipe),
335            call.head,
336            Some(PostWaitCallback::for_job_control(
337                engine_state,
338                Some(child_pid),
339                executable
340                    .as_path()
341                    .file_name()
342                    .and_then(|it| it.to_str())
343                    .map(|it| it.to_string()),
344            )),
345        )?;
346
347        Ok(PipelineData::byte_stream(
348            ByteStream::child(child, call.head),
349            None,
350        ))
351    }
352
353    fn examples(&self) -> Vec<Example<'_>> {
354        vec![
355            Example {
356                description: "Run an external command",
357                example: r#"run-external "echo" "-n" "hello""#,
358                result: None,
359            },
360            Example {
361                description: "Redirect stdout from an external command into the pipeline",
362                example: r#"run-external "echo" "-n" "hello" | split chars"#,
363                result: None,
364            },
365            Example {
366                description: "Redirect stderr from an external command into the pipeline",
367                example: r#"run-external "nu" "-c" "print -e hello" e>| split chars"#,
368                result: None,
369            },
370        ]
371    }
372}
373
374/// Evaluate all arguments, performing expansions when necessary.
375pub fn eval_external_arguments(
376    engine_state: &EngineState,
377    stack: &mut Stack,
378    call_args: Vec<Value>,
379) -> Result<Vec<Spanned<OsString>>, ShellError> {
380    let cwd = engine_state.cwd(Some(stack))?;
381    let mut args: Vec<Spanned<OsString>> = Vec::with_capacity(call_args.len());
382
383    for arg in call_args {
384        let span = arg.span();
385        match arg {
386            // Expand globs passed to run-external
387            Value::Glob { val, no_expand, .. } if !no_expand => args.extend(
388                expand_glob(
389                    &val,
390                    cwd.as_std_path(),
391                    span,
392                    engine_state.signals().clone(),
393                )?
394                .into_iter()
395                .map(|s| s.into_spanned(span)),
396            ),
397            other => args
398                .push(OsString::from(coerce_into_string(engine_state, other)?).into_spanned(span)),
399        }
400    }
401    Ok(args)
402}
403
404/// Custom `coerce_into_string()`, including globs, since those are often args to `run-external`
405/// as well
406fn coerce_into_string(engine_state: &EngineState, val: Value) -> Result<String, ShellError> {
407    match val {
408        Value::List { .. } => Err(ShellError::CannotPassListToExternal {
409            arg: String::from_utf8_lossy(engine_state.get_span_contents(val.span())).into_owned(),
410            span: val.span(),
411        }),
412        Value::Glob { val, .. } => Ok(val),
413        _ => val.coerce_into_string(),
414    }
415}
416
417/// Performs glob expansion on `arg`. If the expansion found no matches or the pattern
418/// is not a valid glob, then this returns the original string as the expansion result.
419///
420/// Note: This matches the default behavior of Bash, but is known to be
421/// error-prone. We might want to change this behavior in the future.
422fn expand_glob(
423    arg: &str,
424    cwd: &Path,
425    span: Span,
426    signals: Signals,
427) -> Result<Vec<OsString>, ShellError> {
428    // For an argument that isn't a glob, just do the `expand_tilde`
429    // and `expand_ndots` expansion
430    if !nu_glob::is_glob_with_backend(arg) {
431        let path = expand_ndots_safe(expand_tilde(arg));
432        return Ok(vec![path.into()]);
433    }
434
435    // We must use `nu_engine::glob_from` here, in order to ensure we get paths from the correct
436    // dir
437    let glob = NuGlob::Expand(arg.to_owned()).into_spanned(span);
438    if let Ok((prefix, matches)) = nu_engine::glob_from(&glob, cwd, span, None, signals.clone()) {
439        let mut result: Vec<OsString> = vec![];
440
441        for m in matches {
442            signals.check(&span)?;
443            if let Ok(arg) = m {
444                let arg = resolve_globbed_path_to_cwd_relative(arg, prefix.as_ref(), cwd);
445                result.push(arg.into());
446            } else {
447                result.push(arg.into());
448            }
449        }
450
451        // FIXME: do we want to special-case this further? We might accidentally expand when they don't
452        // intend to
453        if result.is_empty() {
454            result.push(arg.into());
455        }
456
457        Ok(result)
458    } else {
459        Ok(vec![arg.into()])
460    }
461}
462
463fn resolve_globbed_path_to_cwd_relative(
464    path: PathBuf,
465    prefix: Option<&PathBuf>,
466    cwd: &Path,
467) -> PathBuf {
468    if let Some(prefix) = prefix {
469        if let Ok(remainder) = path.strip_prefix(prefix) {
470            let new_prefix = if let Some(pfx) = diff_paths(prefix, cwd) {
471                pfx
472            } else {
473                prefix.to_path_buf()
474            };
475            new_prefix.join(remainder)
476        } else {
477            path
478        }
479    } else {
480        path
481    }
482}
483
484/// Write `PipelineData` into `writer`. If `PipelineData` is not binary, it is
485/// first rendered using the `table` command.
486///
487/// Note: Avoid using this function when piping data from an external command to
488/// another external command, because it copies data unnecessarily. Instead,
489/// extract the pipe from the `PipelineData::byte_stream` of the first command
490/// and hand it to the second command directly.
491fn write_pipeline_data(
492    mut engine_state: EngineState,
493    mut stack: Stack,
494    data: PipelineData,
495    mut writer: impl Write,
496) -> Result<(), ShellError> {
497    if let PipelineData::ByteStream(stream, ..) = data {
498        stream.write_to(writer)?;
499    } else if let PipelineData::Value(Value::Binary { val, .. }, ..) = data {
500        writer
501            .write_all(&val)
502            .map_err(|err| IoError::new_internal(err, "Could not write pipeline data"))?;
503    } else {
504        stack.start_collect_value();
505
506        // Turn off color as we pass data through
507        Arc::make_mut(&mut engine_state.config).use_ansi_coloring = UseAnsiColoring::False;
508
509        // Invoke the `table` command.
510        let output =
511            crate::Table.run(&engine_state, &mut stack, &Call::new(Span::unknown()), data)?;
512
513        // Write the output.
514        for value in output {
515            let bytes = value.coerce_into_binary()?;
516            writer
517                .write_all(&bytes)
518                .map_err(|err| IoError::new_internal(err, "Could not write pipeline data"))?;
519        }
520    }
521    Ok(())
522}
523
524/// Returns a helpful error message given an invalid command name.
525pub fn command_not_found(
526    name: &str,
527    span: Span,
528    engine_state: &EngineState,
529    stack: &mut Stack,
530    cwd: &AbsolutePath,
531) -> ShellError {
532    // Run the `command_not_found` hook if there is one.
533    if let Some(hook) = &stack.get_config(engine_state).hooks.command_not_found {
534        let mut stack = stack.start_collect_value();
535        // Set a special environment variable to avoid infinite loops when the
536        // `command_not_found` hook triggers itself.
537        let canary = "ENTERED_COMMAND_NOT_FOUND";
538        if stack.has_env_var(engine_state, canary) {
539            return ShellError::ExternalCommand {
540                label: format!(
541                    "Command {name} not found while running the `command_not_found` hook"
542                ),
543                help: "Make sure the `command_not_found` hook itself does not use unknown commands"
544                    .into(),
545                span,
546            };
547        }
548        stack.add_env_var(canary.into(), Value::bool(true, Span::unknown()));
549
550        let output = eval_hook(
551            &mut engine_state.clone(),
552            &mut stack,
553            None,
554            vec![("cmd_name".into(), Value::string(name, span))],
555            hook,
556            "command_not_found",
557        );
558
559        // Remove the special environment variable that we just set.
560        stack.remove_env_var(engine_state, canary);
561
562        match output {
563            Ok(PipelineData::Value(Value::String { val, .. }, ..)) => {
564                return ShellError::ExternalCommand {
565                    label: format!("Command `{name}` not found"),
566                    help: val,
567                    span,
568                };
569            }
570            Err(err) => {
571                return err;
572            }
573            _ => {
574                // The hook did not return a string, so ignore it.
575            }
576        }
577    }
578
579    // If the name is one of the removed commands, recommend a replacement.
580    if let Some(replacement) = crate::removed_commands().get(&name.to_lowercase()) {
581        return ShellError::RemovedCommand {
582            removed: name.to_lowercase(),
583            replacement: replacement.clone(),
584            span,
585        };
586    }
587
588    // Making this a closure allows using return inside instead of nesting if-else's
589    let help = (|| {
590        // The command might be from another module. Try to find it.
591        // Note that built-in command categories are not modules,
592        // hence this won't find `math sqrt` if the user types `sqrt`.
593        if let Some(module) = engine_state.which_module_has_decl(name.as_bytes(), &[]) {
594            let module = String::from_utf8_lossy(module);
595
596            // Is the command already imported?
597            let full_name = format!("{module} {name}");
598            if engine_state.find_decl(full_name.as_bytes(), &[]).is_some() {
599                return format!("Did you mean `{full_name}`?");
600            }
601
602            return format!(
603                "A command with that name exists in module `{module}`. Try importing it with `use`"
604            );
605        }
606
607        // Try to match the name with the search terms of existing commands.
608        let signatures = engine_state.get_signatures_and_declids(false);
609        if let Some((last, others)) = signatures
610            .iter()
611            .map(|(sig, _)| sig)
612            .filter(|sig| {
613                let name = name.to_folded_case(); // do not allocate new strings in any()
614                sig.name
615                    .to_folded_case()
616                    .split_ascii_whitespace() // basically split into words
617                    .contains(name.as_str()) // find this one `math sqrt` from the example above
618                    || sig
619                        .search_terms
620                        .iter()
621                        .any(|term| term.to_folded_case() == name)
622            })
623            .map(|sig| format!("`{}`", sig.name))
624            .collect::<Vec<_>>()
625            .split_last()
626        {
627            let commands = if others.is_empty() {
628                last
629            } else {
630                // other or last
631                // other, other or last
632                &format!("{} or {last}", others.join(", "))
633            };
634
635            return format!("Did you mean {commands}?");
636        }
637
638        // Try a fuzzy search on the names of all existing commands.
639        if let Some(cmd) = did_you_mean(signatures.iter().map(|(sig, _)| &sig.name), name) {
640            // The user is invoking an external command with the same name as a
641            // built-in command. Remind them of this.
642            if cmd == name {
643                return "There is a built-in command with the same name".to_string();
644            }
645
646            return format!("Did you mean `{cmd}`?");
647        }
648
649        // If we find a file, it's likely that the user forgot to set permissions
650        if cwd.join(name).is_file() {
651            return format!(
652                "`{name}` refers to a file that is not executable. Did you forget to set execute permissions?"
653            );
654        }
655
656        // We found nothing useful. Give up and return a generic error message.
657        format!("`{name}` is neither a Nushell built-in or a known external command")
658    })();
659
660    ShellError::ExternalCommand {
661        label: format!("Command `{name}` not found"),
662        help,
663        span,
664    }
665}
666
667/// Searches for the absolute path of an executable by name. `.bat` and `.cmd`
668/// files are recognized as executables on Windows.
669///
670/// This is a wrapper around `which::which_in()` except that, on Windows, it
671/// also searches the current directory before any PATH entries.
672///
673/// Note: the `which.rs` crate always uses PATHEXT from the environment. As
674/// such, changing PATHEXT within Nushell doesn't work without updating the
675/// actual environment of the Nushell process.
676pub fn which(name: impl AsRef<OsStr>, paths: &str, cwd: &Path) -> Option<PathBuf> {
677    #[cfg(windows)]
678    let paths = format!("{};{}", cwd.display(), paths);
679    which::which_in(name, Some(paths), cwd).ok()
680}
681
682/// Returns true if `name` is a (somewhat useful) CMD internal command. The full
683/// list can be found at <https://ss64.com/nt/syntax-internal.html>
684fn is_cmd_internal_command(name: &str) -> bool {
685    const COMMANDS: &[&str] = &[
686        "ASSOC", "CLS", "ECHO", "FTYPE", "MKLINK", "PAUSE", "START", "VER", "VOL",
687    ];
688    COMMANDS.iter().any(|cmd| cmd.eq_ignore_ascii_case(name))
689}
690
691/// Returns true if a string contains CMD special characters.
692fn has_cmd_special_character(s: impl AsRef<[u8]>) -> bool {
693    s.as_ref()
694        .iter()
695        .any(|b| matches!(b, b'<' | b'>' | b'&' | b'|' | b'^'))
696}
697
698/// Escape an argument for CMD internal commands. The result can be safely passed to `raw_arg()`.
699#[cfg_attr(not(windows), allow(dead_code))]
700fn escape_cmd_argument(arg: &Spanned<OsString>) -> Result<Cow<'_, OsStr>, ShellError> {
701    let Spanned { item: arg, span } = arg;
702    let bytes = arg.as_encoded_bytes();
703    if bytes.iter().any(|b| matches!(b, b'\r' | b'\n' | b'%')) {
704        // \r and \n truncate the rest of the arguments and % can expand environment variables
705        Err(ShellError::ExternalCommand {
706            label:
707                "Arguments to CMD internal commands cannot contain new lines or percent signs '%'"
708                    .into(),
709            help: "some characters currently cannot be securely escaped".into(),
710            span: *span,
711        })
712    } else if bytes.contains(&b'"') {
713        // If `arg` is already quoted by double quotes, confirm there's no
714        // embedded double quotes, then leave it as is.
715        if bytes.iter().filter(|b| **b == b'"').count() == 2
716            && bytes.starts_with(b"\"")
717            && bytes.ends_with(b"\"")
718        {
719            Ok(Cow::Borrowed(arg))
720        } else {
721            Err(ShellError::ExternalCommand {
722                label: "Arguments to CMD internal commands cannot contain embedded double quotes"
723                    .into(),
724                help: "this case currently cannot be securely handled".into(),
725                span: *span,
726            })
727        }
728    } else if bytes.contains(&b' ') || has_cmd_special_character(bytes) {
729        // If `arg` contains space or special characters, quote the entire argument by double quotes.
730        let mut new_str = OsString::new();
731        new_str.push("\"");
732        new_str.push(arg);
733        new_str.push("\"");
734        Ok(Cow::Owned(new_str))
735    } else {
736        // FIXME?: what if `arg.is_empty()`?
737        Ok(Cow::Borrowed(arg))
738    }
739}
740
741#[cfg(test)]
742mod test {
743    use super::*;
744    use nu_test_support::{fs::Stub, playground::Playground};
745
746    #[test]
747    fn test_expand_glob() {
748        Playground::setup("test_expand_glob", |dirs, play| {
749            play.with_files(&[Stub::EmptyFile("a.txt"), Stub::EmptyFile("b.txt")]);
750
751            let cwd = dirs.test().as_std_path();
752
753            let actual = expand_glob("*.txt", cwd, Span::test_data(), Signals::empty()).unwrap();
754            let expected = &["a.txt", "b.txt"];
755            assert_eq!(actual, expected);
756
757            let actual = expand_glob("./*.txt", cwd, Span::test_data(), Signals::empty()).unwrap();
758            assert_eq!(actual, expected);
759
760            let actual = expand_glob("'*.txt'", cwd, Span::test_data(), Signals::empty()).unwrap();
761            let expected = &["'*.txt'"];
762            assert_eq!(actual, expected);
763
764            let actual = expand_glob(".", cwd, Span::test_data(), Signals::empty()).unwrap();
765            let expected = &["."];
766            assert_eq!(actual, expected);
767
768            let actual = expand_glob("./a.txt", cwd, Span::test_data(), Signals::empty()).unwrap();
769            let expected = &["./a.txt"];
770            assert_eq!(actual, expected);
771
772            let actual = expand_glob("[*.txt", cwd, Span::test_data(), Signals::empty()).unwrap();
773            let expected = &["[*.txt"];
774            assert_eq!(actual, expected);
775
776            let actual =
777                expand_glob("~/foo.txt", cwd, Span::test_data(), Signals::empty()).unwrap();
778            let home = dirs::home_dir().expect("failed to get home dir");
779            let expected: Vec<OsString> = vec![home.join("foo.txt").into()];
780            assert_eq!(actual, expected);
781        })
782    }
783
784    #[test]
785    fn test_write_pipeline_data() {
786        let mut engine_state = EngineState::new();
787        let stack = Stack::new();
788        let cwd = std::env::current_dir()
789            .unwrap()
790            .into_os_string()
791            .into_string()
792            .unwrap();
793
794        // set the PWD environment variable as it's required now
795        engine_state.add_env_var("PWD".into(), Value::string(cwd, Span::test_data()));
796
797        let mut buf = vec![];
798        let input = PipelineData::empty();
799        write_pipeline_data(engine_state.clone(), stack.clone(), input, &mut buf).unwrap();
800        assert_eq!(buf, b"");
801
802        let mut buf = vec![];
803        let input = PipelineData::value(Value::string("foo", Span::test_data()), None);
804        write_pipeline_data(engine_state.clone(), stack.clone(), input, &mut buf).unwrap();
805        assert_eq!(buf, b"foo");
806
807        let mut buf = vec![];
808        let input = PipelineData::value(Value::binary(b"foo", Span::test_data()), None);
809        write_pipeline_data(engine_state.clone(), stack.clone(), input, &mut buf).unwrap();
810        assert_eq!(buf, b"foo");
811
812        let mut buf = vec![];
813        let input = PipelineData::byte_stream(
814            ByteStream::read(
815                b"foo".as_slice(),
816                Span::test_data(),
817                Signals::empty(),
818                ByteStreamType::Unknown,
819            ),
820            None,
821        );
822        write_pipeline_data(engine_state.clone(), stack.clone(), input, &mut buf).unwrap();
823        assert_eq!(buf, b"foo");
824    }
825}