Skip to content

Commit 3de1cc4

Browse files
committed
Auto merge of #12281 - epage:toml, r=weihanglo
feat(cli): Support `cargo Cargo.toml` ### What does this PR try to resolve? This is making the assumption that we want full unity between places accepting both single-file packages and `Cargo.toml` for #12207. This has not been brought up before in any of the discussions (Internals, eRFC), so I can understand if there are concerns about this and we decide to hold off. We might want to resolve symlinks before this so people can have a prettier name for these. ### How should we test and review this PR? The test for this was added in a commit before the actual change, letting people see how the behavior changed.
2 parents dead4b8 + 7f2eca4 commit 3de1cc4

File tree

4 files changed

+32
-4
lines changed

4 files changed

+32
-4
lines changed

src/bin/cargo/cli.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -492,7 +492,7 @@ pub fn cli() -> Command {
492492
let usage = if is_rustup {
493493
"cargo [+toolchain] [OPTIONS] [COMMAND]\n cargo [+toolchain] [OPTIONS] -Zscript <MANIFEST_RS> [ARGS]..."
494494
} else {
495-
"cargo [OPTIONS] [COMMAND]\n cargo [OPTIONS] -Zscript <MANIFEST_RS> [ARGS]..."
495+
"cargo [OPTIONS] [COMMAND]\n cargo [OPTIONS] -Zscript <MANIFEST> [ARGS]..."
496496
};
497497
Command::new("cargo")
498498
// Subcommands all count their args' display order independently (from 0),

src/bin/cargo/commands/run.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,9 @@ pub fn exec(config: &mut Config, args: &ArgMatches) -> CliResult {
8787

8888
pub fn is_manifest_command(arg: &str) -> bool {
8989
let path = Path::new(arg);
90-
1 < path.components().count() || path.extension() == Some(OsStr::new("rs"))
90+
1 < path.components().count()
91+
|| path.extension() == Some(OsStr::new("rs"))
92+
|| path.file_name() == Some(OsStr::new("Cargo.toml"))
9193
}
9294

9395
pub fn exec_manifest_command(config: &Config, cmd: &str, args: &[OsString]) -> CliResult {

src/doc/src/reference/unstable.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1462,16 +1462,19 @@ persistent lockfile.
14621462

14631463
#### Manifest-commands
14641464

1465-
You may pass single-file packages directly to the `cargo` command, without subcommand. This is mostly intended for being put in `#!` lines.
1465+
You may pass a manifest directly to the `cargo` command, without a subcommand,
1466+
like `foo/Cargo.toml` or a single-file package like `foo.rs`. This is mostly
1467+
intended for being put in `#!` lines.
14661468

14671469
The precedence for how to interpret `cargo <subcommand>` is
14681470
1. Built-in xor single-file packages
14691471
2. Aliases
14701472
3. External subcommands
14711473

1472-
A parameter is identified as a single-file package if it has one of:
1474+
A parameter is identified as a manifest-command if it has one of:
14731475
- Path separators
14741476
- A `.rs` extension
1477+
- The file name is `Cargo.toml`
14751478

14761479
### `[lints]`
14771480

tests/testsuite/script.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,29 @@ args: []
6565
.run();
6666
}
6767

68+
#[cargo_test]
69+
fn basic_cargo_toml() {
70+
let p = cargo_test_support::project()
71+
.file("src/main.rs", ECHO_SCRIPT)
72+
.build();
73+
74+
p.cargo("-Zscript Cargo.toml")
75+
.masquerade_as_nightly_cargo(&["script"])
76+
.with_stdout(
77+
r#"bin: target/debug/foo[EXE]
78+
args: []
79+
"#,
80+
)
81+
.with_stderr(
82+
"\
83+
[COMPILING] foo v0.0.1 ([ROOT]/foo)
84+
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]s
85+
[RUNNING] `target/debug/foo[EXE]`
86+
",
87+
)
88+
.run();
89+
}
90+
6891
#[cargo_test]
6992
fn path_required() {
7093
let p = cargo_test_support::project()

0 commit comments

Comments
 (0)