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

201/pass cfg flags #214

Merged
merged 4 commits into from
Aug 16, 2023
Merged

Conversation

NathanFrasier
Copy link
Contributor

@NathanFrasier NathanFrasier commented Aug 7, 2023

This is a little rough. I feel like there has to be a better way to get rid of some of the calls to .push() and .extend() but I wasn't thinking of them.

This still needs documentation as well.

Addresses #201

@xFrednet xFrednet added C-enhancement Category: New feature or request A-marker-adapter Area: Adapter A-marker-cargo Area: All things connected to `cargo_marker` S-waiting-on-review Status: Awaiting review labels Aug 7, 2023
@xFrednet xFrednet added this to the v0.2.0 milestone Aug 7, 2023
Copy link
Member

@xFrednet xFrednet left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a nice start. I haven't tested the cfg part myself yet, but believe that this will work, based on your comments in the issue.

For testing the environment value, I can recommend using the --test-setup argument for dogfood. It'll print out the environment values, instead of running marker, after everything is compiled. For me, it roughly looked like this:

$ cargo dogfood --test-setup
# [...]
env:RUSTC_WORKSPACE_WRAPPER=.../marker/target/debug/marker_rustc_driver
env:MARKER_LINT_CRATES=marker_lints:.../marker/target/marker/lints/libmarker_lints.so;marker_uilints:.../marker/target/marker/lints/libmarker_uilints.so
info:toolchain=nightly-2023-07-13
info:marker-api=0.1.1

