ipetkov / crane

A Nix library for building cargo projects. Never build twice thanks to incremental artifact caching.
https://crane.dev
MIT License
962 stars 92 forks source link

Unable to fetch git dependency. #644

Closed levydsa closed 5 months ago

levydsa commented 5 months ago

When trying to build a package with git dependency this error happens. Any easy way to solve this?

[workspace]
members = [
  # ...
]
resolver = "2"

[workspace.dependencies]
#...
libsql = { git = "https://github.com/levydsa/libsql.git", package = "libsql", branch = "nix-patch" }
# ...
error: builder for '/nix/store/gnj648xgsqgl1niy7h8cfzjhsdgm80r8-qed-web-deps-0.1.0.drv' failed with exit code 101;
       last 10 log lines:
       > Caused by:
       >   failed to clone into: /build/source/.cargo-home/git/db/libsql-32cf909f52753506
       >
       > Caused by:
       >   network failure seems to have happened
       >   if a proxy or similar is necessary `net.git-fetch-with-cli` may help here
       >   https://doc.rust-lang.org/cargo/reference/config.html#netgit-fetch-with-cli
       >
       > Caused by:
       >   failed to resolve address for github.com: Temporary failure in name resolution; class=Net (12)
       For full logs, run 'nix log /nix/store/gnj648xgsqgl1niy7h8cfzjhsdgm80r8-qed-web-deps-0.1.0.drv'.
error: 1 dependencies of derivation '/nix/store/34ffbvvbk4j7950mnmbcjr4n4ma389h2-qed-web-0.1.0.drv' failed to build
error: 1 dependencies of derivation '/nix/store/4l3w200bxs0bcfmjd3a7c9hq3gfiyqrb-image-root.drv' failed to build
error: 1 dependencies of derivation '/nix/store/zmcmqlwl1i9fcyvndmsmvllr34dh76h5-docker-layer-qed-web.drv' failed to build
error: 1 dependencies of derivation '/nix/store/9062fnmyjbydk024fpqgihln0i2lzzb8-docker-image-qed-web.tar.gz.drv' failed to build

When I set the net.git-fetch-with-cli env var and add the git package to the build. The output is similar, but with another error (maybe related to networking again?).

warning: Git tree '/home/dante/src/repos/qed' is dirty
error: builder for '/nix/store/7lsgp812cyad4bm63mrdgrf54mpiq24f-qed-web-deps-0.1.0.drv' failed with exit code 101;
       last 10 log lines:
       >   failed to load source for dependency `libsql`
       >
       > Caused by:
       >   Unable to update https://github.com/levydsa/libsql.git?branch=nix-patch
       >
       > Caused by:
       >   failed to clone into: /build/source/.cargo-home/git/db/libsql-32cf909f52753506
       >
       > Caused by:
       >   process didn't exit successfully: `git fetch --force --update-head-ok 'https://github.com/levydsa/libsql.git' '+refs/heads/nix-patch:refs/remotes/origin/nix-patch'` (exit status: 128)
       For full logs, run 'nix log /nix/store/7lsgp812cyad4bm63mrdgrf54mpiq24f-qed-web-deps-0.1.0.drv'.
error: 1 dependencies of derivation '/nix/store/7wbivdxsvfr80s74snxxm4h06j76374l-qed-web-0.1.0.drv' failed to build
error: 1 dependencies of derivation '/nix/store/ni89x9aa01bx62ba172ark5cj9427bvs-image-root.drv' failed to build
error: 1 dependencies of derivation '/nix/store/d25i2a7af7rpmzbw45q9hbjwsbjbkxm1-docker-layer-qed-web.drv' failed to build
error: 1 dependencies of derivation '/nix/store/xif6czwm2rih9cjw9wigdsm9ms9zbna7-docker-image-qed-web.tar.gz.drv' failed to build
packages.qed-web = craneLib.buildPackage {
  src = fileSetForCrate ./crates/qed-web;
  cargoToml = ./crates/qed-web/Cargo.toml;
  cargoExtraArgs = "-p ${packages.qed-web.pname}";

  strictDeps = true;

  LD_LIBRARY_PATH = "${pkgs.openssl.out}/lib";
  CARGO_NET_GIT_FETCH_WITH_CLI="true";

  nativeBuildInputs = with pkgs; [
    git
    pkg-config
    openssl
  ];

  buildInputs = with pkgs; [
    openssl
  ];
};

