fix incorrect resolution of aliased git dependencies #391
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
In a case where there are multiple references to the same package name via git, crate2nix does not correctly match locked packages to dependencies. For example consider these dependencies,
The same dependency is referenced at two different git revisions, each with its own alias. (I have essentially the same thing in a current work project.) The lock file for this project contains two locked packages with IDs
ludndev-hello-world 0.1.0
andludndev-hello-world 0.1.1
.Crate2nix should match the dependency alias
hello
toludndev-hello-world 0.1.1
, and should matchhello_v010
toludndev-hello-world 0.1.0
. But what actually happens is that it arbitrarily picks one locked package to match to both dependencies. This is because currently package resolution is done according to package name and dependency version range. In this case the package names are the same, and for git dependencies the inferred version range is always*
.The result is a rustc command that repeats the same
--extern
flag twice for one of the aliases, and omits the other alias:In the above case it picked one alias,
hello
, included that as an extern twice matched to the wrong locked package version. Then you get an error like this,Here is what the correct rustc command looks like:
This PR fixes the problem by resolving packages by matching the package source with the dependency source. Source comparison is only done if resolving by version results in multiple matches.
If there are still multiple matches after filtering by version range and by source I included a panic. I think failing fast is the right outcome since the alternative could be building with the wrong package. I haven't seen any cases where this would actually come up. I could make changes to propagate a
Result
instead of panicking if that is desired.The source comparison is a little hacky. It is necessary because the
cargo_metadata
library doesn't provide a cleaner way to match dependencies to packages in this situation. I believe thecargo
library handles this case better. From what I can seecargo
supplies a package ID for bothDependency
andPackage
, and that might be an unambiguous option for dependency resolution. So switching tocargo
might be a better fix. The downsides are a) that is a much bigger change, and b) cargo is a very large dependency.