Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug with build.rs and built crate #6

Closed
nuxeh opened this issue Apr 22, 2019 · 8 comments
Closed

Bug with build.rs and built crate #6

nuxeh opened this issue Apr 22, 2019 · 8 comments
Labels
bug Something isn't working

Comments

@nuxeh
Copy link

nuxeh commented Apr 22, 2019

Error building https://github.com/nuxeh/url-bot-rs, this time with a build.rs...

Building build.rs (url-bot-rs)
Running rustc --crate-name build_script_build build.rs --crate-type bin -C opt-level=3 -C codegen-units=4 --edition 2015 --out-dir target/build/url-bot-rs --emit=dep-info,link -L dependency=target/buildDeps --extern built=/nix/store/si7mi7qqdm55njgvgajdlbs8ida9rn2w-rust_built-0.3.0/lib/libbuilt-8c884e3e71.rlib --extern man=/nix/store/4i8bvbjfsmjsnldxifnw490s60jhrwi0-rust_man-0.3.0/lib/libman-3ad10e7239.rlib --cap-lints allow -L native=/nix/store/1gb46kwmhh86j453rwd09218ryh7mn1h-libgit2-0.26.6/lib -L native=/nix/store/2xb0wphs5h89i0k1r8lya9fpsjwkn7xm-openssl-1.0.2r/lib -L native=/nix/store/g4rgyrkq7yfnqfmv5hxsc32x6cydh1nc-sqlite-3.26.0/lib -L native=/nix/store/iip6qkdai647xww8wjw1q1775nmi6xq9-rust_libz-sys-1.0.25/lib/libz-sys.out/build -L native=/nix/store/iki0dq5ivii3hiwvxx81ppazjlqjk81l-rust_backtrace-sys-0.1.28/lib/backtrace-sys.out -l crypto -l git2 -l sqlite3 -l ssl -l static=backtrace -l static=z --color always
thread 'main' panicked at 'called `Option::unwrap()` on a `None` value', src/libcore/option.rs:355:21
stack backtrace:
   0: std::sys::unix::backtrace::tracing::imp::unwind_backtrace
   1: std::sys_common::backtrace::print
   2: std::panicking::default_hook::{{closure}}
   3: std::panicking::default_hook
   4: std::panicking::rust_panic_with_hook
   5: std::panicking::continue_panic_fmt
   6: rust_begin_unwind
   7: core::panicking::panic_fmt
   8: core::panicking::panic
   9: built::write_built_file_with_opts
  10: built::write_built_file
  11: build_script_build::main
  12: std::rt::lang_start::{{closure}}
  13: std::panicking::try::do_call
  14: __rust_maybe_catch_panic
  15: std::rt::lang_start_internal
  16: main
  17: __libc_start_main
  18: _start
             at ../sysdeps/x86_64/start.S:120
builder for '/nix/store/v58mc6kd167wn73p5a8rq4vga7g5iz9x-rust_url-bot-rs-0.2.0.drv' failed with exit code 101
error: build of '/nix/store/v58mc6kd167wn73p5a8rq4vga7g5iz9x-rust_url-bot-rs-0.2.0.drv' failed
@kolloch
Copy link
Collaborator

kolloch commented Apr 23, 2019

This is a bit more difficult to track down since we do not have a stack trace with line numbers. Do you know what exactly failed? I don't see a plain "unwrap" in write_built_file_with_opts.

The primary reason for build.rs to fail so far has been differing environment variables between buildRustCrate and cargo. Here are the most obvious environment variables of buildRustCrate.

If you have any time to debug this issue, it would be nice to track down the exact line that fails.

@kolloch kolloch added the bug Something isn't working label Apr 23, 2019
@nuxeh
Copy link
Author

nuxeh commented Apr 23, 2019

There are a couple of unwraps in built::write_built_file, precisely dealing with obtaining environment variables, but it seems that both the env vars needed are exported according to the link you posted.

I need to study some more Nix to work out how to enter the build environment to introspect any more, i think.

@kolloch
Copy link
Collaborator

kolloch commented Apr 23, 2019

Since it might come in handy in other situations, this is how I debugged this.

  1. I cloned the built crate from github and improved its error handling:
diff --git a/src/lib.rs b/src/lib.rs
index 52f7034..eae149c 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -380,7 +380,7 @@ fn write_env<T: io::Write>(envmap: &EnvironmentMap, w: &mut T) -> io::Result<()>
     macro_rules! write_env_str {
         ($(($name:ident, $env_name:expr,$doc:expr)),*) => {$(
             writeln!(w, "#[doc={}]\npub const {}: &str = \"{}\";",
-                    stringify!($doc), stringify!($name), envmap.get($env_name).unwrap())?;
+                    stringify!($doc), stringify!($name), envmap.get($env_name).expect(stringify!($env_name)))?;
         )*}
     } 
  1. I patched your Cargo.toml to use my patched version of that crate:
