Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cli): Support cargo Cargo.toml #12281

Merged
merged 2 commits into from
Jun 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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