From 6c4b2ed878daaea64b917005cbf152cd1bfeac54 Mon Sep 17 00:00:00 2001 From: Patryk Wychowaniec Date: Wed, 6 Sep 2023 14:05:28 +0200 Subject: [PATCH] Support unused dependencies Cargo stashes unused patches into a special section of the Cargo.lock file: ``` [[patch.unused]] name = "uuid" version = "1.4.1" source = "git+https://github.com/uuid-rs/uuid#50f70278de02c106650b8d6deb325dd59b5f2a24" ``` ... a section that we, currently, don't read. Ignoring that section causes the build to fail because even if the patch is unused, when the source is unavailable, Cargo tries to fetch it (which, understandably, is not possible inside the sandbox). This commit extends our logic so that we download both the "used" and "unused" dependencies. Closes https://github.com/nix-community/naersk/issues/308. --- lib.nix | 9 ++++++++- test/fast/default.nix | 1 + test/fast/unused-patch/default.nix | 15 +++++++++++++++ test/fast/unused-patch/fixtures/Cargo.lock | 12 ++++++++++++ test/fast/unused-patch/fixtures/Cargo.toml | 7 +++++++ test/fast/unused-patch/fixtures/src/main.rs | 3 +++ 6 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 test/fast/unused-patch/default.nix create mode 100644 test/fast/unused-patch/fixtures/Cargo.lock create mode 100644 test/fast/unused-patch/fixtures/Cargo.toml create mode 100644 test/fast/unused-patch/fixtures/src/main.rs diff --git a/lib.nix b/lib.nix index 33a5875..1e71da3 100644 --- a/lib.nix +++ b/lib.nix @@ -99,7 +99,14 @@ rec } // (lib.optionalAttrs (! isNull branch) { inherit branch; }) // (lib.optionalAttrs (! isNull tag) { inherit tag; }) // (lib.optionalAttrs (! isNull rev) { inherit rev; }); - packageLocks = builtins.map parseLock (lib.filter query cargolock.package); + + usedPackageLocks = + builtins.map parseLock (lib.filter query cargolock.package); + + unusedPackageLocks = + builtins.map parseLock (lib.filter query ((cargolock.patch or []).unused or [])); + + packageLocks = usedPackageLocks ++ unusedPackageLocks; mkFetch = lock: { key = lock.rev or lock.tag or lock.branch or lock.revision diff --git a/test/fast/default.nix b/test/fast/default.nix index ee063b2..3eb1091 100644 --- a/test/fast/default.nix +++ b/test/fast/default.nix @@ -16,6 +16,7 @@ args: { simple-dep = import ./simple-dep args; simple-dep-patched = import ./simple-dep-patched args; symlinks = import ./symlinks args; + unused-patch = import ./unused-patch args; workspace = import ./workspace args; workspace-build-rs = import ./workspace-build-rs args; workspace-patched = import ./workspace-patched args; diff --git a/test/fast/unused-patch/default.nix b/test/fast/unused-patch/default.nix new file mode 100644 index 0000000..2f6ea43 --- /dev/null +++ b/test/fast/unused-patch/default.nix @@ -0,0 +1,15 @@ +{ naersk, pkgs, ... }: +let + app = naersk.buildPackage { + src = ./fixtures; + }; + +in +if builtins.compareVersions pkgs.lib.version "22.11" <= 0 then + # Executing this test requires nixpkgs > 22.11 due to changes to the TOML + # serialization function. + # + # See `writeTOML` in this repository for more details. + true +else + app diff --git a/test/fast/unused-patch/fixtures/Cargo.lock b/test/fast/unused-patch/fixtures/Cargo.lock new file mode 100644 index 0000000..13d034c --- /dev/null +++ b/test/fast/unused-patch/fixtures/Cargo.lock @@ -0,0 +1,12 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "app" +version = "0.1.0" + +[[patch.unused]] +name = "uuid" +version = "1.4.1" +source = "git+https://github.com/uuid-rs/uuid#50f70278de02c106650b8d6deb325dd59b5f2a24" diff --git a/test/fast/unused-patch/fixtures/Cargo.toml b/test/fast/unused-patch/fixtures/Cargo.toml new file mode 100644 index 0000000..3487e36 --- /dev/null +++ b/test/fast/unused-patch/fixtures/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "app" +version = "0.1.0" +edition = "2018" + +[patch.crates-io] +uuid = { git = "https://github.com/uuid-rs/uuid" } diff --git a/test/fast/unused-patch/fixtures/src/main.rs b/test/fast/unused-patch/fixtures/src/main.rs new file mode 100644 index 0000000..e7a11a9 --- /dev/null +++ b/test/fast/unused-patch/fixtures/src/main.rs @@ -0,0 +1,3 @@ +fn main() { + println!("Hello, world!"); +}