rustc: improve diagnostics for file-open failures#158323
Conversation
|
The parser was modified, potentially altering the grammar of (stable) Rust cc @fmease |
|
r? @nnethercote rustbot has assigned @nnethercote. Use Why was this reviewer chosen?The reviewer was selected based on:
|
|
cc: @estebank |
There was a problem hiding this comment.
Generally looks good. A couple of nits below, and I will give @estebank some more time to comment.
dae50a7 to
2f4edae
Compare
|
Edge case: permission denied for directory. This PR reports "permission denied when opening file". For comparison, $ mkdir /tmp/asd
$ chmod 000 /tmp/asd
$ ./rustc /tmp/asd
error: permission denied when opening file `/tmp/asd`
error: aborting due to 1 previous error
$ ls /tmp/asd
ls: cannot open directory '/tmp/asd': Permission deniedThough for directories with access it prints an error about directory: $ chmod 777 /tmp/asd
$ ./rustc /tmp/asd
error: `/tmp/asd` is a directory
error: aborting due to 1 previous error |
There was a problem hiding this comment.
Edge case: permission denied for directory. This PR reports "permission denied when opening file". For comparison, ls from gnu-coreutils handles such case correctly.
Although I agree, I would be hesitant with trying to naively improve that case because depending on how it is written, it won't be TOCTOU-safe. I feel it is ok to go with the current approach of just customizing the text from what ErrorKind is returned.
I'm happy with the changes overall.
| parent.join(suggestion.as_str()).display().to_string() | ||
| }; | ||
| err.help(format!( | ||
| "you might have meant to open `{}`: `rustc {}`", |
There was a problem hiding this comment.
Let's not provide the command example because if someone is running a more complex command (even as simple as rustc foo.rs --edition=2024), we'd be suggesting the wrong thing. Just mentioning the file name should be enough (at least for now).
| if e.kind() == ErrorKind::NotFound { | ||
| if let Some(file_name) = path.file_name().and_then(|n| n.to_str()) { | ||
| let parent = | ||
| path.parent().filter(|p| !p.as_os_str().is_empty()).unwrap_or(Path::new(".")); |
There was a problem hiding this comment.
Minor concern: is this the right way of doing this? It seems ok, but I am surprised there's no better way of doing this :)
There was a problem hiding this comment.
Good point. I didn't find a dedicated helper for this case. parent() can yield an empty path for relative paths like foo.rs, and we need a directory we can iterate, so I filtered out the empty path and fell back to ".". Happy to switch to a more idiomatic approach if there's one I'm missing.
What of this ?
let parent = match path.parent() {
Some(p) if !p.as_os_str().is_empty() => p,
_ => Path::new("."),
};
2f4edae to
a13e2da
Compare
|
@bors r+ rollup |
…e, r=nnethercote rustc: improve diagnostics for file-open failures Fix: [rust-lang#156070](rust-lang#156070)
Rollup of 6 pull requests Successful merges: - #158073 (bootstrap: fix panic when repo path contains spaces by switching to CARGO_ENCODED_RUSTFLAGS) - #158256 (Avoid parser panics bubbling out to proc macros) - #158081 (trait-system: Recover deferred closure calls after errors) - #158323 (rustc: improve diagnostics for file-open failures) - #158327 (Move attribute and keyword docs from `std` to `core`) - #158468 (Include default-stability info in rustdoc JSON.)
|
@bors r- |
|
This pull request was unapproved. This PR was contained in a rollup (#158558), which was unapproved. |
a13e2da to
b81e3b2
Compare
Emit more targeted diagnostics when an input file cannot be opened, including dedicated messages for common error kinds and typo suggestions for missing files. Add run-make tests covering NotFound, PermissionDenied, IsADirectory, and non-existent directory cases. Signed-off-by: Usman Akinyemi <usmanakinyemi202@gmail.com>
b81e3b2 to
f3e42b9
Compare
|
The job Click to see the possible cause of the failure (guessed by this bot) |
|
You will have to change the test to have two flavors, one for windows and one for linux (so you'll end up with two different stderr files), as well as remove the comments in the test that rewrite the stderr output. |
View all comments
Fix: #156070