-
Notifications
You must be signed in to change notification settings - Fork 649
Add cargo add
instruction to crate-sidebar
#5875
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
Conversation
Awesome feature! Thanks! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, thank you!
I'm wondering about adding the version to the command, but that probably doesn't need to block this PR. let me know if I should merge as is and then we can still improve this afterwards :)
app/components/crate-sidebar.js
Outdated
@@ -14,6 +14,10 @@ export default class CrateSidebar extends Component { | |||
return homepage && (!repository || simplifyUrl(repository) !== simplifyUrl(homepage)); | |||
} | |||
|
|||
get cargoAddCommand() { | |||
return `cargo add ${this.args.crate.name}`; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return `cargo add ${this.args.crate.name}`; | |
return `cargo add ${this.args.crate.name}@${this.args.version.num}`; |
any thoughts about including the version number, like we do for the Cargo.toml
snippet?
I understand that cargo add
will automatically use the latest version, but if you're viewing the page of a specific version then maybe it would make sense to include the version in the command?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point! I didn't consider version pages. I added a commit that adds the version only on version detail pages. The regular crate detail page still shows the versionless variant. That makes the most sense, I think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point! I didn't consider version pages. I added a commit that adds the version only on version detail pages. The regular crate detail page still shows the versionless variant. That makes the most sense, I think?
This is exactly the solution I was imagining when I was considering this idea as cargo-add
was being merged into cargo.
Thanks for taking this on! I originally held off on getting this done as I wanted more time for versions of cargo
with add
to proliferate but lost track of coming back to this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That was a good reason to delay it. I was surprised, looking up, that we got it in Rust 1.62.0 in June. Almost half a year ago! Doesn't feel that way. 😅
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
awesome! thanks again! :)
Is there a way for a crate to specify that it's meant to be a dev dependency only? I am an author for a crate that explicitly only wants to be added as a dev dependency because it modifies a lot of code at compile time to make structs mockable. While just adding the dependency alone won't hurt anything my README explicitly states to only add it as a dev dependency to make it fool proof that nothing bad can happen outside of tests. While I understand the motivation for this change I think it unintentionally implies that all crates are meant to be installed the same which is not the case which could cause issues for crates that intentionally want to only be for tests or during build. |
@nrxus Sorry for the previous That said, I've found that even crates usually meant to be used as dev dependencies can be useful in other contexts, like code generator tools, perhaps. I suppose you could check for If you only want to discourage (and not prohibit) your create from being used as a normal dependency, a way to specify that (like a flag in the manifest) would be nice. |
@nrxus I don't think there are currently any flags implemented to indicate that. I'm open to the suggestion though. Do you have any thoughts on how we could implement it? |
The most flexible thing would be to an extra field in [package]
name = "faux"
install-command = "cargo add --dev" |
@nrxus I don't think that this is ideal, because someone malicious could do something like that: install-command = "rm -rf *" I would opt for something like this: add-as = "dev" # or "build" It's a little uglier and less flexible, but safe. Otherwise you'd have to sanitize/check the install-command for every crate. |
That makes sense to me 👍 |
This adds a
cargo add
instruction to the crate sidebar, with clipboard support:This came about after @beeb made a good point that copying a snippet from crates.io prevents typosquatting, and not having a snippet for
cargo add
is reason to avoid it.