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

FoundCrate::Itself reported in doc tests #11

Closed
h33p opened this issue Jun 2, 2021 · 4 comments
Closed

FoundCrate::Itself reported in doc tests #11

h33p opened this issue Jun 2, 2021 · 4 comments

Comments

@h33p
Copy link

h33p commented Jun 2, 2021

Hey there, it appears that FoundCrate::Itself is being returned when inside documentation tests. I currently work this around with the following snippet:

pub fn crate_path() -> TokenStream {
    let found_crate = crate_name("cglue").expect("cglue found in `Cargo.toml`");

    match found_crate {
        FoundCrate::Itself => {
            let has_doc_env = std::env::vars().any(|(k, _)| {
                k == "UNSTABLE_RUSTDOC_TEST_LINE" || k == "UNSTABLE_RUSTDOC_TEST_PATH"
            });

            if has_doc_env {
                quote!(::cglue)
            } else {
                quote!(crate)
            }
        }
        FoundCrate::Name(name) => quote!(::#name),
    }
}

Now, these environment variables are clearly not stable, but it was the only way I could distinguish doc tests, sooooo I don't know, the worst that could happen is these variables being removed.

@bkchr
Copy link
Owner

bkchr commented Jun 2, 2021

Hey,
sadly there isn't anything I can do here. There is nothing to detect if this is a doc test or not.

The best is to do the following:

extern crate self as cglue;

@bkchr bkchr closed this as completed Jun 2, 2021
@CobaltCause
Copy link
Contributor

You could use your own environment variable, for example DOCTEST_MODE, and then run your CI like this:

cargo test --tests # everything except doctests
DOCTEST_MODE="" cargo test --doc # only doctests

Since #13, you could also just use proc-macro-crate as normal, and use the above snippet in CI except with the CARGO_TARGET_TMPDIR environment variable.

@jplatte
Copy link
Contributor

jplatte commented Nov 13, 2021

Hey, sadly there isn't anything I can do here. There is nothing to detect if this is a doc test or not.

The best is to do the following:

extern crate self as cglue;

But that only works if ones emits the crate name on FoundCrate::Itself, which defeats the purpose of that enum variant. I feel like this should be re-opened (with a blocked label potentially`) and only closed once it is possible (through Cargo / rustc changes) to make this work.

@bkchr
Copy link
Owner

bkchr commented Nov 13, 2021

I mean this entire crate is a hack around some missing feature in rustc :P I somehow still hope that rustc at some day brings "native" support for this.

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

No branches or pull requests

4 participants