Repository navigation
Refactor I/O Errors - #14927
Refactor I/O Errors#14927
Conversation
|
I'm open to changing the exact display format of the file reference but not everything is easily done using The format with |
|
We are 1 week before the next release. I'm hoping we'll be able to land this but I'm not sure there is time. We shall see. |
|
I'm not opposed to landing this and dogfood for 1 week. |
|
Looks great! I was a little concerned about how often we would be showing code locations in errors, but after looking through all the usages of the internal errors they seem to all make sense. I did find two cases I think we can improve:
diff --git a/crates/nu-engine/src/glob_from.rs b/crates/nu-engine/src/glob_from.rs
index bf4801ba1..1120499b4 100644
--- a/crates/nu-engine/src/glob_from.rs
+++ b/crates/nu-engine/src/glob_from.rs
@@ -27,6 +27,7 @@ pub fn glob_from(
ShellError,
> {
let no_glob_for_pattern = matches!(pattern.item, NuGlob::DoNotExpand(_));
+ let pattern_span = pattern.span;
let (prefix, pattern) = if nu_glob::is_glob(pattern.item.as_ref()) {
// Pattern contains glob, split it
let mut p = PathBuf::new();
@@ -79,13 +80,7 @@ pub fn glob_from(
}
Ok(p) => p,
Err(err) => {
- return Err(IoError::new_internal_with_path(
- err.kind(),
- "Could not canonicalize path",
- nu_protocol::location!(),
- path,
- )
- .into())
+ return Err(IoError::new(err.kind(), pattern_span, path).into());
}
};
(path.parent().map(|parent| parent.to_path_buf()), path)Before: ls foo
# => Error: nu::shell::io::not_found
# =>
# => × I/O error
# => ╰─▶ × Entity not found
# =>
# => ╭────
# => 1 │ crates/nu-engine/src/glob_from.rs:85:25
# => · ───────────────────┬───────────────────
# => · ╰── Could not canonicalize path
# => ╰────
# => help: The error occurred at '/home/rose/src/nushell/foo'After: ls foo
# => Error: nu::shell::io::not_found
# =>
# => × I/O error
# => ╰─▶ × Entity not found
# =>
# => ╭─[entry #6:1:4]
# => 1 │ ls foo
# => · ───
# => ╰────
# => help: The error occurred at '/home/rose/src/nushell/foo'
If you would be able to fix those, I'm good with landing this. |
|
@132ikl I updated that 👍🏻 I worked through most of the code via deprecation warnings, so I missed that. |
|
lgtm, thank you! |
|
if we can get the ci green, i'll land this pr |
|
Great work here! Thanks so much! |
<!-- 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 <!-- 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. --> It seems in my PR #14927 I missed a few calls to `IoError::new` that had `Span::unknown` inside them, which shouldn't be used but rather `IoError::new_internal`. I replaced these calls. Thanks to @132ikl to finding out that I forgot some. 😄 # User-Facing Changes <!-- List of all changes that impact the user experience here. This helps us keep track of breaking changes. --> Pretty much none really. # 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 > ``` --> - 🟢 `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. -->
<!-- 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 <!-- 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. --> Tweaks the error style for I/O errors introduced #14927. Moves the additional context to below the text that says "I/O error", and always shows the error kind in the label. Additional context|Before PR|After PR :-:|:-:|:-: yes| |  no|  |  # User-Facing Changes <!-- List of all changes that impact the user experience here. This helps us keep track of breaking changes. --> N/A, as this is a follow-up to #14927 which has not been included in a release # 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 > ``` --> N/A # 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 --------- Co-authored-by: Piepmatz <[email protected]>
Description
As mentioned in #10698, we have too many
ShellErrorvariants, with some even overlapping in meaning. This PR simplifies and improves I/O error handling by restructuringShellErrorrelated to I/O issues. Previously,ShellError::IOErroronly contained a message string, making it convenient but overly generic. It was widely used without providing spans (#4323).This PR introduces a new
ShellError::Iovariant that consolidates multiple I/O-related errors (except forShellError::NetworkFailure, which remains distinct for now). The newShellError::Iovariant replaces the following:FileNotFoundFileNotFoundCustomIOInterruptedIOErrorIOErrorSpannedNotADirectoryDirectoryNotFoundMoveNotPossibleCreateNotPossibleChangeAccessTimeNotPossibleChangeModifiedTimeNotPossibleRemoveNotPossibleReadingFileThe
IoErrorIoErrorincludes the following fields:kind: Extendsstd::io::ErrorKindto specify the type of I/O error without needing newShellErrorvariants. This aligns with the approach used instd::io::Error. This adds a second dimension to error reporting by combining thekindfield withShellErrorvariants, making it easier to describe errors in more detail. As proposed by @kubouch in #design-discussion on Discord, this helps reduce the number ofShellErrorvariants. In the error report, thekindfield is displayed as the "source" of the error, e.g., "I/O error," followed by the specific kind of I/O error.span: A non-optional field to encourage providing spans for better error reporting (Deprecate error types without spans #4323).path: OptionalPathBufto give context about the file or directory involved in the error (Provide more context to OS errors #7695). If provided, it’s shown as a help entry in error reports.additional_context: Allows adding custom messages when the span, kind, and path are insufficient. This is rendered in the error report at the labeled span.location: Sometimes, I/O errors occur in the engine itself and are not caused directly by user input. In such cases, if we don’t have a span and must set it toSpan::unknown(), we need another way to reference the error. For this, thelocationfield uses the newLocationstruct, which records the Rust file and line number where the error occurred. This ensures that we at least know the Rust code location that failed, helping with debugging. To make this work, a newlocation!macro was added, which retrievesfile!,line!, andcolumn!values accurately. IfLocation::newis used directly, it issues a warning to remind developers to use the macro instead, ensuring consistent and correct usage.Constructor Behavior
IoErrorprovides five constructor methods:newandnew_with_additional_context: Used for errors caused by user input and require a valid (non-unknown) span to ensure precise error reporting.new_internalandnew_internal_with_path: Used for internal errors where a span is not available. These methods require additional context and theLocationstruct to pinpoint the source of the error in the engine code.factory: Returns a closure that maps anstd::io::Errorto anIoError. This is useful for handling multiple I/O errors that share the same span and path, streamlining error handling in such cases.New Report Look
This is simulation how the I/O errors look like (the

open cratesis simulated to show how internal errors are referenced now):Span::test_data()To enable better testing,
Span::test_data()now returns a value distinct fromSpan::unknown(). BothSpan::test_data()andSpan::unknown()refer to invalid source code, but having a separate value for test data helps identify issues during testing while keeping spans unique.Cursed Sneaky Error Transfers
I removed the conversions between
std::io::ErrorandShellErroras they often removed important information and were used too broadly to handle I/O errors. This also removed the problematic implementation found here:nushell/crates/nu-protocol/src/errors/shell_error.rs
Lines 1534 to 1583 in 7ea4895
which hid some downcasting from I/O errors and made it hard to trace where
ShellErrorwas converted intostd::io::Error. To address this, I introduced a new struct calledShellErrorBridge, which explicitly defines this transfer behavior. WithShellErrorBridge, we can now easily grep the codebase to locate and manage such conversions.Miscellaneous
commands::network::http::*::*_timeouttests on non-english system #14640, as it’s no longer needed.glob_from(Handle permission denied error atnu_engine::glob_from#14679).opencaused a permissions denied error (it's just what the OS provides). I added ais_dircheck to provide a better error in that case.User-Facing Changes
ShellErrorhas changed, requiring plugin authors and embedders to update their implementations.Tests + Formatting
I updated tests to account for the new I/O error structure and formatting changes.
After Submitting
This PR closes #7695 and closes #14892 and partially addresses #4323 and #10698.