Skip to content

fix(package): improve error reporting for unreadable directories#16517

Open
arin-r wants to merge 1 commit intorust-lang:masterfrom
arin-r:fix/package-unreadable-error
Open

fix(package): improve error reporting for unreadable directories#16517
arin-r wants to merge 1 commit intorust-lang:masterfrom
arin-r:fix/package-unreadable-error

Conversation

@arin-r
Copy link

@arin-r arin-r commented Jan 15, 2026

What does this PR try to resolve?

This PR addresses the issue where cargo package crashes on unreadable directories in the project root (common in Docker workflows) and cargo package --list misleadingly lists them as valid.

As discussed with @epage in #16465, this change focuses on better error reporting rather than changing build behavior. It introduces two improvements:

  1. Hints on Failure: Adds a helpful hint to the "Permission denied" error in prepare_archive (used by cargo package), suggesting the user add the file to .gitignore or exclude.
  2. Warnings on Listing: Adds a warning to cargo package --list when it encounters an unreadable directory, ensuring the user is alerted to potential issues without breaking the command. This check mirrors the archiver's access logic to ensure consistency across platforms.

Note: The wording of the error hint is a proposal and can be adjusted during review to match project standards.

How to test and review this PR?

You can verify these changes by creating a dummy project with an unreadable directory:

cargo new --lib test_proj
cd test_proj
mkdir unreadable_dir
chmod 000 unreadable_dir

1. Test cargo package --list warning:
Run cargo package --list and verify that it prints a warning to stderr about unreadable_dir instead of silently listing it.

2. Test cargo package error hint:
Run cargo package and verify that the build failure message now includes the new hint suggesting exclusion via .gitignore or exclude.

Fixes #16465

@rustbot rustbot added Command-package S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jan 15, 2026
@rustbot
Copy link
Collaborator

rustbot commented Jan 15, 2026

r? @weihanglo

rustbot has assigned @weihanglo.
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

Comment on lines +7899 to +7902
// Unix-only test because Windows has different permission handling
#[cfg(unix)]
#[cargo_test]
fn unreadable_directory_error_message() {
Copy link
Contributor

Choose a reason for hiding this comment

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

We generally ask that PRs be split into at least two commits

  1. Adding tests that reproduce the existing behavior
  2. The bugfix with updates to the tests to show the new behavior

See also https://doc.crates.io/contrib/process/working-on-cargo.html#submitting-a-pull-request

Copy link
Contributor

Choose a reason for hiding this comment

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

Also

It introduces two improvements:

That is a good hint that that should be at least two separate commits, if not two PRs.

@epage
Copy link
Contributor

epage commented Jan 15, 2026

This PR addresses the issue where cargo package crashes on unreadable directories in the project root (common in Docker workflows) and cargo package --list misleadingly lists them as valid.

It would be inaccurate to describe this as a crash. That could apply to a non-assert panic but not an error like this.

// If this is a permission denied error, add a helpful hint
if e.kind() == std::io::ErrorKind::PermissionDenied {
anyhow::Error::from(e).context(format!(
"failed to open for archiving: `{}`\n\nNote: If this is a local artifact, add it to .gitignore or the 'exclude' list in Cargo.toml.",
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
"failed to open for archiving: `{}`\n\nNote: If this is a local artifact, add it to .gitignore or the 'exclude' list in Cargo.toml.",
"failed to open for archiving: `{}`\n\nnote: to exclude local artifacts, add it to `.gitignore` or `Cargo.toml`s 'package.exclude'",

Comment on lines +913 to +922
// If this is a permission denied error, add a helpful hint
if e.kind() == std::io::ErrorKind::PermissionDenied {
anyhow::Error::from(e).context(format!(
"failed to open for archiving: `{}`\n\nNote: If this is a local artifact, add it to .gitignore or the 'exclude' list in Cargo.toml.",
disk_path.display()
))
} else {
anyhow::Error::from(e).context(format!(
"failed to open for archiving: `{}`",
disk_path.display()
Copy link
Contributor

Choose a reason for hiding this comment

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

generally how we structure these cases is

let suggestion = if condition {
    // ...
} else {
    "".to_owned();
};
anyhow::Error::from(e).context(format!(
                            "failed to open for archiving: `{}`{}",
                            disk_path.display(),
                            suggestion,
                        ))

which avoids repeating the error message.

if let Err(e) = File::open(path) {
// Emit a warning but continue processing
let _ = ws.gctx().shell().warn(format!(
"cannot access `{}`: {}. `cargo package` will fail.",
Copy link
Contributor

Choose a reason for hiding this comment

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

While I understand where you are coming from, I'm not thrilled with the way this message is being communicated atm and find it easier for now to remove it

Suggested change
"cannot access `{}`: {}. `cargo package` will fail.",
"cannot access `{}`: {}",

Copy link
Member

Choose a reason for hiding this comment

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

Do we feel that this is worth a richer annotate-snippets style report? I see that a warning without action item is somehow confusing.

Copy link
Contributor

Choose a reason for hiding this comment

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

True, the hand written note on the error can be made an annotate snippets note here.

I'd recommend making making the path an Origin while at it.

This adds a warning to 'cargo package --list' when encountering unreadable directories and appends a hint to 'cargo package' permission errors suggesting exclusion via .gitignore.
@ranger-ross
Copy link
Member

Hi @arin-r just checking in, do you plan to continue pursuing this PR?

@rustbot author

@rustbot rustbot added S-waiting-on-author Status: The marked PR is awaiting some action (such as code changes) from the PR author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Mar 4, 2026
@rustbot
Copy link
Collaborator

rustbot commented Mar 4, 2026

Reminder, once the PR becomes ready for a review, use @rustbot ready.

@rustbot
Copy link
Collaborator

rustbot commented Mar 6, 2026

☔ The latest upstream changes (possibly #16688) made this pull request unmergeable. Please resolve the merge conflicts.

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

Labels

Command-package S-waiting-on-author Status: The marked PR is awaiting some action (such as code changes) from the PR author.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

cargo package doesn't properly handle unreadable directories in the root

5 participants