Skip to content

rustc: improve diagnostics for file-open failures#158323

Open
Unique-Usman wants to merge 1 commit into
rust-lang:mainfrom
Unique-Usman:ua/invalid_source_file
Open

rustc: improve diagnostics for file-open failures#158323
Unique-Usman wants to merge 1 commit into
rust-lang:mainfrom
Unique-Usman:ua/invalid_source_file

Conversation

@Unique-Usman

@Unique-Usman Unique-Usman commented Jun 23, 2026

Copy link
Copy Markdown
Contributor

@rustbot

rustbot commented Jun 23, 2026

Copy link
Copy Markdown
Collaborator

The parser was modified, potentially altering the grammar of (stable) Rust
which would be a breaking change.

cc @fmease

@rustbot rustbot added A-run-make Area: port run-make Makefiles to rmake.rs S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jun 23, 2026
@rustbot

rustbot commented Jun 23, 2026

Copy link
Copy Markdown
Collaborator

r? @nnethercote

rustbot has assigned @nnethercote.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: compiler
  • compiler expanded to 73 candidates
  • Random selection from 21 candidates

@Unique-Usman

Copy link
Copy Markdown
Contributor Author

cc: @estebank

@nnethercote nnethercote left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Generally looks good. A couple of nits below, and I will give @estebank some more time to comment.

View changes since this review

Comment thread compiler/rustc_parse/src/lib.rs Outdated
Comment thread compiler/rustc_parse/src/lib.rs Outdated
Comment thread tests/run-make/input-file-errors/rmake.rs Outdated
@Unique-Usman Unique-Usman requested a review from nnethercote June 24, 2026 07:25
@Unique-Usman Unique-Usman force-pushed the ua/invalid_source_file branch from dae50a7 to 2f4edae Compare June 24, 2026 08:22
@UnkwUsr

UnkwUsr commented Jun 24, 2026

Copy link
Copy Markdown

Edge case: permission denied for directory. This PR reports "permission denied when opening file". For comparison, ls from gnu-coreutils handles such case correctly.

$ 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 denied

Though 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

@estebank estebank left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

View changes since this review

Comment thread compiler/rustc_parse/src/lib.rs Outdated
parent.join(suggestion.as_str()).display().to_string()
};
err.help(format!(
"you might have meant to open `{}`: `rustc {}`",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

noted.

Comment thread compiler/rustc_parse/src/lib.rs Outdated
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("."));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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 :)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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("."),
};

@Unique-Usman Unique-Usman force-pushed the ua/invalid_source_file branch from 2f4edae to a13e2da Compare June 25, 2026 17:54
@Unique-Usman Unique-Usman requested a review from estebank June 25, 2026 22:11
@nnethercote

Copy link
Copy Markdown
Contributor

@bors r+ rollup

@rust-bors

rust-bors Bot commented Jun 29, 2026

Copy link
Copy Markdown
Contributor

📌 Commit a13e2da has been approved by nnethercote

It is now in the queue for this repository.

@rust-bors rust-bors Bot added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jun 29, 2026
jhpratt added a commit to jhpratt/rust that referenced this pull request Jun 29, 2026
…e, r=nnethercote

rustc: improve diagnostics for file-open failures

Fix: [rust-lang#156070](rust-lang#156070)
rust-bors Bot pushed a commit that referenced this pull request Jun 29, 2026
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.)
@jhpratt

jhpratt commented Jun 29, 2026

Copy link
Copy Markdown
Member

@bor r-

#158558 (comment)

@jhpratt

jhpratt commented Jun 29, 2026

Copy link
Copy Markdown
Member

@bors r-

@rust-bors rust-bors Bot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Jun 29, 2026
@rust-bors

rust-bors Bot commented Jun 29, 2026

Copy link
Copy Markdown
Contributor

This pull request was unapproved.

This PR was contained in a rollup (#158558), which was unapproved.

View changes since this unapproval

@Unique-Usman Unique-Usman force-pushed the ua/invalid_source_file branch from a13e2da to b81e3b2 Compare June 29, 2026 21:59
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>
@Unique-Usman Unique-Usman force-pushed the ua/invalid_source_file branch from b81e3b2 to f3e42b9 Compare June 29, 2026 22:05
@Unique-Usman Unique-Usman requested a review from nnethercote June 29, 2026 22:12
@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jun 29, 2026
@rust-log-analyzer

Copy link
Copy Markdown
Collaborator

The job aarch64-gnu-llvm-21-1 failed! Check out the build log: (web) (plain enhanced) (plain)

Click to see the possible cause of the failure (guessed by this bot)
---- [ui] tests/ui/modules/path-no-file-name.rs stdout ----
Saved the actual stderr to `/checkout/obj/build/aarch64-unknown-linux-gnu/test/ui/modules/path-no-file-name/path-no-file-name.stderr`
diff of stderr:

- error: permission denied when opening file `$DIR/.`
+ error: `$DIR/.` is a directory
2   --> $DIR/path-no-file-name.rs:5:1
3    |
4 LL | mod m;

Note: some mismatched output was normalized before being compared
- error: `/checkout/tests/ui/modules/.` is a directory
-   --> /checkout/tests/ui/modules/path-no-file-name.rs:5:1
+ error: `$DIR/.` is a directory


The actual stderr differed from the expected stderr
To update references, rerun the tests and pass the `--bless` flag
To only update this specific test, also pass `--test-args modules/path-no-file-name.rs`

error: 1 errors occurred comparing output.
status: exit status: 1
command: env -u RUSTC_LOG_COLOR RUSTC_ICE="0" RUST_BACKTRACE="short" "/checkout/obj/build/aarch64-unknown-linux-gnu/stage2/bin/rustc" "/checkout/tests/ui/modules/path-no-file-name.rs" "-Zthreads=1" "-Zsimulate-remapped-rust-src-base=/rustc/FAKE_PREFIX" "-Ztranslate-remapped-path-to-local-path=no" "-Z" "ignore-directory-in-diagnostics-source-blocks=/cargo" "-Z" "ignore-directory-in-diagnostics-source-blocks=/checkout/vendor" "--sysroot" "/checkout/obj/build/aarch64-unknown-linux-gnu/stage2" "--target=aarch64-unknown-linux-gnu" "--check-cfg" "cfg(test,FALSE)" "--error-format" "json" "--json" "future-incompat" "-Ccodegen-units=1" "-Zui-testing" "-Zdeduplicate-diagnostics=no" "-Zwrite-long-types-to-disk=no" "-Cstrip=debuginfo" "--emit" "metadata" "-C" "prefer-dynamic" "--out-dir" "/checkout/obj/build/aarch64-unknown-linux-gnu/test/ui/modules/path-no-file-name" "-A" "unused" "-W" "unused_attributes" "-A" "internal_features" "-A" "incomplete_features" "-A" "unused_parens" "-A" "unused_braces" "-Crpath" "-Cdebuginfo=0" "-Lnative=/checkout/obj/build/aarch64-unknown-linux-gnu/native/rust-test-helpers"
stdout: none
--- stderr -------------------------------
error: `/checkout/tests/ui/modules/.` is a directory
##[error]  --> /checkout/tests/ui/modules/path-no-file-name.rs:5:1
   |
LL | mod m; //~ ERROR couldn't read
   | ^^^^^^

error: aborting due to 1 previous error
------------------------------------------

@estebank

Copy link
Copy Markdown
Contributor

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-run-make Area: port run-make Makefiles to rmake.rs S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Improve output when rustc couldn't find the source code file (suggest typo, improve wording)

7 participants