nushell/crates
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
..
nu_plugin_custom_values Bump version to 0.93.1 (#12710) 2024-05-01 17:19:20 -05:00
nu_plugin_example Add string/binary type color to ByteStream (#12897) 2024-05-20 00:35:32 +00:00
nu_plugin_formats Bump version to 0.93.1 (#12710) 2024-05-01 17:19:20 -05:00
nu_plugin_gstat Bump version to 0.93.1 (#12710) 2024-05-01 17:19:20 -05:00
nu_plugin_inc Bump version to 0.93.1 (#12710) 2024-05-01 17:19:20 -05:00
nu_plugin_nu_example Bump version to 0.93.1 (#12710) 2024-05-01 17:19:20 -05:00
nu_plugin_polars Remove dataframes crate and feature (#12889) 2024-05-20 17:22:08 +00:00
nu_plugin_python Bump version to 0.93.1 (#12710) 2024-05-01 17:19:20 -05:00
nu_plugin_query Bump version to 0.93.1 (#12710) 2024-05-01 17:19:20 -05:00
nu_plugin_stress_internals Upgrade to interprocess 2.0.0 (#12729) 2024-05-02 22:31:33 -07:00
nu-cli Remove std::env::set_current_dir() call from EngineState::merge_env() (#12922) 2024-05-22 19:58:27 +03:00
nu-cmd-base fix range semantic in detect_columns, str substring, str index-of (#12894) 2024-05-22 20:00:58 +03:00
nu-cmd-extra Make get_full_help take &dyn Command (#12903) 2024-05-19 19:56:33 +02:00
nu-cmd-lang Remove std::env::set_current_dir() call from EngineState::merge_env() (#12922) 2024-05-22 19:58:27 +03:00
nu-cmd-plugin Make get_full_help take &dyn Command (#12903) 2024-05-19 19:56:33 +02:00
nu-color-config Replace ExternalStream with new ByteStream type (#12774) 2024-05-16 07:11:18 -07:00
nu-command Rewrite run_external.rs (#12921) 2024-05-23 02:05:27 +00:00
nu-engine Add string/binary type color to ByteStream (#12897) 2024-05-20 00:35:32 +00:00
nu-explore explore: consolidate padding config, handle ByteStream, tweak naming+comments (#12915) 2024-05-20 22:03:21 +02:00
nu-glob Bump version to 0.93.1 (#12710) 2024-05-01 17:19:20 -05:00
nu-json Minimize future false positive typos (#12751) 2024-05-04 15:00:44 +00:00
nu-lsp Use CommandType in more places (#12832) 2024-05-18 23:37:31 +00:00
nu-parser Use CommandType in more places (#12832) 2024-05-18 23:37:31 +00:00
nu-path Fix trailing slash in PWD set by cd (#12760) 2024-05-04 12:38:37 +03:00
nu-plugin Add string/binary type color to ByteStream (#12897) 2024-05-20 00:35:32 +00:00
nu-plugin-core Add string/binary type color to ByteStream (#12897) 2024-05-20 00:35:32 +00:00
nu-plugin-engine Add string/binary type color to ByteStream (#12897) 2024-05-20 00:35:32 +00:00
nu-plugin-protocol Add string/binary type color to ByteStream (#12897) 2024-05-20 00:35:32 +00:00
nu-plugin-test-support Replace ExternalStream with new ByteStream type (#12774) 2024-05-16 07:11:18 -07:00
nu-pretty-hex Add string/binary type color to ByteStream (#12897) 2024-05-20 00:35:32 +00:00
nu-protocol Fix leftover wrong column name (#12937) 2024-05-22 21:24:22 +00:00
nu-std Remove std::env::set_current_dir() call from EngineState::merge_env() (#12922) 2024-05-22 19:58:27 +03:00
nu-system Add support for the ps command on FreeBSD, NetBSD, and OpenBSD (#12892) 2024-05-22 08:13:45 -07:00
nu-table Avoid taking unnecessary ownership of intermediates (#12740) 2024-05-04 00:53:15 +00:00
nu-term-grid Bump version to 0.93.1 (#12710) 2024-05-01 17:19:20 -05:00
nu-test-support Exclude polars from ensure_plugins_built(), for performance reasons (#12896) 2024-05-17 15:04:59 +00:00
nu-utils overhaul shell_integration to enable individual control over ansi escape sequences (#12629) 2024-05-02 09:56:50 -04:00
nuon Shrink Value by boxing Range/Closure (#12784) 2024-05-09 08:10:58 +08:00
README.md Remove old nushell/merge engine-q 2022-02-07 14:54:06 -05:00

Nushell core libraries and plugins

These sub-crates form both the foundation for Nu and a set of plugins which extend Nu with additional functionality.

Foundational libraries are split into two kinds of crates:

  • Core crates - those crates that work together to build the Nushell language engine
  • Support crates - a set of crates that support the engine with additional features like JSON support, ANSI support, and more.

Plugins are likewise also split into two types:

  • Core plugins - plugins that provide part of the default experience of Nu, including access to the system properties, processes, and web-connectivity features.
  • Extra plugins - these plugins run a wide range of different capabilities like working with different file types, charting, viewing binary data, and more.