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

Show desc of well known subcommands (fmt, clippy) in cargo --list #9848

Merged
merged 1 commit into from
Aug 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion src/bin/cargo/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,23 @@ use cargo::core::{features, CliUnstable};
use cargo::{self, drop_print, drop_println, CliResult, Config};
use clap::{AppSettings, Arg, ArgMatches};
use itertools::Itertools;
use std::collections::HashMap;

use super::commands;
use super::list_commands;
use crate::command_prelude::*;
use cargo::core::features::HIDDEN;

lazy_static::lazy_static! {
// Maps from commonly known external commands (not builtin to cargo) to their
// description, for the help page. Reserved for external subcommands that are
// core within the rust ecosystem (esp ones that might become internal in the future).
static ref KNOWN_EXTERNAL_COMMAND_DESCRIPTIONS: HashMap<&'static str, &'static str> = vec![
("clippy", "Checks a package to catch common mistakes and improve your Rust code."),
("fmt", "Formats all bin and lib files of the current crate using rustfmt."),
].into_iter().collect();
}

pub fn main(config: &mut Config) -> CliResult {
// CAUTION: Be careful with using `config` until it is configured below.
// In general, try to avoid loading config values unless necessary (like
Expand Down Expand Up @@ -100,14 +111,22 @@ Run with 'cargo -Z [FLAG] [SUBCOMMAND]'",
if args.is_present("list") {
drop_println!(config, "Installed Commands:");
for (name, command) in list_commands(config) {
let known_external_desc = KNOWN_EXTERNAL_COMMAND_DESCRIPTIONS.get(name.as_str());
match command {
CommandInfo::BuiltIn { about } => {
assert!(
known_external_desc.is_none(),
"KNOWN_EXTERNAL_COMMANDS shouldn't contain builtin \"{}\"",
name
);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

this should help us catch / update this if (for example) fmt becomes a builtin in the future

let summary = about.unwrap_or_default();
let summary = summary.lines().next().unwrap_or(&summary); // display only the first line
drop_println!(config, " {:<20} {}", name, summary);
}
CommandInfo::External { path } => {
if is_verbose {
if let Some(desc) = known_external_desc {
drop_println!(config, " {:<20} {}", name, desc);
} else if is_verbose {
drop_println!(config, " {:<20} {}", name, path.display());
} else {
drop_println!(config, " {}", name);
Expand Down
25 changes: 25 additions & 0 deletions tests/testsuite/cargo_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,31 @@ fn list_command_looks_at_path() {
);
}

#[cargo_test]
fn list_command_handles_known_external_commands() {
let p = project()
.executable(Path::new("path-test").join("cargo-fmt"), "")
.build();

let fmt_desc = " fmt Formats all bin and lib files of the current crate using rustfmt.";

// Without path - fmt isn't there
p.cargo("--list")
.env("PATH", "")
.with_stdout_does_not_contain(fmt_desc)
.run();

// With path - fmt is there with known description
let mut path = path();
path.push(p.root().join("path-test"));
let path = env::join_paths(path.iter()).unwrap();

p.cargo("--list")
.env("PATH", &path)
.with_stdout_contains(fmt_desc)
.run();
}

#[cargo_test]
fn list_command_resolves_symlinks() {
let proj = project()
Expand Down