Skip to content

Commit 0e648c3

Browse files
committed
fix(script): Be quiet on programmatic output
The goal is that we shouldn't interefere with end-user output when "cargo script"s are used programmatically. The only way to detect this is when piping. CI will also look like this. My thought is that if someone does want to do `#!/usr/bin/env -S cargo -v`, it should have a consistent meaning between local development (`cargo run --manifest-path`) and "script mode" (`cargo`), so I effectively added a new verbosity level in these cases. To get normal output in all cases, add a `-v` like the tests do. Do `-vv` if you want the normal `-v` mode. If you want it always quiet, do `--quiet`. I want to see the default verbosity for interactive "script mode" a bit quieter to the point that all normal output cargo makes is cleared before running the built binary. I am holding off on that now as that could tie into bigger conversations / refactors (see https://rust-lang.zulipchat.com/#narrow/stream/246057-t-cargo/topic/Re-thinking.20cargo's.20output).
1 parent 2790779 commit 0e648c3

File tree

3 files changed

+45
-34
lines changed

3 files changed

+45
-34
lines changed

src/bin/cargo/cli.rs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ Run with 'cargo -Z [FLAG] [COMMAND]'",
176176
}
177177
};
178178
let exec = Exec::infer(cmd)?;
179-
config_configure(config, &expanded_args, subcommand_args, global_args)?;
179+
config_configure(config, &expanded_args, subcommand_args, global_args, &exec)?;
180180
super::init_git(config);
181181

