Skip to content

Commit 4926f1a

Browse files
downloadCargoPackageFromGit: use cargo package for creating the file list (resubmit) (#976)
This avoids pitfalls with `rg` misinterpreting include/exclude rules, because it uses THE SAME logic `cargo` uses for .crate files. Because it uses Cargo itself. Issues with lock file generation are side-stepped by using `--exclude-lockfile`, which I verified completely disables the check that the lockfile is up-to-date. --------- Co-authored-by: Vika Shleina <vsh@nyantec.com>
1 parent 2510f2c commit 4926f1a

2 files changed

Lines changed: 20 additions & 21 deletions

File tree

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
1111
* `mkDummySrc` can now pass the `cleanCargoTomlFilter` argument to `cleanCargoToml`
1212
* `craneLib.filters` exposes `cargoTomlAggressive`, `cargoTomlConservative`, and
1313
`cargoTomlDefault` for composition of custom filters for `cleanCargoToml`
14+
* `downloadCargoPackageFromGit` now uses `cargo package -l` for generating the file
15+
list, resulting in more accurate include/exclude rule interpretation. This fixes
16+
builds in some dependencies, notably `aws-lc-rs` from git.
1417

1518
## [0.23.0] - 2026-01-13
1619

lib/downloadCargoPackageFromGit.nix

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
craneUtils,
55
jq,
66
pkgsBuildBuild,
7-
remarshal,
8-
ripgrep,
97
}:
108

119
let
@@ -61,11 +59,9 @@ stdenv.mkDerivation {
6159
cargo
6260
craneUtils
6361
jq
64-
remarshal
65-
ripgrep
6662
];
6763

68-
installPhase = ''
64+
installPhase = /* bash */ ''
6965
runHook preInstall
7066
7167
mkdir -p $out
@@ -105,21 +101,8 @@ stdenv.mkDerivation {
105101
(
106102
cd "$(dirname "$cargoToml")"
107103
108-
# NB: we tell ripgrep to ignore any ignore files (via -uuu) since we are manually
109-
# applying the includes/excludes defined in Cargo.toml. Since this is a fresh git
110-
# checkout, it will not include any files listed in .gitignore anyway!
111-
crateFiles="$(rg -uuu --follow --files --ignore-file=<(
112-
remarshal -i "$cargoToml" -if toml -of json \
113-
| jq -r '.package | if has("include") then .include | map("!\(.)" | sub("^!!"; "")) else .exclude // [] end| .[]?'
114-
echo '!/Cargo.toml'
115-
# Always excluded
116-
echo '/target'
117-
# Always exclude subpackages (directories with `Cargo.toml`)
118-
# mindepth 2 because ./Cargo.toml counts as a depth of 1
119-
find ./ -mindepth 2 -name Cargo.toml -print0 \
120-
| xargs -0 -r -n1 dirname \
121-
| sed 's|^\.||'
122-
) | sort)"
104+
# Use `cargo package` to interpret the include/exclude rules
105+
crateFiles="$(cargo package --offline --exclude-lockfile -l | grep -v -e "^Cargo.toml.orig" | sort)"
123106
124107
(
125108
cd "$dest"
@@ -130,7 +113,20 @@ stdenv.mkDerivation {
130113
| xargs -0 -r mkdir -p
131114
)
132115
tr '\n' '\0' <<<"$crateFiles" \
133-
| xargs -0 -r "-P''${NIX_BUILD_CORES:-1}" -I FILE cp -L FILE "$dest/FILE"
116+
| xargs -0 -r "-P''${NIX_BUILD_CORES:-1}" -I FILE cp -L FILE "$dest/FILE" \
117+
|| (
118+
# NB: Sometimes cargo will list out certain files (e.g. README.md which is meant to
119+
# refer to the one at the root of the repo) even if they don't actually exist in the
120+
# crate subdirectory, so we should ignore any such files which fail to copy, but
121+
# explicitly warn about them for debugging purposes. This also side steps any issues
122+
# from legitimately broken symlinks (e.g. we cannot blindly resolve all symlinks
123+
# because some crates intentionally have broken symlinks for tests etc.).
124+
#
125+
# At the very least if we get this wrong, downstream consumers can always patch this
126+
# derivation to fixup the files beforehand and hopefully do the right thing...
127+
echo 'NOTE: ignoring the following files (listed by cargo) that failed to copy:'
128+
comm -23 - <<<"$crateFiles" <(find . -type f -printf "%P\n" | sort) | awk '{ print "'"$(pwd)"'/" $0 }'
129+
)
134130
)
135131
136132
echo '{"files":{}, "package":null}' > "$dest/.cargo-checksum.json"

0 commit comments

Comments
 (0)