Skip to content

Commit c4b53c9

Browse files
committed
Fix a bug loading v2 lockfiles
This commit fixes a bug in Cargo where after `DefaultBranch` and `Branch("master")` are considered distinct no v2 lockfile would by default match a dependency specification with the `branch = 'master'` annotation. A feature of the v2 lockfile, however, is that no mention of a branch is equivalent to the `master` branch. The bug here is fixed by updating the code which registers a previous lock file with our `PackageRegistry`. This code registers nodes, only with v2 lock files, for both default and `master` branch git dependencies. This way requests for either one will get matched now that they're considered distinct.
1 parent 7dd9872 commit c4b53c9

File tree

1 file changed

+29
-3
lines changed

1 file changed

+29
-3
lines changed

src/cargo/ops/resolve.rs

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,12 @@
1313
use crate::core::compiler::{CompileKind, RustcTargetData};
1414
use crate::core::registry::PackageRegistry;
1515
use crate::core::resolver::features::{FeatureResolver, ForceAllTargets, ResolvedFeatures};
16-
use crate::core::resolver::{self, HasDevUnits, Resolve, ResolveOpts};
16+
use crate::core::resolver::{self, HasDevUnits, Resolve, ResolveOpts, ResolveVersion};
1717
use crate::core::summary::Summary;
1818
use crate::core::Feature;
19-
use crate::core::{PackageId, PackageIdSpec, PackageSet, Source, SourceId, Workspace};
19+
use crate::core::{
20+
GitReference, PackageId, PackageIdSpec, PackageSet, Source, SourceId, Workspace,
21+
};
2022
use crate::ops;
2123
use crate::sources::PathSource;
2224
use crate::util::errors::{CargoResult, CargoResultExt};
@@ -597,7 +599,31 @@ fn register_previous_locks(
597599
.deps_not_replaced(node)
598600
.map(|p| p.0)
599601
.filter(keep)
600-
.collect();
602+
.collect::<Vec<_>>();
603+
604+
// In the v2 lockfile format and prior the `branch=master` dependency
605+
// directive was serialized the same way as the no-branch-listed
606+
// directive. Nowadays in Cargo, however, these two directives are
607+
// considered distinct and are no longer represented the same way. To
608+
// maintain compatibility with older lock files we register locked nodes
609+
// for *both* the master branch and the default branch.
610+
//
611+
// Note that this is only applicable for loading older resolves now at
612+
// this point. All new lock files are encoded as v3-or-later, so this is
613+
// just compat for loading an old lock file successfully.
614+
if resolve.version() <= ResolveVersion::V2 {
615+
let source = node.source_id();
616+
if let Some(GitReference::DefaultBranch) = source.git_reference() {
617+
let new_source =
618+
SourceId::for_git(source.url(), GitReference::Branch("master".to_string()))
619+
.unwrap()
620+
.with_precise(source.precise().map(|s| s.to_string()));
621+
622+
let node = node.with_source_id(new_source);
623+
registry.register_lock(node, deps.clone());
624+
}
625+
}
626+
601627
registry.register_lock(node, deps);
602628
}
603629

0 commit comments

Comments
 (0)