Skip to content

Commit

Permalink
Merge pull request #142699 from steveeJ-forks/fix-import-cargo-lock
Browse files Browse the repository at this point in the history
rust: find nested packages in git repositories
  • Loading branch information
andir authored Nov 17, 2021
2 parents 28cba4d + 445510e commit 48de1b8
Show file tree
Hide file tree
Showing 6 changed files with 701 additions and 9 deletions.
29 changes: 20 additions & 9 deletions pkgs/build-support/rust/import-cargo-lock.nix
Original file line number Diff line number Diff line change
Expand Up @@ -125,20 +125,31 @@ let
};
in runCommand "${pkg.name}-${pkg.version}" {} ''
tree=${tree}
if grep --quiet '\[workspace\]' "$tree/Cargo.toml"; then
# If the target package is in a workspace, find the crate path
# using `cargo metadata`.
crateCargoTOML=$(${cargo}/bin/cargo metadata --format-version 1 --no-deps --manifest-path $tree/Cargo.toml | \
${jq}/bin/jq -r '.packages[] | select(.name == "${pkg.name}") | .manifest_path')
# If the target package is in a workspace, or if it's the top-level
# crate, we should find the crate path using `cargo metadata`.
crateCargoTOML=$(${cargo}/bin/cargo metadata --format-version 1 --no-deps --manifest-path $tree/Cargo.toml | \
${jq}/bin/jq -r '.packages[] | select(.name == "${pkg.name}") | .manifest_path')
# If the repository is not a workspace the package might be in a subdirectory.
if [[ -z $crateCargoTOML ]]; then
for manifest in $(find $tree -name "Cargo.toml"); do
echo Looking at $manifest
crateCargoTOML=$(${cargo}/bin/cargo metadata --format-version 1 --no-deps --manifest-path "$manifest" | ${jq}/bin/jq -r '.packages[] | select(.name == "${pkg.name}") | .manifest_path' || :)
if [[ ! -z $crateCargoTOML ]]; then
tree=$(dirname $crateCargoTOML)
else
>&2 echo "Cannot find path for crate '${pkg.name}-${pkg.version}' in the Cargo workspace in: $tree"
exit 1
break
fi
done
if [[ -z $crateCargoTOML ]]; then
>&2 echo "Cannot find path for crate '${pkg.name}-${pkg.version}' in the tree in: $tree"
exit 1
fi
fi
echo Found crate ${pkg.name} at $crateCargoTOML
tree=$(dirname $crateCargoTOML)
cp -prvd "$tree/" $out
chmod u+w $out
Expand Down
1 change: 1 addition & 0 deletions pkgs/build-support/rust/test/import-cargo-lock/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
basicDynamic = callPackage ./basic-dynamic { };
gitDependency = callPackage ./git-dependency { };
gitDependencyRev = callPackage ./git-dependency-rev { };
gitDependencyRevNonWorkspaceNestedCrate = callPackage ./git-dependency-rev-non-workspace-nested-crate { };
gitDependencyTag = callPackage ./git-dependency-tag { };
gitDependencyBranch = callPackage ./git-dependency-branch { };
maturin = callPackage ./maturin { };
Expand Down
Loading

0 comments on commit 48de1b8

Please sign in to comment.