I was doing this to try to debug another problem when trying to build with the libsql-ffi dependency. Maybe I can make another issue later? I'll try to figure it out on my own, but this issue is a bit overwhelming to me. No idea what's happening.

ipetkov commented 5 months ago

Hi @levydsa! First question that comes to mind is your Cargo.lock file up to date? Running cargo update should make sure that the git entry shows up and can be picked up during vendoring

levydsa commented 5 months ago

It makes a lot of sense. I don't remenber updating the Cargo.lock before running the nix build. Now, cargo update fails in a similar way. Maybe I have to set something up?

$ cargo update
    Updating crates.io index
    Updating git repository `https://github.com/levydsa/libsql.git`
error: failed to get `libsql` as a dependency of package `qed-web v0.1.0 (/home/dante/src/repos/qed/crates/qed-web)`

Caused by:
  failed to load source for dependency `libsql`

Caused by:
  Unable to update https://github.com/levydsa/libsql.git?branch=nix-patch

Caused by:
  failed to fetch into: /home/dante/.local/share/cargo/git/db/libsql-32cf909f52753506

Caused by:
  network failure seems to have happened
  if a proxy or similar is necessary `net.git-fetch-with-cli` may help here
  https://doc.rust-lang.org/cargo/reference/config.html#netgit-fetch-with-cli

Caused by:
  SSL error: unknown error; class=Ssl (16)
$ CARGO_NET_GIT_FETCH_WITH_CLI="true" cargo update
    Updating crates.io index
    Updating git repository `https://github.com/levydsa/libsql.git`
remote: Enumerating objects: 243527, done.
remote: Counting objects: 100% (128/128), done.
remote: Compressing objects: 100% (104/104), done.
error: 1315 bytes of body are still expected5 MiB | 1004.00 KiB/s
fetch-pack: unexpected disconnect while reading sideband packet
fatal: early EOF
fatal: fetch-pack: invalid index-pack output
error: failed to get `libsql` as a dependency of package `qed-web v0.1.0 (/home/dante/src/repos/qed/crates/qed-web)`

Caused by:
  failed to load source for dependency `libsql`

Caused by:
  Unable to update https://github.com/levydsa/libsql.git?branch=nix-patch

Caused by:
  failed to fetch into: /home/dante/.local/share/cargo/git/db/libsql-32cf909f52753506

Caused by:
  process didn't exit successfully: `git fetch --force --update-head-ok 'https://github.com/levydsa/libsql.git' '+refs/heads/nix-patch:refs/remotes/origin/nix-patch'` (exit status: 128)

cargo run worked lol. I made a typo, it's already "ammended" it in the nix-patch branch. Just to show the repos getting fetched.

