nushell/tests/repl
YizhePKU 6c649809d3
Rewrite run_external.rs (#12921)
This PR is a complete rewrite of `run_external.rs`. The main goal of the
rewrite is improving readability, but it also fixes some bugs related to
argument handling and the PATH variable (fixes
https://github.com/nushell/nushell/issues/6011).

I'll discuss some technical details to make reviewing easier.

## Argument handling

Quoting arguments for external commands is hard. Like, *really* hard.
We've had more than a dozen issues and PRs dedicated to quoting
arguments (see Appendix) but the current implementation is still buggy.

Here's a demonstration of the buggy behavior:

```nu
let foo = "'bar'"
^touch $foo            # This creates a file named `bar`, but it should be `'bar'`
^touch ...[ "'bar'" ]  # Same
```

I'll describe how this PR deals with argument handling.

First, we'll introduce the concept of **bare strings**. Bare strings are
**string literals** that are either **unquoted** or **quoted by
backticks** [^1]. Strings within a list literal are NOT considered bare
strings, even if they are unquoted or quoted by backticks.

When a bare string is used as an argument to external process, we need
to perform tilde-expansion, glob-expansion, and inner-quotes-removal, in
that order. "Inner-quotes-removal" means transforming from
`--option="value"` into `--option=value`.

## `.bat` files and CMD built-ins

On Windows, `.bat` files and `.cmd` files are considered executable, but
they need `CMD.exe` as the interpreter. The Rust standard library
supports running `.bat` files directly and will spawn `CMD.exe` under
the hood (see
[documentation](https://doc.rust-lang.org/std/process/index.html#windows-argument-splitting)).
However, other extensions are not supported [^2].

Nushell also supports a selected number of CMD built-ins. The problem
with CMD is that it uses a different set of quoting rules. Correctly
quoting for CMD requires using
[Command::raw_arg()](https://doc.rust-lang.org/std/os/windows/process/trait.CommandExt.html#tymethod.raw_arg)
and manually quoting CMD special characters, on top of quoting from the
Nushell side. ~~I decided that this is too complex and chose to reject
special characters in CMD built-ins instead [^3]. Hopefully this will
not affact real-world use cases.~~ I've implemented escaping that works
reasonably well.

## `which-support` feature

The `which` crate is now a hard dependency of `nu-command`, making the
`which-support` feature essentially useless. The `which` crate is
already a hard dependency of `nu-cli`, and we should consider removing
the `which-support` feature entirely.

## Appendix

Here's a list of quoting-related issues and PRs in rough chronological
order.

* https://github.com/nushell/nushell/issues/4609
* https://github.com/nushell/nushell/issues/4631
* https://github.com/nushell/nushell/issues/4601
  * https://github.com/nushell/nushell/pull/5846
* https://github.com/nushell/nushell/issues/5978
  * https://github.com/nushell/nushell/pull/6014
* https://github.com/nushell/nushell/issues/6154
  * https://github.com/nushell/nushell/pull/6161
* https://github.com/nushell/nushell/issues/6399
  * https://github.com/nushell/nushell/pull/6420
  * https://github.com/nushell/nushell/pull/6426
* https://github.com/nushell/nushell/issues/6465
* https://github.com/nushell/nushell/issues/6559
  * https://github.com/nushell/nushell/pull/6560

[^1]: The idea that backtick-quoted strings act like bare strings was
introduced by Kubouch and briefly mentioned in [the language
reference](https://www.nushell.sh/lang-guide/chapters/strings_and_text.html#backtick-quotes).

[^2]: The documentation also said "running .bat scripts in this way may
be removed in the future and so should not be relied upon", which is
another reason to move away from this. But again, quoting for CMD is
hard.

[^3]: If anyone wants to try, the best resource I found on the topic is
[this](https://daviddeley.com/autohotkey/parameters/parameters.htm).
2024-05-23 02:05:27 +00:00
..
mod.rs Merged tests to produce a single binary (#12826) 2024-05-13 13:37:53 +00:00
test_bits.rs Merged tests to produce a single binary (#12826) 2024-05-13 13:37:53 +00:00
test_cell_path.rs Merged tests to produce a single binary (#12826) 2024-05-13 13:37:53 +00:00
test_commandline.rs Merged tests to produce a single binary (#12826) 2024-05-13 13:37:53 +00:00
test_conditionals.rs Merged tests to produce a single binary (#12826) 2024-05-13 13:37:53 +00:00
test_config_path.rs Merged tests to produce a single binary (#12826) 2024-05-13 13:37:53 +00:00
test_config.rs Merged tests to produce a single binary (#12826) 2024-05-13 13:37:53 +00:00
test_converters.rs Merged tests to produce a single binary (#12826) 2024-05-13 13:37:53 +00:00
test_custom_commands.rs allow passing float value to custom command (#12879) 2024-05-16 10:50:29 +02:00
test_engine.rs Make get_full_help take &dyn Command (#12903) 2024-05-19 19:56:33 +02:00
test_env.rs Merged tests to produce a single binary (#12826) 2024-05-13 13:37:53 +00:00
test_help.rs Merged tests to produce a single binary (#12826) 2024-05-13 13:37:53 +00:00
test_hiding.rs Merged tests to produce a single binary (#12826) 2024-05-13 13:37:53 +00:00
test_ide.rs Merged tests to produce a single binary (#12826) 2024-05-13 13:37:53 +00:00
test_iteration.rs Merged tests to produce a single binary (#12826) 2024-05-13 13:37:53 +00:00
test_known_external.rs Rewrite run_external.rs (#12921) 2024-05-23 02:05:27 +00:00
test_math.rs Merged tests to produce a single binary (#12826) 2024-05-13 13:37:53 +00:00
test_modules.rs Merged tests to produce a single binary (#12826) 2024-05-13 13:37:53 +00:00
test_parser.rs Rewrite run_external.rs (#12921) 2024-05-23 02:05:27 +00:00
test_ranges.rs Merged tests to produce a single binary (#12826) 2024-05-13 13:37:53 +00:00
test_regex.rs Merged tests to produce a single binary (#12826) 2024-05-13 13:37:53 +00:00
test_signatures.rs Merged tests to produce a single binary (#12826) 2024-05-13 13:37:53 +00:00
test_spread.rs Rewrite run_external.rs (#12921) 2024-05-23 02:05:27 +00:00
test_stdlib.rs Merged tests to produce a single binary (#12826) 2024-05-13 13:37:53 +00:00
test_strings.rs fix range semantic in detect_columns, str substring, str index-of (#12894) 2024-05-22 20:00:58 +03:00
test_table_operations.rs Merged tests to produce a single binary (#12826) 2024-05-13 13:37:53 +00:00
test_type_check.rs Merged tests to produce a single binary (#12826) 2024-05-13 13:37:53 +00:00
tests.rs Merged tests to produce a single binary (#12826) 2024-05-13 13:37:53 +00:00