Skip to content

Commit

Permalink
feat(workspace): Print all members even default-members is specified
Browse files Browse the repository at this point in the history
  • Loading branch information
linyihai committed Feb 17, 2025
1 parent 1e2f579 commit 3c409c9
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 23 deletions.
16 changes: 6 additions & 10 deletions src/cargo/util/command_prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -823,7 +823,7 @@ Run `{cmd}` to see possible targets."
};

if let Some(ws) = workspace {
self.check_optional_opts(ws, &opts)?;
self.check_optional_opts(ws)?;
} else if self.is_present_with_zero_values("package") {
// As for cargo 0.50.0, this won't occur but if someone sneaks in
// we can still provide this informative message for them.
Expand Down Expand Up @@ -909,29 +909,25 @@ Run `{cmd}` to see possible targets."
}
}

fn check_optional_opts(
&self,
workspace: &Workspace<'_>,
compile_opts: &CompileOptions,
) -> CargoResult<()> {
fn check_optional_opts(&self, workspace: &Workspace<'_>) -> CargoResult<()> {
if self.is_present_with_zero_values("package") {
print_available_packages(workspace)?
}

if self.is_present_with_zero_values("example") {
print_available_examples(workspace, compile_opts)?;
print_available_examples(workspace)?;
}

if self.is_present_with_zero_values("bin") {
print_available_binaries(workspace, compile_opts)?;
print_available_binaries(workspace)?;
}

if self.is_present_with_zero_values("bench") {
print_available_benches(workspace, compile_opts)?;
print_available_benches(workspace)?;
}

if self.is_present_with_zero_values("test") {
print_available_tests(workspace, compile_opts)?;
print_available_tests(workspace)?;
}

Ok(())
Expand Down
24 changes: 11 additions & 13 deletions src/cargo/util/workspace.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::core::compiler::Unit;
use crate::core::manifest::TargetSourcePath;
use crate::core::{Target, Workspace};
use crate::ops::CompileOptions;
use crate::ops::Packages;
use crate::util::CargoResult;
use anyhow::bail;
use cargo_util::paths::normalize_path;
Expand All @@ -12,9 +12,8 @@ use std::path::PathBuf;
fn get_available_targets<'a>(
filter_fn: fn(&Target) -> bool,
ws: &'a Workspace<'_>,
options: &'a CompileOptions,
) -> CargoResult<Vec<&'a str>> {
let packages = options.spec.get_packages(ws)?;
let packages = Packages::All(Vec::new()).get_packages(ws)?;

let mut targets: Vec<_> = packages
.into_iter()
Expand All @@ -35,11 +34,10 @@ fn get_available_targets<'a>(
fn print_available_targets(
filter_fn: fn(&Target) -> bool,
ws: &Workspace<'_>,
options: &CompileOptions,
option_name: &str,
plural_name: &str,
) -> CargoResult<()> {
let targets = get_available_targets(filter_fn, ws, options)?;
let targets = get_available_targets(filter_fn, ws)?;

let mut output = String::new();
writeln!(output, "\"{}\" takes one argument.", option_name)?;
Expand Down Expand Up @@ -79,20 +77,20 @@ pub fn print_available_packages(ws: &Workspace<'_>) -> CargoResult<()> {
bail!("{}", output)
}

pub fn print_available_examples(ws: &Workspace<'_>, options: &CompileOptions) -> CargoResult<()> {
print_available_targets(Target::is_example, ws, options, "--example", "examples")
pub fn print_available_examples(ws: &Workspace<'_>) -> CargoResult<()> {
print_available_targets(Target::is_example, ws, "--example", "examples")
}

pub fn print_available_binaries(ws: &Workspace<'_>, options: &CompileOptions) -> CargoResult<()> {
print_available_targets(Target::is_bin, ws, options, "--bin", "binaries")
pub fn print_available_binaries(ws: &Workspace<'_>) -> CargoResult<()> {
print_available_targets(Target::is_bin, ws, "--bin", "binaries")
}

pub fn print_available_benches(ws: &Workspace<'_>, options: &CompileOptions) -> CargoResult<()> {
print_available_targets(Target::is_bench, ws, options, "--bench", "bench targets")
pub fn print_available_benches(ws: &Workspace<'_>) -> CargoResult<()> {
print_available_targets(Target::is_bench, ws, "--bench", "bench targets")
}

pub fn print_available_tests(ws: &Workspace<'_>, options: &CompileOptions) -> CargoResult<()> {
print_available_targets(Target::is_test, ws, options, "--test", "test targets")
pub fn print_available_tests(ws: &Workspace<'_>) -> CargoResult<()> {
print_available_targets(Target::is_test, ws, "--test", "test targets")
}

/// The source path and its current dir for use in compilation.
Expand Down
1 change: 1 addition & 0 deletions tests/testsuite/workspaces.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2758,6 +2758,7 @@ fn print_available_targets_within_virtual_workspace() {
[ERROR] "--bin" takes one argument.
Available binaries:
crate1
crate2
"#]])
Expand Down

0 comments on commit 3c409c9

Please sign in to comment.