$ cargo run
    Updating git repository `https://github.com/levydsa/libsql.git`
     Locking 7 packages
      Adding libsql v0.4.0 (https://github.com/levydsa/libsql.git?branch=nix-patch#10cb6996)
      Adding libsql-ffi v0.3.0 (https://github.com/levydsa/libsql.git?branch=nix-patch#10cb6996)
      Adding libsql-hrana v0.2.0 (https://github.com/levydsa/libsql.git?branch=nix-patch#10cb6996)
      Adding libsql-rusqlite v0.31.0 (https://github.com/levydsa/libsql.git?branch=nix-patch#10cb6996)
      Adding libsql-sqlite3-parser v0.12.0 (https://github.com/levydsa/libsql.git?branch=nix-patch#10cb6996)
      Adding libsql-sys v0.6.0 (https://github.com/levydsa/libsql.git?branch=nix-patch#10cb6996)
      Adding libsql_replication v0.4.0 (https://github.com/levydsa/libsql.git?branch=nix-patch#10cb6996)
   Compiling libsql-ffi v0.3.0 (https://github.com/levydsa/libsql.git?branch=nix-patch#10cb6996)
   Compiling libsql-sqlite3-parser v0.12.0 (https://github.com/levydsa/libsql.git?branch=nix-patch#10cb6996)
   Compiling libsql-hrana v0.2.0 (https://github.com/levydsa/libsql.git?branch=nix-patch#10cb6996)
error: expected `;`, found keyword `let`
   --> /home/dante/.local/share/cargo/git/checkouts/libsql-32cf909f52753506/10cb699/libsql-ffi/build.rs:102:57
    |
102 |     dbg!((format!("{dir}/{bindgen_rs_path}"), out_path))
    |                                                         ^ help: add `;` here
103 |
104 |     let mut cfg = cc::Build::new();
    |     --- unexpected token

error: could not compile `libsql-ffi` (build script) due to 1 previous error
warning: build failed, waiting for other jobs to finish...

When I fixed the semicolon, everything seems to run fine. But when trying to build...

$ nix build .#container
warning: Git tree '/home/dante/src/repos/qed' is dirty
error: builder for '/nix/store/fng144bdb79s083j49d4x6p6m93fmwvs-cargo-git.drv' failed with exit code 1;
       last 10 log lines:
       > source root is source
       > Running phase: patchPhase
       > Running phase: updateAutotoolsGnuConfigScriptsPhase
       > Updating Autotools / GNU config script to a newer upstream version: ./libsql-ffi/bundled/SQLite3MultipleCiphers/admin/build-aux/config.sub
       > Updating Autotools / GNU config script to a newer upstream version: ./libsql-sqlite3/config.sub
       > Updating Autotools / GNU config script to a newer upstream version: ./libsql-ffi/bundled/SQLite3MultipleCiphers/admin/build-aux/config.guess
       > Updating Autotools / GNU config script to a newer upstream version: ./libsql-sqlite3/config.guess
       > Running phase: installPhase
       > cp: cannot stat '/build/source/libsql-sqlite3/ext/crr/rs/sqlite-rs-embedded/sqlite3_capi/deps/sqlite3.h': No such file or directory
       > cp: cannot stat '/build/source/libsql-sqlite3/ext/crr/rs/sqlite-rs-embedded/sqlite3_capi/deps/sqlite3ext.h': No such file or directory
       For full logs, run 'nix log /nix/store/fng144bdb79s083j49d4x6p6m93fmwvs-cargo-git.drv'.
error: 1 dependencies of derivation '/nix/store/2yxgnbj59alh04bjikka6pyzkib5nmp1-linkLockedDeps.drv' failed to build
error: 1 dependencies of derivation '/nix/store/6rpzy95kb32qq9l9qs9xhjd3vfkw6a5z-vendor-cargo-deps.drv' failed to build
error: 1 dependencies of derivation '/nix/store/kcbfdjzvkya0l7v7k25mm52m566dka7b-qed-web-0.1.0.drv' failed to build
error: 1 dependencies of derivation '/nix/store/4ah1mx85jj4n9namp1n4gyzpfvwddg2d-image-root.drv' failed to build
error: 1 dependencies of derivation '/nix/store/pm4ibhzrgvnvivig3yw5jz1c99va5jdw-docker-layer-qed-web.drv' failed to build
error: 1 dependencies of derivation '/nix/store/3i4v3099fv4z9lbzi6b59q9h7yjgxfls-docker-image-qed-web.tar.gz.drv' failed to build

Now I'm lost. I know that the build.rs for libsql-ffi is broken when trying to build with crane from the cargo registry. But this error is new. I don't know why there is a symlink in upstream libsql that points to nothing. I'm going insane.

levydsa commented 5 months ago

Ok, now if fixed it by just copying the files that should be symlinked, just to make things work. Now I can debug the initial problem. It seems that libsql-ffi is trying to modify files that are off the nix store.

warning: Git tree '/home/dante/src/repos/qed' is dirty
error: builder for '/nix/store/w1i4hsyy5bxvjxs11n8r4kacq0bx2mrs-qed-web-deps-0.1.0.drv' failed with exit code 101;
       last 10 log lines:
       >
       >   --- stderr
       >   [/nix/store/h23s6yzi0w9m9h0bm606fysg5wmq4bk4-vendor-cargo-deps/2207e6162cffb926680ad9d4345ae20306b4e0c2c08a080ea970acf2486365a6/libsql-ffi-0.3.0/build.rs:101:5] (format!("{dir}/{bindgen_rs_path}"), out_path) = (
       >       "/nix/store/h23s6yzi0w9m9h0bm606fysg5wmq4bk4-vendor-cargo-deps/2207e6162cffb926680ad9d4345ae20306b4e0c2c08a080ea970acf2486365a6/libsql-ffi-0.3.0/bundled/bindings/bindgen.rs",
       >       "/build/source/target/release/build/libsql-ffi-c266b42decff3ceb/out/bindgen.rs",
       >   )
       >   thread 'main' panicked at /nix/store/h23s6yzi0w9m9h0bm606fysg5wmq4bk4-vendor-cargo-deps/2207e6162cffb926680ad9d4345ae20306b4e0c2c08a080ea970acf2486365a6/libsql-ffi-0.3.0/build.rs:102:65:
       >   called `Result::unwrap()` on an `Err` value: Os { code: 13, kind: PermissionDenied, message: "Permission denied" }
       >   note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
       > warning: build failed, waiting for other jobs to finish...
       For full logs, run 'nix log /nix/store/w1i4hsyy5bxvjxs11n8r4kacq0bx2mrs-qed-web-deps-0.1.0.drv'.
error: 1 dependencies of derivation '/nix/store/75ggrf973z2n75lx35x87qqmm9nknhka-qed-web-0.1.0.drv' failed to build
error: 1 dependencies of derivation '/nix/store/xn4h04xnpij284jqdiqfi95lhc35lms3-image-root.drv' failed to build
error: 1 dependencies of derivation '/nix/store/5apj1agb28qwd6v3x635418g8iz343my-docker-layer-qed-web.drv' failed to build
error: 1 dependencies of derivation '/nix/store/5barcbggjvgw1nzkz6q86ldnzi85g3yx-docker-image-qed-web.tar.gz.drv' failed to build

https://github.com/levydsa/libsql/blob/74a8df8304a3fd99cee4373e2bce0d9955d5e43b/libsql-ffi/build.rs#L99-L103

But it seems to be trying to write to $OUT_DIR, which is correct? idk

https://github.com/levydsa/libsql/blob/74a8df8304a3fd99cee4373e2bce0d9955d5e43b/libsql-ffi/build.rs#L20-L21

levydsa commented 5 months ago

@ipetkov shoud I open another issue and close this one?

ipetkov commented 5 months ago

If you have a flake that reproduces the issue you can post it here

levydsa commented 5 months ago

Clone it from here https://github.com/levydsa/qed.

ipetkov commented 5 months ago

The problem is that build script will attempt to copy bindgen.rs to OUT_DIR multiple times. Here's what happens:

  1. Sources are vendored in a derivation which goes into the /nix/store/...
  2. Nix changes the permissions of the files to be readonly (i.e. r--, usually cargo keeps source files stored as rw-)
  3. When the vendored sources are linked to the deps/real derivations they maintain the same permission bits
  4. The build script will successfully copy bindgen.rs to OUT_DIR the first time, but the file will maintain its read-only r-- permissions
  5. A second attempt to overwrite the file fails since std::fs::copy appears to try to update the file in place (which fails due to no write-permissions)

If you go through some trouble to make the vendored sources writable the build continues past this point. Hence my advice is to patch the build script to either not copy the file multiple times, or to attempt to delete the file before copying.

Hope this helps!