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

Let cargo test and cargo commands with --examples ignore examples that are not for the current target #7233

Closed
najamelan opened this issue Aug 11, 2019 · 5 comments
Labels
A-cross-compiling Area: using --target flag for other platforms A-examples Area: example targets C-feature-request Category: proposal for a feature. Before PR, ping rust-lang/cargo if this is not `Feature accepted`

Comments

@najamelan
Copy link

Describe the problem you are trying to solve
Currently cargo test --target wasm32-unkown-unkown will try to compile all examples. This is a problem if there is code that uses dev-dependencies that are not available on this target, like networking crates.

Describe the solution you'd like
This already works for files in the tests directory, but not for examples:
I would like to see an example ignored when it has: #![ cfg(not( target_arch = "wasm32" )) ] at the top of the file.

Currently this throws an error like:

error[E0601]: `main` function not found in crate `basic`
  |
  = note: consider adding a `main` function to `examples/basic.rs`

The only workaround I have found is to put a cfg attribute on each toplevel statement and to add a dummy main tagged with a cfg for wasm. This is problematic if you have a bunch of free standing functions and type definitions, especially since examples are a form of documentation.

Ideally also cargo would have a nice error message if a user tries to run an example on the wrong target. That is, if there is a positive cfg limiting the example to a single target, telling them to run it with cargo run --example basic --target wasm32-unknown-unknown and if there is a negative cfg, telling them: This example cannot run on target xxx.

@najamelan najamelan added the C-feature-request Category: proposal for a feature. Before PR, ping rust-lang/cargo if this is not `Feature accepted` label Aug 11, 2019
@najamelan
Copy link
Author

ps: I haven't tested what the score is with features, but I imagine it would be handy to in/exclude examples depending on whether a feature is turned on or not.

@najamelan
Copy link
Author

I have a similar problem with a crate that compiles on stable, but has examples that require nightly. Now cargo test can no longer be run on stable.

@najamelan
Copy link
Author

najamelan commented Aug 16, 2019

I made some progress without modifying cargo:

  • I tried package.autoexamples = false + [[target."cfg(not( target_arch = \"wasm32\" ))".example]] but that failed. It seems that the target selection is specific to dependencies?
  • I managed a workaround with package.autoexamples = false, a notwasm feature and using required-features = notwasm in the example section. The downside of this is that users can only run examples with: cargo run --example basic --features notwasm, which is unfortunate and also one needs to list all examples in Cargo.toml. I suppose this technique could also work with a nightly feature to allow tests to run in stable.

I suppose a solution to #4663 would solve most of the user facing problem here.

@4LT
Copy link

4LT commented Apr 15, 2022

I'm having the similar problem, but with an example that uses #![cfg(feature = "std")]. Thought it would be worth mentioning since the issue is broader than just targets, but not worth creating a separate issue for.

@epage
Copy link
Contributor

epage commented Nov 1, 2023

#9208 sounds like its wanting to solve the same problem, so I'm going to close this to consolidate the conversation. If there is something that I missed, let us know!

@epage epage closed this as not planned Won't fix, can't repro, duplicate, stale Nov 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-cross-compiling Area: using --target flag for other platforms A-examples Area: example targets C-feature-request Category: proposal for a feature. Before PR, ping rust-lang/cargo if this is not `Feature accepted`
Projects
None yet
Development

No branches or pull requests

4 participants