`cargo install` from a directory-source-replaced registry fails for dependency-free crates
Problem
cargo installing from a directory source fails for binary packages with no dependencies. It succeeds if the package has a dependency. This is a stable-to-stable regression first present in cargo 1.96.0.
Steps
$CARGO_HOME/config.toml:
[source.local]
directory = "/tmp/cargo-registry"
[source.crates-io]
replace-with = "local"
mkdir -p /tmp/cargo-registry && cd /tmp/cargo-registrycargo new --lib --vcs=none my-lib && cargo new --bin --vcs=none my-binecho '{"files":{}}' > my-bin/.cargo-checksum.json(and for my-lib)cargo +1.95.0 install my-bin && cargo uninstall my-bin(succeeds)cargo +1.96.0 install my-bin && cargo uninstall my-bin(fails)- edit
my-bin/Cargo.toml:
[dependencies]
+my-lib = "0.1.0"
cargo +1.96.0 install my-bin && cargo uninstall my-bin(succeeds)
Possible Solution(s)
LLM analysis
DirectorySource::download() (src/cargo/sources/directory.rs) looks up self.packages without calling update() — unlike query(), which lazily updates first. In cargo install, the package selected by select_dep_pkg is downloaded a second time through a fresh source instance during the compile step ("Installing my-bin v0.1.0" prints before the failure, so selection works). A dependency-free crate never triggers query() on that fresh instance, so download() hits an empty map. With ≥1 dependency, resolution queries the source and populates the map first — which is exactly the dep-free/dep-full split the script demonstrates. The async Source/RegistryData migration touching directory.rs landed in the regression window (April 2026, between 1.95.0 and 1.96.0), e.g. "Make download and finish_download async" (2026-04-18) — worth a bisect from there.
Notes
LLM disclaimer: I ran into this issue and did initial repro minimization using an LLM. The reproduction provided here and issue report is non-LLM.
Version
cargo 1.97.1 (c980f4866 2026-06-30)
release: 1.97.1
commit-hash: c980f4866141969fab6254a680546a277789d6f0
commit-date: 2026-06-30
host: x86_64-unknown-linux-gnu
libgit2: 1.9.2 (sys:0.20.4 vendored)
libcurl: 8.20.0-DEV (sys:0.4.88+curl-8.20.0 vendored ssl:OpenSSL/3.6.2)
ssl: OpenSSL 3.6.2 7 Apr 2026
os: Ubuntu 26.4.0 (resolute) [64-bit]
@rustbot label +regression-from-stable-to-stable
This commit is from #16745, Use async for registry network operations. The likely regression is that DirectorySource::update() was moved behind the async query() path when block_until_ready() was removed, while DirectorySource::download() continued to read self.packages without ensuring that the source had been initialized. A dependency-free package can therefore be selected successfully, but fail during the later download step when a fresh source instance has an empty package map.
Could the Cargo team confirm whether this issue is accepted and whether initializing the directory source from download() is the preferred fix?
I believe this also affects binary crates with at least one vendored dependency that is in turn dependency-free. Had it happen with a forked version of ar that is then consumed via Git, cargo install fails with:
error: failed to compile `dragonfire v0.1.4 (E:\cerbero-sources\sources\build-tools\dragonfire-0.1.4)`, intermediate artifacts can be found at `E:/cerbero-sources/sources/build-tools/dragonfire-0.1.4/_builddir`.
To reuse those artifacts with a future compilation, set the environment variable `CARGO_BUILD_BUILD_DIR` to that path.
Caused by:
failed to load source for dependency `ar`
Caused by:
unable to update https://codeberg.org/amyspark/rust-ar.git?rev=f179dd2a98bb7587ff94adbfc3864b2fa92067f2
Caused by:
the source git+https://codeberg.org/amyspark/rust-ar.git?rev=f179dd2a98bb7587ff94adbfc3864b2fa92067f2 requires a lock file to be present first before it can be
used against vendored source code
remove the source replacement configuration, generate a lock file, and then
restore the source replacement configuration to continue the build
while a properly updated Cargo.lock is already in place.
Using the workaround of --locked explained in https://github.com/rust-lang/cargo/pull/17388 works, and makes me feel like the two bugs are somewhat linked.
This issue originates from using bool updated and functions directly accessing packages variable which can be uninitialized. Rather than simply sprinkling if !self.updated.get() { self.update()?; } into download() and verify(), we can make direct uninitialized access impossible by design:
My approach:
- Update DirectorySource's
packages: RefCell<HashMap<PackageId, (Package, Checksum)>>->packages: RefCell<Option<HashMap<PackageId, (Package, Checksum)>>>So that anybody tries to accessdirectorySource.packagedirectly (out of habit) gets an compiler error. - Now we will remove update function and add
.packages()method which will immediately return ifpackage.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 theupdated:bool.
If this approach looks good to the team, I'd be happy to submit a PR along with a regression test