Skip to content

Fix: cargo installing from a directory source fails for binary packages with no dependencies. - #17552

Merged
weihanglo merged 2 commits into
rust-lang:masterfrom
Suryansh-Dey:option
Oct 8, 2026
Merged

weihanglo merged 2 commits into
rust-lang:masterfrom
Suryansh-Dey:option

Conversation

@Suryansh-Dey

@Suryansh-Dey Suryansh-Dey commented Oct 6, 2026 •

Copy link
Copy Markdown
Contributor

What does this PR try to resolve?

closes #17458

The issue originates from using updated:bool and functions directly accessing packages variable which can be uninitialized.

if !self.updated.get() {
self.update()?;
}
let packages = self.packages.borrow();

Rather than spreading if !self.updated.get() { self.update()?; } into download() and verify(), we can make direct uninitialized access impossible by design:

Approach:

  • Update DirectorySource's packages: RefCell<HashMap<PackageId, (Package, Checksum)>> -> packages: RefCell<Option<HashMap<PackageId, (Package, Checksum)>>> So that anybody tries to access directorySource.package directly (out of habit) gets an compiler error.
  • Replace update() method with .packages() method which will immediately return if package.is_some() else it will populate packages and return it (lazy behavior). This way we will not need this check everywhere fixing the problem forever by just a refactor. Also removes the updated:bool.

If team doesn't like this approach I am ready for changes.

How to test and review this PR?

Run cargo test --test testsuite directory::install_without_dependencies

Manual: Steps metioned in this issue

@rustbot rustbot added A-directory-source Area: directory sources (vendoring) S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Oct 6, 2026
@rustbot

rustbot commented Oct 6, 2026

Copy link
Copy Markdown
Collaborator

r? @epage

rustbot has assigned @epage.
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: @Muscraft, @epage, @weihanglo
  • @Muscraft, @epage, @weihanglo expanded to Muscraft, epage, weihanglo
  • Random selection from Muscraft, epage, weihanglo

@epage

epage commented Oct 6, 2026

Copy link
Copy Markdown
Contributor

r? @Muscraft

@rustbot rustbot assigned Muscraft and unassigned epage Oct 6, 2026
@Suryansh-Dey
Suryansh-Dey force-pushed the option branch 2 times, most recently from a662fd4 to 0091e02 Compare October 6, 2026 15:23

@weihanglo weihanglo left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Looks good! I read the history and yes it is indeed a regression that block_until_ready becomes lazy so not all sources kind are pre-filled. Thanks for the fix!

View changes since this review

Comment thread src/sources/directory.rs Outdated
self.root.display()
)
})?;
fn packages(&self) -> CargoResult<Ref<'_, HashMap<PackageId, (Package, Checksum)>>> {

@weihanglo weihanglo Oct 7, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

What do you think of restructuring it like this?
It could keep the loading logic at its original indentation, so code diff gets much smaller.

  • Keep the package loading logic in fn update, perhaps rename to load_packages and returning the hash map.
  • Change RefCell to OnceCell, as the map is never mutated once initialized. This should be fine because DirectorySource::invalidate_cache never really invalidates loaded packages.
    If we ever need that, it is an impl detail we can change.
  • fn packages becomes a thin accessor:
    fn packages(&self) -> CargoResult<&HashMap<...>> {
        self.packages.try_borrow_with(|| self.load_packages())
    }
    (try_borrow_with comes from OnceExt btw)

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.

I also thought about this but was afraid of changing Ref to Once. Done!

@weihanglo weihanglo left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thank you for working with us!

View changes since this review

@weihanglo
weihanglo added this pull request to the merge queue Oct 8, 2026
@Suryansh-Dey

Copy link
Copy Markdown
Contributor Author

Thank you for working with us!

View changes since this review

I also learnt a lot from you, thank you.

Merged via the queue into rust-lang:master with commit 33f504c Oct 8, 2026
29 checks passed
@rustbot rustbot removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Oct 8, 2026
@Suryansh-Dey
Suryansh-Dey deleted the option branch October 8, 2026 04:13
rust-bors Bot pushed a commit to rust-lang/rust that referenced this pull request Oct 14, 2026
Update cargo submodule

14 commits in 29c5daa1afc262323364e039bb70c83372cc702c..3960fa69bb617e3d93c4ddc087c1b32bf12ad907
2026-10-07 22:13:48 +0000 to 2026-10-12 19:29:05 +0000
- refactor: modernize some API usages for 1.99 (rust-lang/cargo#17584)
- Removed some dead workspace tests (rust-lang/cargo#17582)
- Bypass rustup proxy when RUSTUP_TOOLCHAIN is an absolute path (rust-lang/cargo#17562)
- chore: Add an LLM disclosure to the PR template (rust-lang/cargo#17580)
- Restrict workspace-relative dep-info to workspace packages (rust-lang/cargo#17575)
- fix(layout): exclude a build-dir created as a parent from backups (rust-lang/cargo#17560)
- Avoid absolute paths in fingerprints if they are relative to the workspace root (rust-lang/cargo#17573)
- chore(deps): update msrv (3 versions) to v1.97 (rust-lang/cargo#17572)
- fix(job): Allow explicit child process breakaway on Windows (rust-lang/cargo#17527)
- fix(git): Add future-incompat warning in prep for removing auto-detection (rust-lang/cargo#17568)
- fix(config): Don't process includes for `credentials.toml` files (rust-lang/cargo#17569)
- doc(builtin-dep): Document the builtin package ID spec format (rust-lang/cargo#17564)
- test(mock-std): Set stable crate versions to 0.0.0 (rust-lang/cargo#17565)
- Fix: cargo installing from a directory source fails for binary packages with no dependencies. (rust-lang/cargo#17552)

r? ghost
rust-bors Bot pushed a commit to rust-lang/rust that referenced this pull request Oct 14, 2026
Update cargo submodule

14 commits in 29c5daa1afc262323364e039bb70c83372cc702c..3960fa69bb617e3d93c4ddc087c1b32bf12ad907
2026-10-07 22:13:48 +0000 to 2026-10-12 19:29:05 +0000
- refactor: modernize some API usages for 1.99 (rust-lang/cargo#17584)
- Removed some dead workspace tests (rust-lang/cargo#17582)
- Bypass rustup proxy when RUSTUP_TOOLCHAIN is an absolute path (rust-lang/cargo#17562)
- chore: Add an LLM disclosure to the PR template (rust-lang/cargo#17580)
- Restrict workspace-relative dep-info to workspace packages (rust-lang/cargo#17575)
- fix(layout): exclude a build-dir created as a parent from backups (rust-lang/cargo#17560)
- Avoid absolute paths in fingerprints if they are relative to the workspace root (rust-lang/cargo#17573)
- chore(deps): update msrv (3 versions) to v1.97 (rust-lang/cargo#17572)
- fix(job): Allow explicit child process breakaway on Windows (rust-lang/cargo#17527)
- fix(git): Add future-incompat warning in prep for removing auto-detection (rust-lang/cargo#17568)
- fix(config): Don't process includes for `credentials.toml` files (rust-lang/cargo#17569)
- doc(builtin-dep): Document the builtin package ID spec format (rust-lang/cargo#17564)
- test(mock-std): Set stable crate versions to 0.0.0 (rust-lang/cargo#17565)
- Fix: cargo installing from a directory source fails for binary packages with no dependencies. (rust-lang/cargo#17552)

r? ghost
@rustbot rustbot added this to the 1.101.0 milestone Oct 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-directory-source Area: directory sources (vendoring)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

cargo install from a directory-source-replaced registry fails for dependency-free crates

5 participants