Skip to main content

nu_command/system/
run_external.rs

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