Skip to content

Commit 6b0b5a8

Browse files
committed
docs(unstable): Expand on manifest commands so far
This is written to reflect the current implementation though some parts might read a little weird because I didn't want to write throw-away documentation for when we change this. For example, single-file packages are currently only supported in `cargo <command>` and not as manifest paths but this will change.
1 parent 33c9d8e commit 6b0b5a8

File tree

2 files changed

+97
-2
lines changed

2 files changed

+97
-2
lines changed

src/bin/cargo/cli.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -490,9 +490,9 @@ pub fn cli() -> Command {
490490
#[allow(clippy::disallowed_methods)]
491491
let is_rustup = std::env::var_os("RUSTUP_HOME").is_some();
492492
let usage = if is_rustup {
493-
"cargo [+toolchain] [OPTIONS] [COMMAND]"
493+
"cargo [+toolchain] [OPTIONS] [COMMAND]\n cargo [+toolchain] [OPTIONS] -Zscript <MANIFEST_RS> [ARGS]..."
494494
} else {
495-
"cargo [OPTIONS] [COMMAND]"
495+
"cargo [OPTIONS] [COMMAND]\n cargo [OPTIONS] -Zscript <MANIFEST_RS> [ARGS]..."
496496
};
497497
Command::new("cargo")
498498
// Subcommands all count their args' display order independently (from 0),

src/doc/src/reference/unstable.md

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1397,6 +1397,101 @@ Valid operations are the following:
13971397

13981398
* Tracking Issue: [#12207](https://github.com/rust-lang/cargo/issues/12207)
13991399

1400+
Cargo can directly run `.rs` files as:
1401+
```console
1402+
$ cargo -Zscript file.rs
1403+
```
1404+
where `file.rs` can be as simple as:
1405+
```rust
1406+
fn main() {}
1407+
```
1408+
1409+
A user may optionally specify a manifest in a `cargo` code fence in a module-level comment, like:
1410+
```rust
1411+
#!/usr/bin/env cargo
1412+
1413+
//! ```cargo
1414+
//! [dependencies]
1415+
//! clap = { version = "4.2", features = ["derive"] }
1416+
//! ```
1417+
1418+
use clap::Parser;
1419+
1420+
#[derive(Parser, Debug)]
1421+
#[clap(version)]
1422+
struct Args {
1423+
#[clap(short, long, help = "Path to config")]
1424+
config: Option<std::path::PathBuf>,
1425+
}
1426+
1427+
fn main() {
1428+
let args = Args::parse();
1429+
println!("{:?}", args);
1430+
}
1431+
```
1432+
1433+
#### Single-file packages
1434+
1435+
In addition to today's multi-file packages (`Cargo.toml` file with other `.rs`
1436+
files), we are adding the concept of single-file packages which may contain an
1437+
embedded manifest. There is no required distinguishment for a single-file
1438+
`.rs` package from any other `.rs` file.
1439+
1440+
A single-file package may contain an embedded manifest. An embedded manifest
1441+
is stored using `TOML` in a markdown code-fence with `cargo` at the start of the
1442+
infostring inside a target-level doc-comment. It is an error to have multiple
1443+
`cargo` code fences in the target-level doc-comment. We can relax this later,
1444+
either merging the code fences or ignoring later code fences.
1445+
1446+
Supported forms of embedded manifest are:
1447+
``````rust
1448+
//! ```cargo
1449+
//! ```
1450+
``````
1451+
``````rust
1452+
/*!
1453+
* ```cargo
1454+
* ```
1455+
*/
1456+
``````
1457+
1458+
Inferred / defaulted manifest fields:
1459+
- `package.name = <slugified file stem>`
1460+
- `package.version = "0.0.0"` to [call attention to this crate being used in unexpected places](https://matklad.github.io/2021/08/22/large-rust-workspaces.html#Smaller-Tips)
1461+
- `package.publish = false` to avoid accidental publishes, particularly if we
1462+
later add support for including them in a workspace.
1463+
- `package.edition = <current>` to avoid always having to add an embedded
1464+
manifest at the cost of potentially breaking scripts on rust upgrades
1465+
- Warn when `edition` is unspecified. While with single-file packages this will be
1466+
silenced by default, users wanting stability are also likely to be using
1467+
other commands, like `cargo test` and will see it.
1468+
1469+
Disallowed manifest fields:
1470+
- `[workspace]`, `[lib]`, `[[bin]]`, `[[example]]`, `[[test]]`, `[[bench]]`
1471+
- `package.workspace`, `package.build`, `package.links`, `package.autobins`, `package.autoexamples`, `package.autotests`, `package.autobenches`
1472+
1473+
As the primary role for these files is exploratory programming which has a high
1474+
edit-to-run ratio, building should be fast. Therefore `CARGO_TARGET_DIR` will
1475+
be shared between single-file packages to allow reusing intermediate build
1476+
artifacts.
1477+
1478+
The lockfile for single-file packages will be placed in `CARGO_TARGET_DIR`. In
1479+
the future, when workspaces are supported, that will allow a user to have a
1480+
persistent lockfile.
1481+
1482+
#### Manifest-commands
1483+
1484+
You may pass single-file packages directly to the `cargo` command, without subcommand. This is mostly intended for being put in `#!` lines.
1485+
1486+
The precedence for how to interpret `cargo <subcommand>` is
1487+
1. Built-in xor single-file packages
1488+
2. Aliases
1489+
3. External subcommands
1490+
1491+
A parameter is identified as a single-file package if it has one of:
1492+
- Path separators
1493+
- A `.rs` extension
1494+
14001495
### `[lints]`
14011496

14021497
* Tracking Issue: [#12115](https://github.com/rust-lang/cargo/issues/12115)

0 commit comments

Comments
 (0)