Skip to content

cargo test invokes rustdoc with absolute paths, causing compile errors #9676

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

Closed
kwi-dk opened this issue Jul 9, 2021 · 2 comments
Closed
Labels
C-bug Category: bug

Comments

@kwi-dk
Copy link

kwi-dk commented Jul 9, 2021

Problem

cargo test invokes rustdoc with absolute paths, causing compile errors if the code depends on source paths being relative, e.g. through using

include!(concat!(env!("OUT_DIR"), "/", file!()));

to auto-include matching build.rs-generated code.

Steps
To demonstrate, let's make a quine!

  1. Create a new Cargo binary project with cargo new quine, and put this in src/main.rs:

    const MAIN_SOURCE: &str = include_str!(concat!("../", file!()));
    
    fn main() {
        print!("{}", MAIN_SOURCE);
    }

    ✔️ cargo run shows that the code is working, and
    ✔️ cargo test confirms that our non-existing test suite is green.

  2. This code works, but is horribly unorganized. The next obvious step is to create a separate library crate, so put this in src/lib.rs:

    pub const LIB_SOURCE: &str = include_str!(concat!("../", file!()));

    and add this line to the end of main(), in src/main.rs:

        print!("{}", quine::LIB_SOURCE);

    ✔️ cargo run works as expected, but...
    cargo test chokes when trying to run doc tests:

       Doc-tests quine
    error: couldn't read /data/projects/quine/src/..//data/projects/quine/src/lib.rs: No such file or directory (os error 2)
    

    cargo test -v confirms that Cargo is passing absolute source paths to rustdoc. If I run the same rustdoc command, except with a relative path to the source file, everything compiles as expected.

Possible Solution(s)

For now, my workaround is to disable doc tests in Cargo.toml, and then invoke rustdoc manually from build automation.

[lib]
doctest = false

But intuitively, I'd not expect the value of path!() to change between my normal build and my doc tests.
To ensure that, it seems that Cargo must pass a relative source path to rustdoc, not an absolute path.

Notes

Output of cargo version:

cargo 1.53.0 (4369396 2021-04-27)
cargo 1.55.0-nightly (3ebb5f1 2021-07-02)

Confirmed with both above versions, on Linux and Windows.

@kwi-dk kwi-dk added the C-bug Category: bug label Jul 9, 2021
@ehuss
Copy link
Contributor

ehuss commented Jul 10, 2021

Thanks for the thorough report! This is a known issue, and there is a fix on the nightly channel using the -Z doctest-in-workspace flag. If you could give it a try on your projects and report if there are any problems, that would be appreciated. Closing in favor of tracking issue #9427.

@ehuss ehuss closed this as completed Jul 10, 2021
@kwi-dk
Copy link
Author

kwi-dk commented Jul 11, 2021

Ah! I did see that issue, but it's talk of working directories seemed unrelated to my issue with absolute paths.

Nevertheless, cargo +nightly -Z doctest-in-workspace test does indeed solve the problem. Thanks!

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

No branches or pull requests

2 participants