(With #213 merged it will become cargo dogfood test-setup).

I left some comments, as always, please don't hesitate to ask if you have any questions.

cargo-marker/src/backend/lints/build.rs Show resolved Hide resolved
Comment on lines 49 to +51
for lint_src in sources {
build_lint(lint_src, config)?;
}

// Find lint lint crates
match std::fs::read_dir(&lints_dir) {
Ok(dir) => {
let ending = OsStr::new(DYNAMIC_LIB_FILE_ENDING);
let mut lints = vec![];
for file in dir {
let file = file.unwrap().path();
if file.extension() == Some(ending) {
lints.push(LintCrate { file });
match std::fs::read_dir(&lints_dir) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As you noted above, it feels inefficient to scan the directory after every compilation. cargo metadata sadly doesn't provide any information how the build file will be called.

I see a few solutions:

  1. Leaving it like this, it seems to work

  2. Your suggestion of changing the target directory per crate

  3. Guess the output file name and error if the name can't be found. The dynamic library names are constructed by adding a lib prefix on unix systems and the OS-specific file ending. Here is a draft of the code:

    // Probably store these above as constants with `DYNAMIC_LIB_FILE_ENDING`
    #[cfg(unix)]
    let lib_file_prefix = "lib";
    #[cfg(windows)]
    let lib_file_prefix = "";
    
    // ...
    let dir = &lints_dir;
    for lint_src in sources {
        build_lint(lint_src, config)?;
    
        let name = lint_src.name.clone();
        let file = dir.join(format!("{lib_file_prefix}{name}.{DYNAMIC_LIB_FILE_ENDING}"));
        if file.exists() {
            lints.push(LintCrate { name, file });
        }
    }
  4. Guess the file name with more confidence. Option 3 would fail, if the user specified a name field in the [lib] section, as that changes the name. This is where we can do some more hackery:

    Cargo's build command supports the --config KEY=VALUE argument, to override parts of the manifest (At least from my understanding). We should be able to use this during lint compilation to prevent any weird renames, by setting the name ourself: --config "lib.name = '<lint_crate_name>'". Then we can just apply option 3 to confidently guess that the file actually exists and uses the name we expect.

Would you mind looking at option 4? It's totally fine if not, this is already a really nice addition to Marker. Option 2, 3 and 4 can also be added later :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can absolutely look into number 4, But I'd like to focus on getting this in and the user documentation ready first. I'm inclined to handle making this path more efficient as a separate issue, but If you think it should be part of this, I can work on it here.

I was trying to think if we'd get any real sandboxing type benefits from option 2, but I don't really see a point in making sure that some lints can't use build artifacts of others. it would just be more file system usage with little benefit. Besides, if there's one thing I learned writing C#, it's that windows doesn't like deeply nested files, so don't make folders unless you need them :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For me it's fine if we continue this implementation and do the path cleanup in a later PR. This system should have the same robustness level :)

marker_adapter/src/loader.rs Show resolved Hide resolved
marker_rustc_driver/src/main.rs Outdated Show resolved Hide resolved
marker_rustc_driver/src/main.rs Outdated Show resolved Hide resolved
@xFrednet
Copy link
Member

xFrednet commented Aug 7, 2023

Btw, can you also add a test case in the uilints crate?

The CI checks that the cargo-marker crate builds on stable, we can use this to test that #[cfg_atte(marker, allow(...))] allows crates to build on stable

@NathanFrasier NathanFrasier marked this pull request as ready for review August 8, 2023 02:43
Copy link
Member

@xFrednet xFrednet left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This version looks good to me, I have three comments, which will hopefully be simple. Thank you for writing the documentation!

@@ -1,3 +1,29 @@
# Usage

This section is intended for users who want to install Marker and run provided lints.

## Controlling Marker Lints
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you move this into a separate file/section in the usage folder? (Similar to #212)

And maybe Setting Lint Levels would be a more descriptive title

marker_uilints/tests/ui/cfg_attr_check.rs Show resolved Hide resolved
Comment on lines 11 to 15
Marker provides this functionality via the `#[register_tool()]` attribute currently available in
[nightly](https://github.com/rust-lang/rust/issues/66079). If your crate is compiled using nightly, then controlling
lints is as simple as placing `#![feature(register_tool)]` and `#![register_tool(marker)]` at the top of your crate
`lib.rs` or `mod.rs` file. You can then use all of the normal lint attributes you might usually use, on marker provided
lints, like `#[allow(marker::my_lint)]`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would make this statement a bit stronger, saying that rustc requires the feature. Maybe:

Marker's lints use the `marker::` tool prefix. Rustc requires the [`register_tool` feature](https://github.com/rust-lang/rust/issues/66079) to allow this prefix in the normal lint level attributes, like `#[allow(...)]` and `#[deny(...)]`....

Marker's lints use the marker:: tool prefix. Rustc requires the register_tool feature to allow this prefix in the normal lint level attributes, like #[allow(...)] and #[deny(...)]....

It's likely that most projects use a stable version by default. It would be nice to have a small code block, which could be directly copied. This should probably work:

#[cfg_attr(marker, feature(register_tool))]
#[cfg_attr(marker, register_tool(marker))]

@NathanFrasier
Copy link
Contributor Author

@xFrednet I know I've been absent from the project for a minute. I'm going on vacation after this week and would like to get this in, rather than leave you hanging waiting on the few changes left on here. I plan to rebase to current and make the changes mentioned above, is there anything else? I saw you self-assigned a few days ago.

@xFrednet
Copy link
Member

Hey @NathanFrasier, no problem at all, thank you for coming back and wanting to finish it! This week, I'm also taking it a bit easy. For the PR, only the documentation and test update were left. I'll review your rebased changes tomorrow. The self assignment, was just to give me an overview :)

Copy link
Member

@xFrednet xFrednet left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! This version looks perfect, I just have three small remarks about the docs. Do you have the time to push an update before your vacation? Otherwise, I'm more than happy to merge this and add them on master. The implementation is really solid, and I'm looking forward to including this in v0.2.0

docs/book/src/usage/setting-lint-levels.md Outdated Show resolved Hide resolved
docs/book/src/usage/setting-lint-levels.md Outdated Show resolved Hide resolved
docs/book/src/usage/setting-lint-levels.md Outdated Show resolved Hide resolved
Copy link
Member

@xFrednet xFrednet left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This version looks excellent, thank you for working on this issue and finishing the PR!

Enjoy your vacation! ⛱️

bors r+

@bors
Copy link
Contributor

bors bot commented Aug 16, 2023

Build succeeded!

The publicly hosted instance of bors-ng is deprecated and will go away soon.

If you want to self-host your own instance, instructions are here.
For more help, visit the forum.

If you want to switch to GitHub's built-in merge queue, visit their help page.

@bors bors bot merged commit dc477cf into rust-marker:master Aug 16, 2023
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-marker-adapter Area: Adapter A-marker-cargo Area: All things connected to `cargo_marker` C-enhancement Category: New feature or request S-waiting-on-review Status: Awaiting review
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants