From ffb997fd2d9878f7d2e17a183efa301a8ba27f8e Mon Sep 17 00:00:00 2001 From: Ed Page Date: Tue, 13 Jun 2023 12:14:06 -0500 Subject: [PATCH 1/2] test(cli): Verify you can't use Cargo.toml --- tests/testsuite/script.rs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/tests/testsuite/script.rs b/tests/testsuite/script.rs index 638677bc4b3..7863ecaedb1 100644 --- a/tests/testsuite/script.rs +++ b/tests/testsuite/script.rs @@ -65,6 +65,26 @@ args: [] .run(); } +#[cargo_test] +fn basic_cargo_toml() { + let p = cargo_test_support::project() + .file("src/main.rs", ECHO_SCRIPT) + .build(); + + p.cargo("-Zscript Cargo.toml") + .masquerade_as_nightly_cargo(&["script"]) + .with_status(101) + .with_stdout("") + .with_stderr( + "\ +error: no such command: `Cargo.toml` + +View all installed commands with `cargo --list` +", + ) + .run(); +} + #[cargo_test] fn path_required() { let p = cargo_test_support::project() From 7f2eca409f5e10bb66c0d61f43266f7882060062 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Tue, 13 Jun 2023 21:20:38 -0500 Subject: [PATCH 2/2] feat(cli): Accept 'cargo Cargo.toml' This wasn't in the original Pre-RFC but in terms of consistently accepting manifests everything, I think this makes sense. --- src/bin/cargo/cli.rs | 2 +- src/bin/cargo/commands/run.rs | 4 +++- src/doc/src/reference/unstable.md | 7 +++++-- tests/testsuite/script.rs | 13 ++++++++----- 4 files changed, 17 insertions(+), 9 deletions(-) diff --git a/src/bin/cargo/cli.rs b/src/bin/cargo/cli.rs index 57cb31c38d4..7b7725fa271 100644 --- a/src/bin/cargo/cli.rs +++ b/src/bin/cargo/cli.rs @@ -492,7 +492,7 @@ pub fn cli() -> Command { let usage = if is_rustup { "cargo [+toolchain] [OPTIONS] [COMMAND]\n cargo [+toolchain] [OPTIONS] -Zscript [ARGS]..." } else { - "cargo [OPTIONS] [COMMAND]\n cargo [OPTIONS] -Zscript [ARGS]..." + "cargo [OPTIONS] [COMMAND]\n cargo [OPTIONS] -Zscript [ARGS]..." }; Command::new("cargo") // Subcommands all count their args' display order independently (from 0), diff --git a/src/bin/cargo/commands/run.rs b/src/bin/cargo/commands/run.rs index 8ee7d3f9aa9..9db28bc626c 100644 --- a/src/bin/cargo/commands/run.rs +++ b/src/bin/cargo/commands/run.rs @@ -87,7 +87,9 @@ pub fn exec(config: &mut Config, args: &ArgMatches) -> CliResult { pub fn is_manifest_command(arg: &str) -> bool { let path = Path::new(arg); - 1 < path.components().count() || path.extension() == Some(OsStr::new("rs")) + 1 < path.components().count() + || path.extension() == Some(OsStr::new("rs")) + || path.file_name() == Some(OsStr::new("Cargo.toml")) } pub fn exec_manifest_command(config: &Config, cmd: &str, args: &[OsString]) -> CliResult { diff --git a/src/doc/src/reference/unstable.md b/src/doc/src/reference/unstable.md index afb2f7cbd61..d33980ff51e 100644 --- a/src/doc/src/reference/unstable.md +++ b/src/doc/src/reference/unstable.md @@ -1462,16 +1462,19 @@ persistent lockfile. #### Manifest-commands -You may pass single-file packages directly to the `cargo` command, without subcommand. This is mostly intended for being put in `#!` lines. +You may pass a manifest directly to the `cargo` command, without a subcommand, +like `foo/Cargo.toml` or a single-file package like `foo.rs`. This is mostly +intended for being put in `#!` lines. The precedence for how to interpret `cargo ` is 1. Built-in xor single-file packages 2. Aliases 3. External subcommands -A parameter is identified as a single-file package if it has one of: +A parameter is identified as a manifest-command if it has one of: - Path separators - A `.rs` extension +- The file name is `Cargo.toml` ### `[lints]` diff --git a/tests/testsuite/script.rs b/tests/testsuite/script.rs index 7863ecaedb1..bf4fc75f27b 100644 --- a/tests/testsuite/script.rs +++ b/tests/testsuite/script.rs @@ -73,13 +73,16 @@ fn basic_cargo_toml() { p.cargo("-Zscript Cargo.toml") .masquerade_as_nightly_cargo(&["script"]) - .with_status(101) - .with_stdout("") + .with_stdout( + r#"bin: target/debug/foo[EXE] +args: [] +"#, + ) .with_stderr( "\ -error: no such command: `Cargo.toml` - -View all installed commands with `cargo --list` +[COMPILING] foo v0.0.1 ([ROOT]/foo) +[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]s +[RUNNING] `target/debug/foo[EXE]` ", ) .run();