From d9a33d69a9c421d64c8d925428864e93be895dcc Mon Sep 17 00:00:00 2001 From: Patryk Wychowaniec Date: Wed, 5 Jul 2023 09:50:33 +0200 Subject: [PATCH] Use checksums for recreating crates.io's directory Closes https://github.com/nix-community/naersk/issues/138. --- build.nix | 32 ++++++++++++++++++- test/fast/default.nix | 1 + .../duplicated-cargo-lock-items/default.nix | 6 ++++ .../fixtures/Cargo.lock | 22 +++++++++++++ .../fixtures/Cargo.toml | 8 +++++ .../fixtures/src/main.rs | 3 ++ 6 files changed, 71 insertions(+), 1 deletion(-) create mode 100644 test/fast/duplicated-cargo-lock-items/default.nix create mode 100644 test/fast/duplicated-cargo-lock-items/fixtures/Cargo.lock create mode 100644 test/fast/duplicated-cargo-lock-items/fixtures/Cargo.toml create mode 100644 test/fast/duplicated-cargo-lock-items/fixtures/src/main.rs diff --git a/build.nix b/build.nix index 2c814c1..8ab8d97 100644 --- a/build.nix +++ b/build.nix @@ -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, ... }: diff --git a/test/fast/default.nix b/test/fast/default.nix index e455152..18582e4 100644 --- a/test/fast/default.nix +++ b/test/fast/default.nix @@ -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; diff --git a/test/fast/duplicated-cargo-lock-items/default.nix b/test/fast/duplicated-cargo-lock-items/default.nix new file mode 100644 index 0000000..d34c380 --- /dev/null +++ b/test/fast/duplicated-cargo-lock-items/default.nix @@ -0,0 +1,6 @@ +{ naersk, ... }: + +naersk.buildPackage { + src = ./fixtures; + doCheck = true; +} diff --git a/test/fast/duplicated-cargo-lock-items/fixtures/Cargo.lock b/test/fast/duplicated-cargo-lock-items/fixtures/Cargo.lock new file mode 100644 index 0000000..74a809d --- /dev/null +++ b/test/fast/duplicated-cargo-lock-items/fixtures/Cargo.lock @@ -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" diff --git a/test/fast/duplicated-cargo-lock-items/fixtures/Cargo.toml b/test/fast/duplicated-cargo-lock-items/fixtures/Cargo.toml new file mode 100644 index 0000000..17a64c8 --- /dev/null +++ b/test/fast/duplicated-cargo-lock-items/fixtures/Cargo.toml @@ -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" diff --git a/test/fast/duplicated-cargo-lock-items/fixtures/src/main.rs b/test/fast/duplicated-cargo-lock-items/fixtures/src/main.rs new file mode 100644 index 0000000..e7a11a9 --- /dev/null +++ b/test/fast/duplicated-cargo-lock-items/fixtures/src/main.rs @@ -0,0 +1,3 @@ +fn main() { + println!("Hello, world!"); +}