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

Detecting docs.rs from build.rs? #147

Closed
kornelski opened this issue Aug 18, 2017 · 19 comments · Fixed by #852
Closed

Detecting docs.rs from build.rs? #147

kornelski opened this issue Aug 18, 2017 · 19 comments · Fixed by #852
Labels
E-easy Effort: Should be easy to implement and would make a good first PR mentor This has instructions for getting started

Comments

@kornelski
Copy link

kornelski commented Aug 18, 2017

I have a crate that requires complicated system dependencies to build. However, the real functionality is not actually needed for docs, so I think I could fake it just for doc generation.

Can I somehow detect docs.rs and build my crate differently for docs?

@onur
Copy link
Member

onur commented Aug 19, 2017

Thanks for the neat idea, currently there is no way to check this.

We can setup an environment variable and you can check presence of this env var to detect if build machine is docs.rs. What do you think?

@kornelski
Copy link
Author

Yes, that would work.

@waywardmonkeys
Copy link

Is there any update on this or something that I can do to help out?

@epage
Copy link

epage commented Apr 21, 2018

As a terrible workaround, what about creating a docs-rs feature that stubs out your -sys dependencies? You can then use the package.metadata.docs.rs.features table in Cargo.toml to enable that feature.

@kornelski
Copy link
Author

kornelski commented Apr 21, 2018

There could be docs.rs-specific solution (e.g. the site could set a feature or env var), but ideally this could be built-in into Rust.

Tests get cfg(test), so perhaps docs should get cfg(doc)?

If there was an officially blessed solution, then it would be easier to make crates like pkg-config-rs solve the problem for all users (e.g. it could ignore pkg-config errors when cfg(doc)).

@pkgw
Copy link

pkgw commented May 16, 2018

See @nickbabcock's comment in #191 for a clever-but-hacky workaround:

[package.metadata.docs.rs]
rustc-args = ["--cfg",  "<name>_docs_rs"]
rustdoc-args = ["--cfg", "<name>_docs_rs"]

Then in build.rs you can do things like:

if cfg!(<name>_docs_rs) { ... }

@kornelski
Copy link
Author

kornelski commented May 16, 2018

I wasn't aware of the metadata table!

[package.metadata.docs.rs]
features = [ "docs-only" ]

That would work for me.

@ogoffart
Copy link

The [package.metadata.docs.rs] features = [ "docs-only" ] Did not work for me while attempting a generic solution for the cpp crate (mystor/rust-cpp#57 )
The first problem is that i forgot to add a [feature] docs-only=[] then the compilation of the cpp_build failed in https://docs.rs/crate/cpp_build/0.5.2/builds/165974 I did not know this was required. But that's easy to fix.

A more complicated problem is that it seems that this package.metadata.docs.rs] only work when generating the documentation for the crate itself, but not when compiling the dependencies.
So a crate that depends on cpp_build did not try to compile it with the feature, as this can be seen in https://docs.rs/crate/qmetaobject/0.1.0/builds/165979 :

   Compiling cpp_build v0.5.2

This succeed, while it should have failed (because of the missing [feature] section in cpp-build) Therefore showing that the feature was not passed to the crate.

Any idea how to solve this?

@jyn514
Copy link
Member

jyn514 commented Mar 20, 2020

cfg(doc) is now native to rustdoc, would that solve your issue?

@retep998
Copy link
Member

Can that be detected by build scripts?

@jyn514
Copy link
Member

jyn514 commented Mar 21, 2020

Hmm, I don't think so. I'll see if we can add an environment variable.

@jyn514
Copy link
Member

jyn514 commented Mar 21, 2020

Mentoring instructions:

  • In execute_build, set an environment variable when calling cargo (maybe DOCS_RS=1?). It would be nice to add a link to this issue as a comment as well.
  • Add documentation to templates/about.hbs, maybe near the top where we mention the version of rustc being used.

See also the contributing guidelines. Feel free to reach out here or on the Discord #docs-rs channel if you need help!

@jyn514 jyn514 added E-easy Effort: Should be easy to implement and would make a good first PR mentor This has instructions for getting started labels Mar 21, 2020
@jyn514
Copy link
Member

jyn514 commented Jun 24, 2020

docs.rs now sets DOCS_RS=1 when compiling crates.

@thewh1teagle
Copy link

thewh1teagle commented Dec 25, 2024

docs.rs now sets DOCS_RS=1 when compiling crates.

How can we detect it also when invoking cargo doc?

@syphar
Copy link
Member

syphar commented Dec 26, 2024

docs.rs now sets DOCS_RS=1 when compiling crates.

How can we detect it also when invoking cargo doc?

@thewh1teagle I don't know of any environment variable that is set, more idiomatically you can use #[cfg(doc)] and #[cfg(not(doc))]

@jyn514
Copy link
Member

jyn514 commented Dec 26, 2024

if you need to detect docs.rs specifically you can use package.metadata.docs.rs.features = [“my-special-feature”]. that will also let other people reuse the code when they depend on your crate, although they still have to know the feature exists at all.

@epage
Copy link

epage commented Dec 26, 2024

The docs.rs documentation has a page on this topic: https://docs.rs/about/builds#detecting-docsrs

@syphar
Copy link
Member

syphar commented Dec 26, 2024

if you need to detect docs.rs specifically you can use package.metadata.docs.rs.features = [“my-special-feature”]. that will also let other people reuse the code when they depend on your crate, although they still have to know the feature exists at all.

the last question here was about detecting cargo doc, not detecting docs.rs

@epage
Copy link

epage commented Dec 26, 2024

The issue for detecting rustdoc is rust-lang/cargo#8811

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
E-easy Effort: Should be easy to implement and would make a good first PR mentor This has instructions for getting started
Projects
None yet
Development

Successfully merging a pull request may close this issue.

10 participants