Skip to content

Commit

Permalink
Auto merge of #12281 - epage:toml, r=weihanglo
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
bors committed Jun 21, 2023
2 parents dead4b8 + 7f2eca4 commit 3de1cc4
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/bin/cargo/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,7 @@ pub fn cli() -> Command {
let usage = if is_rustup {
"cargo [+toolchain] [OPTIONS] [COMMAND]\n cargo [+toolchain] [OPTIONS] -Zscript <MANIFEST_RS> [ARGS]..."
} else {
"cargo [OPTIONS] [COMMAND]\n cargo [OPTIONS] -Zscript <MANIFEST_RS> [ARGS]..."
"cargo [OPTIONS] [COMMAND]\n cargo [OPTIONS] -Zscript <MANIFEST> [ARGS]..."
};
Command::new("cargo")
// Subcommands all count their args' display order independently (from 0),
Expand Down
4 changes: 3 additions & 1 deletion src/bin/cargo/commands/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
7 changes: 5 additions & 2 deletions src/doc/src/reference/unstable.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <subcommand>` 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]`

Expand Down
23 changes: 23 additions & 0 deletions tests/testsuite/script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,29 @@ 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_stdout(
r#"bin: target/debug/foo[EXE]
args: []
"#,
)
.with_stderr(
"\
[COMPILING] foo v0.0.1 ([ROOT]/foo)
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]s
[RUNNING] `target/debug/foo[EXE]`
",
)
.run();
}

#[cargo_test]
fn path_required() {
let p = cargo_test_support::project()
Expand Down

0 comments on commit 3de1cc4

Please sign in to comment.