182182
exec.exec(config, subcommand_args)
@@ -364,12 +364,26 @@ fn config_configure(
364364
args: &ArgMatches,
365365
subcommand_args: &ArgMatches,
366366
global_args: GlobalArgs,
367+
exec: &Exec,
367368
) -> CliResult {
368369
let arg_target_dir = &subcommand_args.value_of_path("target-dir", config);
369-
let verbose = global_args.verbose + args.verbose();
370+
let mut verbose = global_args.verbose + args.verbose();
370371
// quiet is unusual because it is redefined in some subcommands in order
371372
// to provide custom help text.
372-
let quiet = args.flag("quiet") || subcommand_args.flag("quiet") || global_args.quiet;
373+
let mut quiet = args.flag("quiet") || subcommand_args.flag("quiet") || global_args.quiet;
374+
if matches!(exec, Exec::Manifest(_)) && !quiet {
375+
// Verbosity is shifted quieter for `Exec::Manifest` as it is can be used as if you ran
376+
// `cargo install` and we especially shouldn't pollute programmatic output.
377+
//
378+
// For now, interactive output has the same default output as `cargo run` but that is
379+
// subject to change.
380+
if let Some(lower) = verbose.checked_sub(1) {
381+
verbose = lower;
382+
} else if !config.shell().is_err_tty() {
383+
// Don't pollute potentially-scripted output
384+
quiet = true;
385+
}
386+
}
373387
let global_color = global_args.color; // Extract so it can take reference.
374388
let color = args
375389
.get_one::<String>("color")

src/doc/src/reference/unstable.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1483,6 +1483,7 @@ A parameter is identified as a manifest-command if it has one of:
14831483

14841484
Differences between `cargo run --manifest-path <path>` and `cargo <path>`
14851485
- `cargo <path>` runs with the config for `<path>` and not the current dir, more like `cargo install --path <path>`
1486+
- `cargo <path>` is at a verbosity level below the normal default. Pass `-v` to get normal output.
14861487

14871488
### `[lints]`
14881489

tests/testsuite/script.rs

Lines changed: 27 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ fn basic_rs() {
2626
.file("echo.rs", ECHO_SCRIPT)
2727
.build();
2828

29-
p.cargo("-Zscript echo.rs")
29+
p.cargo("-Zscript -v echo.rs")
3030
.masquerade_as_nightly_cargo(&["script"])
3131
.with_stdout(
3232
r#"bin: [..]/debug/echo[EXE]
@@ -50,7 +50,7 @@ fn basic_path() {
5050
.file("echo", ECHO_SCRIPT)
5151
.build();
5252

53-
p.cargo("-Zscript ./echo")
53+
p.cargo("-Zscript -v ./echo")
5454
.masquerade_as_nightly_cargo(&["script"])
5555
.with_stdout(
5656
r#"bin: [..]/debug/echo[EXE]
@@ -74,7 +74,7 @@ fn basic_cargo_toml() {
7474
.file("src/main.rs", ECHO_SCRIPT)
7575
.build();
7676

77-
p.cargo("-Zscript Cargo.toml")
77+
p.cargo("-Zscript -v Cargo.toml")
7878
.masquerade_as_nightly_cargo(&["script"])
7979
.with_stdout(
8080
r#"bin: target/debug/foo[EXE]
@@ -97,7 +97,7 @@ fn path_required() {
9797
.file("echo", ECHO_SCRIPT)
9898
.build();
9999

100-
p.cargo("-Zscript echo")
100+
p.cargo("-Zscript -v echo")
101101
.masquerade_as_nightly_cargo(&["script"])
102102
.with_status(101)
103103
.with_stdout("")
@@ -126,7 +126,7 @@ fn manifest_precedence_over_plugins() {
126126
path.push(p.root().join("path-test"));
127127
let path = std::env::join_paths(path.iter()).unwrap();
128128

129-
p.cargo("-Zscript echo.rs")
129+
p.cargo("-Zscript -v echo.rs")
130130
.env("PATH", &path)
131131
.masquerade_as_nightly_cargo(&["script"])
132132
.with_stdout(
@@ -157,7 +157,7 @@ fn warn_when_plugin_masks_manifest_on_stable() {
157157
path.push(p.root().join("path-test"));
158158
let path = std::env::join_paths(path.iter()).unwrap();
159159

160-
p.cargo("echo.rs")
160+
p.cargo("-v echo.rs")
161161
.env("PATH", &path)
162162
.with_stdout("")
163163
.with_stderr(
@@ -176,7 +176,7 @@ fn requires_nightly() {
176176
.file("echo.rs", ECHO_SCRIPT)
177177
.build();
178178

179-
p.cargo("echo.rs")
179+
p.cargo("-v echo.rs")
180180
.with_status(101)
181181
.with_stdout("")
182182
.with_stderr(
@@ -193,7 +193,7 @@ fn requires_z_flag() {
193193
.file("echo.rs", ECHO_SCRIPT)
194194
.build();
195195

196-
p.cargo("echo.rs")
196+
p.cargo("-v echo.rs")
197197
.masquerade_as_nightly_cargo(&["script"])
198198
.with_status(101)
199199
.with_stdout("")
@@ -221,7 +221,7 @@ fn main() {
221221
.file("script.rs", script)
222222
.build();
223223

224-
p.cargo("-Zscript script.rs")
224+
p.cargo("-Zscript -v script.rs")
225225
.masquerade_as_nightly_cargo(&["script"])
226226
.with_stdout(
227227
r#"Hello world!
@@ -252,7 +252,7 @@ fn main() {
252252
.file("script.rs", script)
253253
.build();
254254

255-
p.cargo("-Zscript script.rs")
255+
p.cargo("-Zscript -v script.rs")
256256
.masquerade_as_nightly_cargo(&["script"])
257257
.with_stdout(
258258
r#"Hello world!
@@ -281,7 +281,7 @@ fn main() {
281281
.file("script.rs", script)
282282
.build();
283283

284-
p.cargo("-Zscript script.rs")
284+
p.cargo("-Zscript -v script.rs")
285285
.masquerade_as_nightly_cargo(&["script"])
286286
.with_stdout(
287287
r#"msg = undefined
@@ -298,7 +298,7 @@ fn main() {
298298
.run();
299299

300300
// Verify we don't rebuild
301-
p.cargo("-Zscript script.rs")
301+
p.cargo("-Zscript -v script.rs")
302302
.masquerade_as_nightly_cargo(&["script"])
303303
.with_stdout(
304304
r#"msg = undefined
@@ -314,7 +314,7 @@ fn main() {
314314
.run();
315315

316316
// Verify we do rebuild
317-
p.cargo("-Zscript script.rs")
317+
p.cargo("-Zscript -v script.rs")
318318
.env("_MESSAGE", "hello")
319319
.masquerade_as_nightly_cargo(&["script"])
320320
.with_stdout(
@@ -374,7 +374,7 @@ args: ["-NotAnArg"]
374374
}
375375

376376
#[cargo_test]
377-
fn default_verbosity() {
377+
fn default_programmatic_verbosity() {
378378
let script = ECHO_SCRIPT;
379379
let p = cargo_test_support::project()
380380
.file("script.rs", script)
@@ -389,10 +389,6 @@ args: ["-NotAnArg"]
389389
)
390390
.with_stderr(
391391
"\
392-
[WARNING] `package.edition` is unspecifiead, defaulting to `2021`
393-
[COMPILING] script v0.0.0 ([ROOT]/foo)
394-
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]s
395-
[RUNNING] `[..]/debug/script[EXE] -NotAnArg`
396392
",
397393
)
398394
.run();
@@ -431,7 +427,7 @@ fn main() {
431427
.file("script.rs", script)
432428
.build();
433429

434-
p.cargo("-Zscript script.rs")
430+
p.cargo("-Zscript -v script.rs")
435431
.masquerade_as_nightly_cargo(&["script"])
436432
.with_stdout(
437433
r#"line: 4
@@ -455,7 +451,7 @@ fn test_escaped_hyphen_arg() {
455451
.file("script.rs", script)
456452
.build();
457453

458-
p.cargo("-Zscript -- script.rs -NotAnArg")
454+
p.cargo("-Zscript -v -- script.rs -NotAnArg")
459455
.masquerade_as_nightly_cargo(&["script"])
460456
.with_stdout(
461457
r#"bin: [..]/debug/script[EXE]
@@ -480,7 +476,7 @@ fn test_unescaped_hyphen_arg() {
480476
.file("script.rs", script)
481477
.build();
482478

483-
p.cargo("-Zscript script.rs -NotAnArg")
479+
p.cargo("-Zscript -v script.rs -NotAnArg")
484480
.masquerade_as_nightly_cargo(&["script"])
485481
.with_stdout(
486482
r#"bin: [..]/debug/script[EXE]
@@ -505,7 +501,7 @@ fn test_same_flags() {
505501
.file("script.rs", script)
506502
.build();
507503

508-
p.cargo("-Zscript script.rs --help")
504+
p.cargo("-Zscript -v script.rs --help")
509505
.masquerade_as_nightly_cargo(&["script"])
510506
.with_stdout(
511507
r#"bin: [..]/debug/script[EXE]
@@ -530,7 +526,7 @@ fn test_name_has_weird_chars() {
530526
.file("s-h.w§c!.rs", script)
531527
.build();
532528

533-
p.cargo("-Zscript s-h.w§c!.rs")
529+
p.cargo("-Zscript -v s-h.w§c!.rs")
534530
.masquerade_as_nightly_cargo(&["script"])
535531
.with_stdout(
536532
r#"bin: [..]/debug/s-h-w-c-[EXE]
@@ -553,7 +549,7 @@ fn script_like_dir() {
553549
.file("script.rs/foo", "something")
554550
.build();
555551

556-
p.cargo("-Zscript script.rs")
552+
p.cargo("-Zscript -v script.rs")
557553
.masquerade_as_nightly_cargo(&["script"])
558554
.with_status(101)
559555
.with_stderr(
@@ -568,7 +564,7 @@ error: manifest path `script.rs` is a directory but expected a file
568564
fn missing_script_rs() {
569565
let p = cargo_test_support::project().build();
570566

571-
p.cargo("-Zscript script.rs")
567+
p.cargo("-Zscript -v script.rs")
572568
.masquerade_as_nightly_cargo(&["script"])
573569
.with_status(101)
574570
.with_stderr(
@@ -596,7 +592,7 @@ fn main() {
596592
.file("script.rs", script)
597593
.build();
598594

599-
p.cargo("-Zscript script.rs --help")
595+
p.cargo("-Zscript -v script.rs --help")
600596
.masquerade_as_nightly_cargo(&["script"])
601597
.with_stdout(
602598
r#"Hello world!
@@ -636,7 +632,7 @@ fn main() {
636632
.file("bar/src/lib.rs", "pub fn bar() {}")
637633
.build();
638634

639-
p.cargo("-Zscript script.rs --help")
635+
p.cargo("-Zscript -v script.rs --help")
640636
.masquerade_as_nightly_cargo(&["script"])
641637
.with_stdout(
642638
r#"Hello world!
@@ -666,7 +662,7 @@ fn main() {
666662
.file("build.rs", "broken")
667663
.build();
668664

669-
p.cargo("-Zscript script.rs --help")
665+
p.cargo("-Zscript -v script.rs --help")
670666
.masquerade_as_nightly_cargo(&["script"])
671667
.with_stdout(
672668
r#"Hello world!
@@ -695,7 +691,7 @@ fn main() {
695691
.file("src/bin/not-script/main.rs", "fn main() {}")
696692
.build();
697693

698-
p.cargo("-Zscript script.rs --help")
694+
p.cargo("-Zscript -v script.rs --help")
699695
.masquerade_as_nightly_cargo(&["script"])
700696
.with_stdout(
701697
r#"Hello world!
@@ -719,7 +715,7 @@ fn implicit_target_dir() {
719715
.file("script.rs", script)
720716
.build();
721717

722-
p.cargo("-Zscript script.rs")
718+
p.cargo("-Zscript -v script.rs")
723719
.masquerade_as_nightly_cargo(&["script"])
724720
.with_stdout(
725721
r#"bin: [ROOT]/home/.cargo/target/[..]/debug/script[EXE]
@@ -747,7 +743,7 @@ fn no_local_lockfile() {
747743

748744
assert!(!local_lockfile_path.exists());
749745

750-
p.cargo("-Zscript script.rs")
746+
p.cargo("-Zscript -v script.rs")
751747
.masquerade_as_nightly_cargo(&["script"])
752748
.with_stdout(
753749
r#"bin: [ROOT]/home/.cargo/target/[..]/debug/script[EXE]

0 commit comments

Comments
 (0)