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

OUT_DIR is not defined during cargo test #879

Closed
dwrensha opened this issue Nov 15, 2014 · 10 comments · Fixed by #897
Closed

OUT_DIR is not defined during cargo test #879

dwrensha opened this issue Nov 15, 2014 · 10 comments · Fixed by #897

Comments

@dwrensha
Copy link

This makes it difficult to test libraries that depend on generated code.

dwrensha added a commit to capnproto/capnpc-rust that referenced this issue Nov 15, 2014
dwrensha added a commit to capnproto/capnp-rpc-rust that referenced this issue Nov 15, 2014
@alexcrichton
Copy link
Member

Hm could you give an example of what's broken here? Some simple smoke tests work so I'm a little unclear on what's going wrong.

@dwrensha
Copy link
Author

Works for me on an executable crate but not on a library crate. E.g.:

#Cargo.toml

[project]
name = "foo"
version = "0.5.0"
authors = []
build = "build.rs"

[lib]
name = "foo"
path = "lib.rs"
//lib.rs

include!(concat!(env!("OUT_DIR"), "/hello.rs"))
// build.rs

use std::os;
use std::io::File;
fn main() {
    let dst = Path::new(os::getenv("OUT_DIR").unwrap());
    let mut f = File::create(&dst.join("hello.rs")).unwrap();
    f.write_str("pub fn message() -> &'static str {
                \"Hello, World!\"
    }
").unwrap();
}
$ cargo --version
cargo 0.0.1-pre-nightly (56852db 2014-11-14 23:33:33 +0000)
$ cargo test
     Running target/foo-60d37b257115c1c7

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured

   Doc-tests foo
lib.rs:3:1: 3:48 error: environment variable `OUT_DIR` not defined
lib.rs:3 include!(concat!(env!("OUT_DIR"), "/hello.rs"))
         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
lib.rs:3:1: 3:48 error: couldn't read 0/hello.rs: couldn't open file (no such file or directory (No such file or directory); path=0/hello.rs; mode=open; access=read)
lib.rs:3 include!(concat!(env!("OUT_DIR"), "/hello.rs"))
         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
task '<main>' panicked at 'Box<Any>', /Users/rustbuild/src/rust-buildbot/slave/nightly-mac/build/src/libsyntax/diagnostic.rs:86

@alexcrichton
Copy link
Member

Aha, I see what's going on here! It looks like it's just the doc tests that cause a problem.

@John-Nagle
Copy link

Did this fix make it into the pre-alpha versions? I'm getting this error when trying to build the "rust-http" module, just downloaded from "https://github.com/chris-morgan/rust-http/archive/master.zip".

cargo build
Compiling http v0.1.0-pre (file:///home/john/projects/rustcode/rust-http)
Compiling rustc-serialize v0.2.7
Compiling gcc v0.1.4
Compiling regex v0.1.10
Compiling matches v0.1.2
Compiling pkg-config v0.1.4
Compiling time v0.1.12
Compiling openssl-sys v0.2.15
Compiling log v0.1.9
Compiling openssl v0.2.15
Compiling url v0.2.16
src/http/server/request.rs:127:21: 127:75 error: couldn't read /home/john/projects/rustcode/rust-http/target/build/http-2b9772103c3d8d3f/out/read_method.rs: IoError { kind: FileNotFound, desc: "couldn't open path as file", detail: Some("no such file or directory (No such file or directory); path=/home/john/projects/rustcode/rust-http/target/build/http-2b9772103c3d8d3f/out/read_method.rs; mode=open; access=read") }
src/http/server/request.rs:127 mod dummy { include!(concat!(env!("OUT_DIR"), "/read_method.rs")); }
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Could not compile http.
To learn more, run the command again with --verbose.

cargo --version
cargo 0.0.1-pre-nightly (8c01b6b 2015-01-08 20:52:43 +0000)

@alexcrichton
Copy link
Member

Yes this change should definitely be in that version of Cargo. Perhaps a local bug?

@John-Nagle
Copy link

It's a problem with the build script for "rust-http". Just for the record, rust-http has a program which generates "read_method.rs", but into the codegen directory, which is not OUT_DIR. "rust-http" is now abandoned - ("OBSOLETION NOTICE...This library is DEAD...YOU SHOULD NOT USE THIS LIBRARY AT ALL.") So this will not be fixed. Switching to "Hyper" for HTTP use. Sigh.

@pbougue
Copy link

pbougue commented Apr 28, 2016

I think I'm having that same problem, when using OUT_DIR just in tests:

#Cargo.toml

[package]
name = "foo"
version = "0.0.1"
authors = [ ]
//tests/tests.rs

#[test]
fn test() {
    println!(env!("OUT_DIR"));
}

Resulting in:

$ cargo --version
cargo 0.9.0-nightly (8fc3fd8 2016-02-29)
$ cargo test
   Compiling foo v0.0.1 (file:///~~~/dev/sources/foo)
tests/tests.rs:3:5: 3:31 error: environment variable `OUT_DIR` not defined
tests/tests.rs:3     println!(env!("OUT_DIR"));
                     ^~~~~~~~~~~~~~~~~~~~~~~~~~
error: aborting due to previous error
error: Could not compile `foo`.

To learn more, run the command again with --verbose.

I have a workaround: adding a build.rs. It's ok, but that sounds like a problem to me.
If I add a build.rs as follows, that works fine:

#Cargo.toml

[package]
name = "foo"
version = "0.0.1"
authors = [ ]
build = "build.rs"
//build.rs

fn main() { }

Resulting in:

$ cargo test
   Compiling foo v0.0.1 (file:///~~~/dev/sources/foo)
     Running target/debug/tests-695fd20f593eeffc

running 1 test
test test ... ok

test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured

@alexcrichton
Copy link
Member

@pbougue currently OUT_DIR is only defined if a build script is defined as well I believe

@pbougue
Copy link

pbougue commented Apr 29, 2016

Ok thanks, we'll let it that way then!

@RReverser
Copy link
Contributor

currently OUT_DIR is only defined if a build script is defined as well I believe

Oh wow, that wasn't very obvious.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants