Use checksums for recreating crates.io's directory

Closes https://github.com/nix-community/naersk/issues/138.
This commit is contained in:
Patryk Wychowaniec 2023-07-05 09:50:33 +02:00
parent 711ebec4c2
commit d9a33d69a9
6 changed files with 71 additions and 1 deletions

View file

@ -404,7 +404,37 @@ let
''
mkdir -p $out
tar -xzf ${crate} -C $out
echo '{"package":"${sha256}","files":{}}' > $out/${name}-${version}/.cargo-checksum.json
# Most filesystems have a maximum filename length of 255
dest="$out/$(echo "${name}-${version}-${sha256}" | head -c 255)"
# Unpacked crates contain a directory named after package's name and its
# version - here we're renaming that directory to contain the package's
# checksum as well, to avoid clashes in edge cases like:
#
# ```
# [dependencies]
# rand_core = "0.6.1"
# rand = { git = "https://github.com/rust-random/rand.git", rev = "...", package = "rand_core" }
# ```
#
# ... which might end up having similar entries in the Cargo.lock file:
#
# ```
# [[package]]
# name = "rand_core"
# version = "0.6.1"
# source = "registry+https://github.com/rust-lang/crates.io-index"
# checksum = "..."
#
# [[package]]
# name = "rand_core"
# version = "0.6.1"
# source = "git+https://github.com/rust-random/rand.git?rev=..."
# ```
mv "$out/${name}-${version}" "$dest"
echo '{"package":"${sha256}","files":{}}' > "$dest/.cargo-checksum.json"
'';
unpackGitDependency = { checkout, key, name, url, ... }:

View file

@ -2,6 +2,7 @@ args: {
cargo-wildcard = import ./cargo-wildcard args;
default-run = import ./default-run args;
dummyfication = import ./dummyfication args;
duplicated-cargo-lock-items = import ./duplicated-cargo-lock-items args;
git-dep = import ./git-dep args;
git-dep-by-branch = import ./git-dep-by-branch args;
git-dep-by-branch-with-slash = import ./git-dep-by-branch-with-slash args;

View file

@ -0,0 +1,6 @@
{ naersk, ... }:
naersk.buildPackage {
src = ./fixtures;
doCheck = true;
}

View file

@ -0,0 +1,22 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "app"
version = "0.1.0"
dependencies = [
"rand_core 0.6.1",
"rand_core 0.6.4",
]
[[package]]
name = "rand_core"
version = "0.6.1"
source = "git+https://github.com/rust-random/rand.git?rev=bda997404d9ab8a89d590734d8b0bb72aed10758#bda997404d9ab8a89d590734d8b0bb72aed10758"
[[package]]
name = "rand_core"
version = "0.6.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c026d7df8b298d90ccbbc5190bd04d85e159eaf5576caeacf8741da93ccbd2e5"

View file

@ -0,0 +1,8 @@
[package]
name = "app"
version = "0.1.0"
edition = "2018"
[dependencies]
rand = { git = "https://github.com/rust-random/rand.git", rev = "bda997404d9ab8a89d590734d8b0bb72aed10758", package = "rand_core" }
rand_core = "0.6.1"

View file

@ -0,0 +1,3 @@
fn main() {
println!("Hello, world!");
}