Skip to content

Commit

Permalink
Don't expand ndots if prefixed with ./ (nushell#14755)
Browse files Browse the repository at this point in the history
<!--
if this PR closes one or more issues, you can automatically link the PR
with
them by using one of the [*linking
keywords*](https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue#linking-a-pull-request-to-an-issue-using-a-keyword),
e.g.
- this PR should close #xxxx
- fixes #xxxx

you can also mention related issues, PRs or discussions!
-->

# Description
Prevents ndots from being expanded if they are prefixed with `./`, as
the agreed resolution to nushell#13303. Only applies to externals, mirroring
the fix from nushell#13218.

I did
[attempt](https://github.com/132ikl/nushell/tree/internal-ndots-attempt)
to apply the fix for internal commands as well, but it seems like the
path is expanded too aggressively and I haven't investigated it further
yet. `./...` gets normalized into `<pwd>/./...`, which gets normalized
into `<pwd>/...` before being handed to `expand_ndots`, and at that
point it just looks like a normal n-dots so we can't tell we shouldn't
expand.

(Fixes nushell#13303)

# User-Facing Changes
<!-- List of all changes that impact the user experience here. This
helps us keep track of breaking changes. -->

* N-dots are no longer expanded to external command calls when prefixed
with `./`.

# Tests + Formatting
<!--
Don't forget to add tests that cover your changes.

Make sure you've run and fixed any issues with these commands:

- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used` to
check that you're using the standard code style
- `cargo test --workspace` to check that all tests pass (on Windows make
sure to [enable developer
mode](https://learn.microsoft.com/en-us/windows/apps/get-started/developer-mode-features-and-debugging))
- `cargo run -- -c "use toolkit.nu; toolkit test stdlib"` to run the
tests for the standard library

> **Note**
> from `nushell` you can also use the `toolkit` as follows
> ```bash
> use toolkit.nu # or use an `env_change` hook to activate it
automatically
> toolkit check pr
> ```
-->



Added tests to prevent regression.

- 🟢 `toolkit fmt`
- 🟢 `toolkit clippy`
- 🟢 `toolkit test`
- 🟢 `toolkit test stdlib`

# After Submitting
<!-- If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
-->

N/A
  • Loading branch information
132ikl authored Jan 5, 2025
1 parent 2b4c54d commit b60f91f
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 17 deletions.
17 changes: 1 addition & 16 deletions crates/nu-command/src/system/run_external.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use nu_cmd_base::hook::eval_hook;
use nu_engine::{command_prelude::*, env_to_strings};
use nu_path::{dots::expand_ndots, expand_tilde, AbsolutePath};
use nu_path::{dots::expand_ndots_safe, expand_tilde, AbsolutePath};
use nu_protocol::{
did_you_mean, process::ChildProcess, ByteStream, NuGlob, OutDest, Signals, UseAnsiColoring,
};
Expand Down Expand Up @@ -639,21 +639,6 @@ fn escape_cmd_argument(arg: &Spanned<OsString>) -> Result<Cow<'_, OsStr>, ShellE
}
}

/// Expand ndots, but only if it looks like it probably contains them, because there is some lossy
/// path normalization that happens.
fn expand_ndots_safe(path: impl AsRef<Path>) -> PathBuf {
let string = path.as_ref().to_string_lossy();

// Use ndots if it contains at least `...`, since that's the minimum trigger point, and don't
// use it if it contains ://, because that looks like a URL scheme and the path normalization
// will mess with that.
if string.contains("...") && !string.contains("://") {
expand_ndots(path)
} else {
path.as_ref().to_owned()
}
}

#[cfg(test)]
mod test {
use super::*;
Expand Down
10 changes: 10 additions & 0 deletions crates/nu-command/tests/commands/run_external.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,16 @@ fn external_command_ndots_args() {
);
}

#[test]
fn external_command_ndots_leading_dot_slash() {
// Don't expand ndots with a leading `./`
let actual = nu!(r#"
nu --testbin cococo ./... ./....
"#);

assert_eq!(actual.out, "./... ./....");
}

#[test]
fn external_command_url_args() {
// If ndots is not handled correctly, we can lose the double forward slashes that are needed
Expand Down
23 changes: 23 additions & 0 deletions crates/nu-path/src/dots.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,23 @@ pub fn expand_dots(path: impl AsRef<Path>) -> PathBuf {
simiplified(&result)
}

/// Expand ndots, but only if it looks like it probably contains them, because there is some lossy
/// path normalization that happens.
pub fn expand_ndots_safe(path: impl AsRef<Path>) -> PathBuf {
let string = path.as_ref().to_string_lossy();

// Use ndots if it contains at least `...`, since that's the minimum trigger point.
// Don't use it if it contains ://, because that looks like a URL scheme and the path normalization
// will mess with that.
// Don't use it if it starts with `./`, as to not break golang wildcard syntax
// (since generally you're probably not using `./` with ndots)
if string.contains("...") && !string.contains("://") && !string.starts_with("./") {
expand_ndots(path)
} else {
path.as_ref().to_owned()
}
}

#[cfg(windows)]
fn simiplified(path: &std::path::Path) -> PathBuf {
path.to_winuser_path()
Expand Down Expand Up @@ -169,6 +186,12 @@ mod test_expand_ndots {
};
assert_path_eq!(expand_ndots(path), expected);
}

#[test]
fn leading_dot_slash() {
let path = Path::new("./...");
assert_path_eq!(expand_ndots_safe(path), "./...");
}
}

#[cfg(test)]
Expand Down
1 change: 0 additions & 1 deletion crates/nu-path/src/tilde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,6 @@ fn expand_tilde_with_another_user_home(path: &Path) -> PathBuf {

/// Expand tilde ("~") into a home directory if it is the first path component
pub fn expand_tilde(path: impl AsRef<Path>) -> PathBuf {
// TODO: Extend this to work with "~user" style of home paths
expand_tilde_with_home(path, dirs::home_dir())
}

Expand Down

0 comments on commit b60f91f

Please sign in to comment.