From b2aa19efe7ffacd5ba9642354ee51f2eb6a10d07 Mon Sep 17 00:00:00 2001 From: Stefan Junker Date: Sun, 24 Oct 2021 01:01:07 +0200 Subject: [PATCH] rust: find nested packages in git repositories this mimics the heuristic cargo uses for finding crates in their git repositories ([cargo-issue-1462]). [cargo-issue-1462]: https://github.com/rust-lang/cargo/issues/1462 --- pkgs/build-support/rust/import-cargo-lock.nix | 29 +++++++++++++------ 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/pkgs/build-support/rust/import-cargo-lock.nix b/pkgs/build-support/rust/import-cargo-lock.nix index fe070e9638d4..fd3608741ea3 100644 --- a/pkgs/build-support/rust/import-cargo-lock.nix +++ b/pkgs/build-support/rust/import-cargo-lock.nix @@ -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