rust-marker / marker

An experimental linting interface for Rust. Let's make custom lints a reality
https://rust-marker.github.io/marker/
Other
144 stars 11 forks source link

[Feat]: Make `cargo test --doc` work with the lint crates to doc test examples #303

Open Veetaha opened 1 year ago

Veetaha commented 1 year ago

Summary

Today if you run cargo test --doc you'll see the following error

warning: doc tests are not supported for crate type(s) `cdylib` in package `marker_lints`
error: no library targets found in package `marker_lints`

There are no good solutions to this problem. Same problem is also described in https://github.com/paritytech/cargo-contract/issues/494.

We could recommend setting crate-type = ["cdylib", "rlib"] to make the doc tests work, but it means that the lint crates would be compiled as a static library in addition to a dynamic one. I suppose that it will influence the lint crates compilation performance by compiling an additional unnecessary library target.

We could provide a command in cargo-marker that adds the rlib crate type temporarily for doc tests. But that will look ugly if we need to modify the Cargo.toml of the crate. What if the process is killed? The modifications to the Cargo.toml file will persist and be potentially committed to the version control. Copying the project to a temp directory would solve this, but it may have a bunch of edge cases on its own as well...

xFrednet commented 1 year ago

Is it maybe possible to change the target based on a cfg flag? That could be a neat option, but I haven't seen anything like that before.

Veetaha commented 1 year ago

There is an ability to override the crate type via cargo rustc --crate-type cdylib. This way the lint crates could just omit the lib.crate-type declaration in the Cargo.toml. So cargo-marker doesn't even need to rely on lint crate users to do that.

The downside is that cargo rustc works only on one crate at a time. We can't pass multiple --package parameters to cargo rustc.