diff --git a/Cargo.toml b/Cargo.toml
index 15ca171..54696bd 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -9,7 +9,7 @@ readme = "README.md"
 build = "build.rs"
 
 [build-dependencies]
-built = "0.3.0"
+built = { path = "../built" }
 man = "0.3.0"
 
 [dev-dependencies]
  1. I ran nix run -f ../crate2nix to have crate2nix in my path

  2. I regenerated the nix build file with crate2nix generate -n '<nixos-unstable>' -o Cargo.nix

  3. I ran nix build -f Cargo.nix, resulting in this error:

builder for '/nix/store/8sdinmsvh88c539s1lx25zlsiip1i76s-rust_url-bot-rs-0.2.0.drv' failed with exit code 101; last 10 log lines:
    14: __rust_maybe_catch_panic
               at src/libpanic_unwind/lib.rs:92
    15: std::rt::lang_start_internal
               at src/libstd/panicking.rs:276
               at src/libstd/panic.rs:388
               at src/libstd/rt.rs:48
    16: main
    17: __libc_start_main
    18: _start
               at ../sysdeps/x86_64/start.S:120
[0 built (1 failed)]
error: build of '/nix/store/8sdinmsvh88c539s1lx25zlsiip1i76s-rust_url-bot-rs-0.2.0.drv' failed
  1. I used nix log /nix/store/8sdinmsvh88c539s1lx25zlsiip1i76s-rust_url-bot-rs-0.2.0.drv to see the full error:
@nix { "action": "setPhase", "phase": "unpackPhase" }
unpacking sources
unpacking source archive /nix/store/kry1kyhxgh5md24skmncf7xzm83rnnd2-url-bot-rs
source root is url-bot-rs
@nix { "action": "setPhase", "phase": "patchPhase" }
patching sources
@nix { "action": "setPhase", "phase": "configurePhase" }
configuring
Building build.rs (url-bot-rs)
Running rustc --crate-name build_script_build build.rs --crate-type bin -C opt-level=3 -C codegen-units=>
thread 'main' panicked at '"CARGO_PKG_VERSION_PRE"', src/libcore/option.rs:1038:5
stack backtrace:
   0: std::sys::unix::backtrace::tracing::imp::unwind_backtrace
             at src/libstd/sys/unix/backtrace/tracing/gcc_s.rs:39
   1: std::sys_common::backtrace::print
             at src/libstd/sys_common/backtrace.rs:70
             at src/libstd/sys_common/backtrace.rs:58
   2: std::panicking::default_hook::{{closure}}
             at src/libstd/panicking.rs:200
   3: std::panicking::default_hook
             at src/libstd/panicking.rs:215
   4: std::panicking::rust_panic_with_hook
             at src/libstd/panicking.rs:478
   5: std::panicking::continue_panic_fmt
             at src/libstd/panicking.rs:385
   6: rust_begin_unwind
             at src/libstd/panicking.rs:312
   7: core::panicking::panic_fmt
             at src/libcore/panicking.rs:85
   8: core::option::expect_failed
             at src/libcore/option.rs:1038
   9: built::write_built_file_with_opts
  10: built::write_built_file
  11: build_script_build::main
  12: std::rt::lang_start::{{closure}}
  13: std::panicking::try::do_call
             at src/libstd/rt.rs:49
             at src/libstd/panicking.rs:297
  14: __rust_maybe_catch_panic
             at src/libpanic_unwind/lib.rs:92
  15: std::rt::lang_start_internal
             at src/libstd/panicking.rs:276
             at src/libstd/panic.rs:388
             at src/libstd/rt.rs:48
  16: main
  17: __libc_start_main
  18: _start
             at ../sysdeps/x86_64/start.S:120
  1. I provided the env variable via overrideAttrs in a separate default.nix and repeated the exercise

  2. I ended up with the following default.nix:

{ pkgs? import <nixos-unstable> { config = {}; }, callPackage? pkgs.callPackage }:

let cargo = callPackage ./Cargo.nix {};
in cargo.root_crate.overrideAttrs (attrs: {
                CARGO_PKG_VERSION_PRE =  "";
                CARGO_PKG_HOMEPAGE = "";
                })

With this, the crate builds (via nix build using the default.nix).

@kolloch
Copy link
Collaborator

kolloch commented Apr 23, 2019

(This means that we have to patch buildRustCrate to always provide this env variables)

@kolloch
Copy link
Collaborator

kolloch commented Apr 23, 2019

The fix is here: NixOS/nixpkgs#60127

@nuxeh
Copy link
Author

nuxeh commented Apr 23, 2019

Excellent stuff, thanks for the debugging info too!

@nuxeh
Copy link
Author

nuxeh commented Apr 27, 2019

Can confirm that this now works for url-bot-rs, now that the Nixpkgs patch has been merged.

@kolloch
Copy link
Collaborator

kolloch commented Apr 28, 2019

Thanks for checking the fix! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants