Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix rm for symlinks pointing to directory on windows (issue #11461) #11463

Merged
merged 3 commits into from Jan 2, 2024

Conversation

yukitomoda
Copy link
Contributor

@yukitomoda yukitomoda commented Jan 2, 2024

Description

Using std::fs::remove_dir instead of std::fs::remove_file when try remove symlinks pointing to a directory on Windows.

User-Facing Changes

none

Tests + Formatting

  • 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)
    • I got 2 test fails on my Windows devenv; these fails in main branch too
      • commands::complete::basic : passed on Ubuntu, failed on Windows (a bug?)
      • commands::cp::copy_file_with_read_permission: failed on Windows with Japanese environment (This test refers error message, so that fails on environments using a language except for english.)
  • cargo run -- -c "use std testing; testing run-tests --path crates/nu-std" to run the tests for the standard library

After Submitting

This fix has no changes to user-facing interface.

@yukitomoda
Copy link
Contributor Author

yukitomoda commented Jan 2, 2024

As above, I got 2 test failure on my Windows (Japanese), but these passed on my ubuntu.

commands::complete::basic

https://github.com/nushell/nushell/blob/f5973801129a13d834af9f0e738d182e49cc2e3b/crates/nu-command/tests/commands/complete.rs#L4C1-L10

I think this is caused by \n or \r\n, but have no idea why this passed on CI...

~\work\tmp
❯ (^echo a | complete) == {stdout: "a\n", exit_code: 0}
false

~\work\tmp
❯ (^echo a | complete) == {stdout: "a\r\n", exit_code: 0}
true

commands::cp::copy_file_with_read_permission

https://github.com/yukitomoda/nushell/blob/de5ad5de19affee3986661da6dd888a2e12a813f/crates/nu-command/tests/commands/cp.rs#L576-L582

I think this is caused by referencing error messages in this test.

// In Windows, symlink pointing to a directory can be removed using
// std::fs::remove_dir instead of std::fs::remove_file.
#[cfg(windows)]
match f.metadata() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about using and_then?

f.metadata().and_then(|metadata|
    if metadata.is_dir() { std::fs::remove_dir(&f) }
    else { std::fs::remove_file(&f) }
}?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about using and_then?

Indeed, it's a better way.
I'm going to refactor it.

@WindSoilder
Copy link
Collaborator

As above, I got 2 test failure on my Windows (Japanese), but these passed on my ubuntu.

I remember it's because windows CI have git bash installed

@yukitomoda
Copy link
Contributor Author

As above, I got 2 test failure on my Windows (Japanese), but these passed on my ubuntu.

I remember it's because windows CI have git bash installed

Oh, it makes sense.
I tried cargo test --workspace in git bash and commands::complete::basic passed.

Then, should we change the tests to be independent on platforms? Or should we note in CONTRIBUTING.md that cargo test --workspace should be runned in git bash on Windows?

@fdncred
Copy link
Collaborator

fdncred commented Jan 2, 2024

We should not rely on git bash being installed.

Copy link
Collaborator

@WindSoilder WindSoilder left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! LGTM!

For the failing test on windows, I think we can cover it in separate pr :-D
Thanks for your contribution!

@WindSoilder WindSoilder merged commit 42bb42a into nushell:main Jan 2, 2024
19 checks passed
@yukitomoda yukitomoda deleted the fix-rm-symlink-to-dir-windows branch January 2, 2024 13:46
fdncred pushed a commit that referenced this pull request Jan 3, 2024
- related PR: #11463

# Description

Currently, `commands::complete::basic` fails on Windows without git
bash.
This pr fixes it.

# User-Facing Changes

(none)

# Tests + Formatting

- [x] (on Windows) `cargo fmt --all -- --check` to check standard code
formatting (`cargo fmt --all` applies these changes)
- [x] (on Windows) `cargo clippy --workspace -- -D warnings -D
clippy::unwrap_used` to check that you're using the standard code style
- [x] (on Windows without git bash, Windows with git bash and Ubuntu)
`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))
- on my Windows with Japanese lang pack: 1 test still fails. (see
#11463)
- [x] (on Windows and Ubuntu) `cargo run -- -c "use std testing; testing
run-tests --path crates/nu-std"` to run the tests for the standard
library

# After Submitting

(none)
dmatos2012 pushed a commit to dmatos2012/nushell that referenced this pull request Feb 20, 2024
…1461) (nushell#11463)

<!--
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!
-->

- this PR closes nushell#11461

# Description
<!--
Thank you for improving Nushell. Please, check our [contributing
guide](../CONTRIBUTING.md) and talk to the core team before making major
changes.

Description of your pull request goes here. **Provide examples and/or
screenshots** if your changes affect the user experience.
-->

Using `std::fs::remove_dir` instead of `std::fs::remove_file` when try
remove symlinks pointing to a directory on Windows.

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

none

# 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 std testing; testing run-tests --path
crates/nu-std"` 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
> ```
-->

- [x] `cargo fmt --all -- --check` to check standard code formatting
(`cargo fmt --all` applies these changes)
- [x] `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used`
to check that you're using the standard code style
- [x] `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))
- I got 2 test fails on my Windows devenv; these fails in main branch
too
- `commands::complete::basic` : passed on Ubuntu, failed on Windows (a
bug?)
- `commands::cp::copy_file_with_read_permission`: failed on Windows with
Japanese environment (This test refers error message, so that fails on
environments using a language except for english.)
- [x] `cargo run -- -c "use std testing; testing run-tests --path
crates/nu-std"` to run the tests for the standard library

# 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.
-->

This fix has no changes to user-facing interface.
dmatos2012 pushed a commit to dmatos2012/nushell that referenced this pull request Feb 20, 2024
- related PR: nushell#11463

# Description

Currently, `commands::complete::basic` fails on Windows without git
bash.
This pr fixes it.

# User-Facing Changes

(none)

# Tests + Formatting

- [x] (on Windows) `cargo fmt --all -- --check` to check standard code
formatting (`cargo fmt --all` applies these changes)
- [x] (on Windows) `cargo clippy --workspace -- -D warnings -D
clippy::unwrap_used` to check that you're using the standard code style
- [x] (on Windows without git bash, Windows with git bash and Ubuntu)
`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))
- on my Windows with Japanese lang pack: 1 test still fails. (see
nushell#11463)
- [x] (on Windows and Ubuntu) `cargo run -- -c "use std testing; testing
run-tests --path crates/nu-std"` to run the tests for the standard
library

# After Submitting

(none)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

rm: Cannot remove a symlink pointing to a directory on Windows
3 participants