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

Print message to help user manage false positives #80

Merged
merged 3 commits into from
Aug 29, 2023

Conversation

tgnottingham
Copy link
Contributor

When unused dependencies are found, print a message that tells the user how to manage false positives.

Example output:

Analyzing dependencies of crates in this directory...
cargo-machete found the following unused dependencies in /home/tgnottingham/example:
example -- /home/tgnottingham/example/Cargo.toml:
        fxhash

If you believe cargo-machete has detected an unused dependency incorrectly, you may add it
to the list of dependencies to ignore in the `[package.metadata.cargo-machete]` section of
the appropriate Cargo.toml file. For example:

[package.metadata.cargo-machete]
ignored = ["prost"]

Done!

@bnjbvr
Copy link
Owner

bnjbvr commented Aug 20, 2023

Thanks, that's a nice touch. Do you think it'd make sense to publicize the --with-metadata option too, as it could find other unused dependencies based on their public names?

@tgnottingham
Copy link
Contributor Author

Sure, that makes sense to me.

When user doesn't pass --with-metadata:

Analyzing dependencies of crates in this directory...
cargo-machete found the following unused dependencies in /home/tgnottingham/example:
example -- /home/tgnottingham/example/Cargo.toml:
        fxhash
        xml-rs

If you believe cargo-machete has detected an unused dependency incorrectly,
you can try running it with the `--with-metadata` flag for better accuracy,
though this may modify your Cargo.lock files.

You can also add the dependency to the list of dependencies to ignore in the
`[package.metadata.cargo-machete]` section of the appropriate Cargo.toml file.
For example:

[package.metadata.cargo-machete]
ignored = ["prost"]

Done!

When user does pass --with-metadata (here, xml-rs turned out to be used):

Analyzing dependencies of crates in this directory...
cargo-machete found the following unused dependencies in /home/tgnottingham/example:
example -- /home/tgnottingham/example/Cargo.toml:
        fxhash

If you believe cargo-machete has detected an unused dependency incorrectly,
you can add the dependency to the list of dependencies to ignore in the
`[package.metadata.cargo-machete]` section of the appropriate Cargo.toml file.
For example:

[package.metadata.cargo-machete]
ignored = ["prost"]

Done!

Copy link
Owner

@bnjbvr bnjbvr left a comment

Choose a reason for hiding this comment

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

Nice! I think we can avoid a bit of text duplication, and this should be good to go 👍

src/main.rs Outdated
@@ -211,6 +211,33 @@ fn run_machete() -> anyhow::Result<bool> {
}
}

if has_unused_dependencies {
if !args.with_metadata {
println!("\n\
Copy link
Owner

Choose a reason for hiding this comment

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

nit: we could use a raw string here:

println!(r#"my

long

string");

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 tried, but the formatting was very unpleasant with a raw string, especially after cargo fmt got its hands on it.

src/main.rs Outdated
);
} else {
println!("\n\
If you believe cargo-machete has detected an unused dependency incorrectly,\n\
Copy link
Owner

Choose a reason for hiding this comment

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

Could we use constant str to avoid the code duplication here? This would also help spot that we're showing almost the same text, and that would highlight what the differences are too.

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 gave something like that a shot, but the result reads "inside-out", which is awkward:

    if has_unused_dependencies {
        let middle = if !args.with_metadata {
            "you can try running it with the `--with-metadata` flag for better accuracy,\n\
            though this may modify your Cargo.lock files.\n\
            \n\
            You can also add the dependency to the list of dependencies to ignore in the\n"
        } else {
            "you can add the dependency to the list of dependencies to ignore in the\n"
        };

        println!(
            "\n\
            If you believe cargo-machete has detected an unused dependency incorrectly,\n\
            {middle}\
            `[package.metadata.cargo-machete]` section of the appropriate Cargo.toml.\n\
            For example:\n\
            \n\
            [package.metadata.cargo-machete]\n\
            ignored = [\"prost\"]"
        );
    }

Maybe I'm missing an obvious way to make this less contorted.

In the end, I moved the part which is only applicable sometimes -- the --with-metadata suggestion -- to the end. That simplifies things, though it obviously means the --with-metadata part is mentioned second, which I originally didn't like as much. But it seems fine. Anyway, let me know what you prefer here.

@tgnottingham
Copy link
Contributor Author

After reducing some duplication, without --with-metadata:

Analyzing dependencies of crates in this directory...
cargo-machete found the following unused dependencies in /home/tgnottingham/example:
example -- /home/tgnottingham/example/Cargo.toml:
        ahash

If you believe cargo-machete has detected an unused dependency incorrectly,
you can add the dependency to the list of dependencies to ignore in the
`[package.metadata.cargo-machete]` section of the appropriate Cargo.toml.
For example:

[package.metadata.cargo-machete]
ignored = ["prost"]

You can also try running it with the `--with-metadata` flag for better accuracy,
though this may modify your Cargo.lock files.

Done!

With --with-metadata:

Analyzing dependencies of crates in this directory...
cargo-machete found the following unused dependencies in /home/tgnottingham/example:
example -- /home/tgnottingham/example/Cargo.toml:
        ahash

If you believe cargo-machete has detected an unused dependency incorrectly,
you can add the dependency to the list of dependencies to ignore in the
`[package.metadata.cargo-machete]` section of the appropriate Cargo.toml.
For example:

[package.metadata.cargo-machete]
ignored = ["prost"]

Done!

Copy link
Owner

@bnjbvr bnjbvr left a comment

Choose a reason for hiding this comment

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

Nice, thank you!

@bnjbvr bnjbvr merged commit 5ca864e into bnjbvr:main Aug 29, 2023
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

Successfully merging this pull request may close these issues.

2 participants