Skip to content

Commit

Permalink
feat(cli): Accept 'cargo Cargo.toml'
Browse files Browse the repository at this point in the history
This wasn't in the original Pre-RFC but in terms of consistently
accepting manifests everything, I think this makes sense.
  • Loading branch information
epage committed Jun 17, 2023
1 parent 476a1f7 commit f11ebdd
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 9 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
13 changes: 8 additions & 5 deletions tests/testsuite/script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`
<tab>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();
Expand Down

0 comments on commit f11ebdd

Please sign in to comment.