Skip to content

Consistently default to --lib --bins when --all-targets is not given #5146

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

Closed
wants to merge 6 commits into from
Closed
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
7 changes: 6 additions & 1 deletion src/bin/command_prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,11 @@ pub trait ArgMatchesExt {
}
};

let all_targets = if mode.all_targets_by_default() {
true
} else {
self._is_present("all-targets")
};
let opts = CompileOptions {
config,
jobs: self.jobs()?,
Expand All @@ -245,7 +250,7 @@ pub trait ArgMatchesExt {
self._values_of("test"), self._is_present("tests"),
self._values_of("example"), self._is_present("examples"),
self._values_of("bench"), self._is_present("benches"),
self._is_present("all-targets")),
all_targets),
message_format,
target_rustdoc_args: None,
target_rustc_args: None,
Expand Down
3 changes: 2 additions & 1 deletion src/bin/commands/rustc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ pub fn exec(config: &mut Config, args: &ArgMatches) -> CliResult {
let mut compile_opts = args.compile_options_for_single_package(
config, mode,
)?;
compile_opts.target_rustc_args = Some(values(args, "args"));
let target_args = values(args, "args");
compile_opts.target_rustc_args = if target_args.is_empty() { None } else { Some(target_args) };
ops::compile(&ws, &compile_opts)?;
Ok(())
}
5 changes: 3 additions & 2 deletions src/bin/commands/rustdoc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub fn cli() -> App {
"Build all tests",
"Build only the specified bench target",
"Build all benches",
"Build all targets (default)",
"Build all targets (lib and bin targets by default)",
)
.arg_release("Build artifacts in release mode, with optimizations")
.arg_manifest_path()
Expand All @@ -45,7 +45,8 @@ pub fn exec(config: &mut Config, args: &ArgMatches) -> CliResult {
let mut compile_opts = args.compile_options_for_single_package(
config, CompileMode::Doc { deps: false },
)?;
compile_opts.target_rustdoc_args = Some(values(args, "args"));
let target_args = values(args, "args");
compile_opts.target_rustdoc_args = if target_args.is_empty() { None } else { Some(target_args) };
let doc_opts = DocOptions {
open_result: args.is_present("open"),
compile_opts,
Expand Down
17 changes: 15 additions & 2 deletions src/cargo/ops/cargo_compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,15 @@ pub enum CompileMode {
Doctest,
}

impl CompileMode {
pub fn all_targets_by_default(&self) -> bool {
match self {
&CompileMode::Test | &CompileMode::Bench => true,
_ => false
}
}
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum MessageFormat {
Human,
Expand Down Expand Up @@ -422,15 +431,19 @@ impl CompileFilter {

pub fn need_dev_deps(&self) -> bool {
match *self {
CompileFilter::Default { .. } => true,
CompileFilter::Default { .. } => false,
CompileFilter::Only { ref examples, ref tests, ref benches, .. } =>
examples.is_specific() || tests.is_specific() || benches.is_specific()
}
}

pub fn matches(&self, target: &Target) -> bool {
match *self {
CompileFilter::Default { .. } => true,
CompileFilter::Default { .. } => match *target.kind() {
TargetKind::Bin => true,
TargetKind::Lib(..) => true,
_ => false,
},
CompileFilter::Only { lib, ref bins, ref examples, ref tests, ref benches, .. } => {
let rule = match *target.kind() {
TargetKind::Bin => bins,
Expand Down
54 changes: 47 additions & 7 deletions tests/testsuite/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4235,6 +4235,50 @@ fn build_filter_infer_profile() {
);
}

#[test]
fn all_targets_with_and_without() {
let p = project("foo")
.file("Cargo.toml", r#"
[package]
name = "foo"
version = "0.1.0"
authors = []
"#)
.file("src/main.rs", "fn main() {}")
.build();
assert_that(p.cargo("build").arg("-v").arg("--all-targets"),
execs().with_status(0)
// bin
.with_stderr_contains("\
[RUNNING] `rustc --crate-name foo src[/]main.rs --crate-type bin \
--emit=dep-info,link[..]")
// bench
.with_stderr_contains("\
[RUNNING] `rustc --crate-name foo src[/]main.rs --emit=dep-info,link \
-C opt-level=3 --test [..]")
// unit test
.with_stderr_contains("\
[RUNNING] `rustc --crate-name foo src[/]main.rs --emit=dep-info,link \
-C debuginfo=2 --test [..]")
);
assert_that(p.cargo("clean"), execs().with_status(0));
assert_that(p.cargo("build").arg("-v"),
execs().with_status(0)
// bin
.with_stderr_contains("\
[RUNNING] `rustc --crate-name foo src[/]main.rs --crate-type bin \
--emit=dep-info,link[..]")
// bench
.with_stderr_does_not_contain("\
[RUNNING] `rustc --crate-name foo src[/]main.rs --emit=dep-info,link \
-C opt-level=3 --test [..]")
// unit test
.with_stderr_does_not_contain("\
[RUNNING] `rustc --crate-name foo src[/]main.rs --emit=dep-info,link \
-C debuginfo=2 --test [..]")
);
}

#[test]
fn all_targets_no_lib() {
let p = project("foo")
Expand Down Expand Up @@ -4310,13 +4354,9 @@ fn avoid_dev_deps() {
.file("src/main.rs", "fn main() {}")
.build();

// --bins is needed because of #5134
assert_that(p.cargo("build").arg("--bins"),
execs().with_status(101));
assert_that(p.cargo("build").arg("--bins")
.masquerade_as_nightly_cargo()
.arg("-Zavoid-dev-deps"),
execs().with_status(0));
assert_that(p.cargo("build"), execs().with_status(101));
assert_that(p.cargo("build").masquerade_as_nightly_cargo()
.arg("-Zavoid-dev-deps"), execs().with_status(0));
}

#[test]
Expand Down
11 changes: 10 additions & 1 deletion tests/testsuite/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,7 @@ fn check_virtual_all_implied() {
}

#[test]
fn check_all_targets() {
fn all_targets_with_and_without() {
let foo = project("foo")
.file("Cargo.toml", SIMPLE_MANIFEST)
.file("src/main.rs", "fn main() {}")
Expand All @@ -495,6 +495,15 @@ fn check_all_targets() {
.with_stderr_contains("[..] --crate-name test2 tests[/]test2.rs [..]")
.with_stderr_contains("[..] --crate-name bench3 benches[/]bench3.rs [..]")
);
assert_that(foo.cargo("clean"), execs().with_status(0));
assert_that(foo.cargo("check").arg("-v"),
execs().with_status(0)
.with_stderr_contains("[..] --crate-name foo src[/]lib.rs [..]")
.with_stderr_contains("[..] --crate-name foo src[/]main.rs [..]")
.with_stderr_does_not_contain("[..] --crate-name example1 examples[/]example1.rs [..]")
.with_stderr_does_not_contain("[..] --crate-name test2 tests[/]test2.rs [..]")
.with_stderr_does_not_contain("[..] --crate-name bench3 benches[/]bench3.rs [..]")
);
}

#[test]
Expand Down
14 changes: 4 additions & 10 deletions tests/testsuite/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -920,11 +920,8 @@ fn dev_dependencies_no_check() {
.file("src/main.rs", "fn main() {}")
.build();

// --bins is needed because of #5134
assert_that(p.cargo("build").arg("--bins"),
execs().with_status(101));
assert_that(p.cargo("install").arg("--bins"),
execs().with_status(0));
assert_that(p.cargo("build"), execs().with_status(101));
assert_that(p.cargo("install"), execs().with_status(0));
}

#[test]
Expand All @@ -950,12 +947,9 @@ fn dev_dependencies_lock_file_untouched() {
.file("a/src/lib.rs", "")
.build();

// --bins is needed because of #5134
assert_that(p.cargo("build").arg("--bins"),
execs().with_status(0));
assert_that(p.cargo("build"), execs().with_status(0));
let lock = p.read_lockfile();
assert_that(p.cargo("install").arg("--bins"),
execs().with_status(0));
assert_that(p.cargo("install"), execs().with_status(0));
let lock2 = p.read_lockfile();
assert!(lock == lock2, "different lockfiles");
}
Expand Down
44 changes: 44 additions & 0 deletions tests/testsuite/rustc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,50 @@ fn build_only_bar_dependency() {
"));
}

#[test]
fn all_targets_with_and_without() {
let p = project("foo")
.file("Cargo.toml", r#"
[package]
name = "foo"
version = "0.1.0"
authors = []
"#)
.file("src/main.rs", "fn main() {}")
.build();
assert_that(p.cargo("rustc").arg("-v").arg("--all-targets"),
execs().with_status(0)
// bin
.with_stderr_contains("\
[RUNNING] `rustc --crate-name foo src[/]main.rs --crate-type bin \
--emit=dep-info,link[..]")
// bench
.with_stderr_contains("\
[RUNNING] `rustc --crate-name foo src[/]main.rs --emit=dep-info,link \
-C opt-level=3 --test [..]")
// unit test
.with_stderr_contains("\
[RUNNING] `rustc --crate-name foo src[/]main.rs --emit=dep-info,link \
-C debuginfo=2 --test [..]")
);
assert_that(p.cargo("clean"), execs().with_status(0));
assert_that(p.cargo("rustc").arg("-v"),
execs().with_status(0)
// bin
.with_stderr_contains("\
[RUNNING] `rustc --crate-name foo src[/]main.rs --crate-type bin \
--emit=dep-info,link[..]")
// bench
.with_stderr_does_not_contain("\
[RUNNING] `rustc --crate-name foo src[/]main.rs --emit=dep-info,link \
-C opt-level=3 --test [..]")
// unit test
.with_stderr_does_not_contain("\
[RUNNING] `rustc --crate-name foo src[/]main.rs --emit=dep-info,link \
-C debuginfo=2 --test [..]")
);
}

#[test]
fn fail_with_multiple_packages() {
let foo = project("foo")
Expand Down