Cargo DEP environment vars: What am I missing?

In the cargo docs, under Outputs of the build script:

# specially recognized by Cargo
cargo:rustc-link-lib=static=foo
cargo:rustc-link-search=native=/path/to/foo
cargo:rustc-cfg=foo
cargo:rustc-env=FOO=bar
# arbitrary user-defined metadata
cargo:root=/path/to/foo
cargo:libdir=/path/to/foo/lib
cargo:include=/path/to/foo/include 

[...]
Any other element is a user-defined metadata that will be passed to dependents. More information about this can be found in the links section.

Under The links Manifest Key:

As mentioned above in the output format, each build script can generate an arbitrary set of metadata in the form of key-value pairs. This metadata is passed to the build scripts of dependent packages. For example, if libbar depends on libfoo, then if libfoo generates key=value as part of its metadata, then the build script of libbar will have the environment variables DEP_FOO_KEY=value.


Okay then. I shall create two crates where b depends on a. a's build script will generate metadata, and b's build script will look for the resulting environment variables:

# in an impty directory...

cargo new a
cargo new b

# b depends on a
cat >>b/Cargo.toml <<EOF

# I tried this also with [dependencies], but that seems
# to allow b's build script to run before a's build script,
# in which case the env passing clearly could not work.
[build-dependencies]
a = { path = "../a" }
EOF

# a's build script emits custom metadata...
cat >a/build.rs <<EOF
fn main() {
    // examples straight from the cargo docs
    println!("cargo:root=/path/to/foo");
    println!("cargo:libdir=/path/to/foo/lib");
    println!("cargo:include=/path/to/foo/include");
}
EOF

# ...so b's build script should see it.
cat >b/build.rs <<EOF
fn main() {
    ::std::env::vars()
        .filter(|&(ref key, _)| key.starts_with("DEP"))
        .next().expect("no env vars begin with 'DEP'!");
}
EOF

# ...right?
(cd b; cargo build)

Output:

     Created library `a` project
     Created library `b` project
   Compiling a v0.1.0 (file:///home/exp/dev/test/cargo-build-dep/a)
   Compiling b v0.1.0 (file:///home/exp/dev/test/cargo-build-dep/b)
error: failed to run custom build command for `b v0.1.0 (file:///home/exp/dev/test/cargo-build-dep/b)`
process didn't exit successfully: `/home/exp/dev/test/cargo-build-dep/b/target/debug/build/b-6e6e15905d151499/build-script-build` (exit code: 101)
--- stderr
thread 'main' panicked at 'no env vars begin with 'DEP'!', /checkout/src/libcore/option.rs:891:5

It seems to me these environment variables simply are not there. Please let me know what I'm doing wrong.

https://github.com/rust-lang/cargo/issues/3544

1 Like