Skip to content

Conversation

@epage
Copy link
Contributor

@epage epage commented Jan 30, 2026

Fixes #12207

RFC: https://rust-lang.github.io/rfcs/3502-cargo-script.html
Tracking issue: #12207
Frontmatter stabilization: rust-lang/rust#148051

This stabilizes an MVP of Cargo script

  • Cargo scripts are Rust source files without an extension or with a .rs extension
  • They may have a frontmatter block with manifest content
  • Changes from Cargo.toml
    • package.name is optional, defaults to the file stem
    • package.edition defaults to current edition, not 2015 edition, with a warning
    • package.build, package.links, package.default-run, package.workspace, package.auto-* are disallowed
    • workspace, build-dependencies, and any build-target tables are disallowed
    • Workspace auto-discovery does not run
  • package.name defaulting logic
    • strips leading numbers
      • If nothing left after this, calls it package
    • replaces non-UnicodeXID characters with - or _
    • commonly reserved names are allowed:
      • windows names, like con
      • sysroot names, like test
      • keywords, like self
    • disallowed names on a short-term basis:
      • build-dir directories like deps
  • cargo foo precedence:
    1. built-in command
    2. script if there is a .rs extension or a path component is present (e.g. ./foo )
    3. aliases
    4. plugins
  • Command support:
    • cargo install errors on publishing a script
    • cargo package errors on publishing a script
    • cargo publish errors on publishing a script
  • Script metadata
    • arg0 is set on a best-effort basis to the script's path
    • CARGO_MANIFEST_PATH was previously stabilized in addition to the existing CARGO_MANIFEST_DIR
  • cargo foo only loads config CARGO_HOME
    • Regular cargo run loads from current dir
    • Previous proposal was to load from scripts directory but there was concern over running scripts in a Downloads directory pulling in unexpected config
  • Pseudo-config defaults
    [build]
    build-dir = "{cargo-cache-home}/build/{workspace-path-hash}"
    target-dir = "{build-dir}/target"
    [resolver]
    lockfile-path = "{build-dir}/Cargo.lock"

Whats missing:

  • rustfmt styling frontmatter according to the Style Guide
  • rust-analyzer handling manifest in frontmatter
  • playground support

Known issues:

Risks

  • If people share their build-dir across projects, then multiple scripts will point to the same Cargo.lock, fighting over what is locked in it
  • Not allowing local config for scripts could become a problem as we get into workspace support as people would expect their repo's config to be respected when it won't be
  • Changing package name rules impacts programmatic output
  • Precedence rules for cargo foo
    • In particularly, requiring an extension or path element should be safe, especially for most uses of shebang but you can intentionally contrive a shebang and PATH that makes this not work

Future possibilities

@epage epage added the T-cargo Team: Cargo label Jan 30, 2026
@rustbot
Copy link
Collaborator

rustbot commented Jan 30, 2026

r? @weihanglo

rustbot has assigned @weihanglo.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added A-build-execution Area: anything dealing with executing the compiler A-cli Area: Command-line interface, option parsing, etc. A-cli-help Area: built-in command-line help A-documenting-cargo-itself Area: Cargo's documentation A-manifest Area: Cargo.toml issues A-unstable Area: nightly unstable support Command-run S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jan 30, 2026
@epage epage marked this pull request as draft January 30, 2026 14:32
@rustbot rustbot removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jan 30, 2026
Comment on lines 99 to 100
match manifest_path.is_file() {
true => {}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cleaned up in the next commit

Comment on lines +797 to 798
// TODO: remove once frontmatter hits nightly
if unit.pkg.manifest().is_embedded() {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Blocked on rust-lang/rust#148051 hitting nightly

Comment on lines +847 to 848
// TODO: remove once frontmatter hits nightly
if unit.pkg.manifest().is_embedded() {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Blocked on rust-lang/rust#148051 hitting nightly

@epage epage marked this pull request as ready for review January 30, 2026 15:39
@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jan 30, 2026
@epage
Copy link
Contributor Author

epage commented Jan 30, 2026

@rfcbot fcp merge T-cargo

For me, my biggest concerns are the first two Risks listed.

@rust-rfcbot
Copy link
Collaborator

rust-rfcbot commented Jan 30, 2026

Team member @epage has proposed to merge this. The next step is review by the rest of the tagged team members:

No concerns currently listed.

Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up!

See this document for info about what commands tagged team members can give me.

@rust-rfcbot rust-rfcbot added proposed-final-comment-period An FCP proposal has started, but not yet signed off. disposition-merge FCP with intent to merge labels Jan 30, 2026
Copy link
Member

@weihanglo weihanglo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we inform rustfmt team that we're going to stabilize this and leave frontmatter style issue unresolved? I remembered that let-else has caused confusions and frustrations when we stabilized it in the language syntax side but rustfmt didn't format it: rust-lang/rustfmt#4914.

At least the rustfmt team should be aware of the potential incoming volume of issues regarding this.

View changes since this review

in the [TOML] format. It contains metadata that is needed to compile the package. Checkout
the `cargo locate-project` section for more detail on how cargo finds the manifest file.

The content of the manifest can also be embedded inside of the frontmatter inside of a Rust source file:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a blocker but not satisfied with the current stable doc. I would love to see a dedicated section for Cargo scripts with some examples, and all other mentions of "script" to link back to that section. Currently it is a bit not obvious when it is mentioned.

The original unstable docs has some good stuff in it but maybe it depends on many stuff we want to commit as "stable"

(Or it was migrated to elsewhere? The diff is too large that I could have missed it)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was unsure how to handle it. A guide? If so, where does it fit in. A reference? Does it get its own page or a section on manifests?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or a reference-like doc in src/doc/man/cargo.md, and we have another standalone guide? In manifest is not too bad but script is more than manifest.

@weihanglo
Copy link
Member

rustfmt styling frontmatter according to the Style Guide

rust-analyzer handling manifest in frontmatter

playground support

All these teams actually

@weihanglo
Copy link
Member

Pseudo-config defaults

This is worth calling out in user doc that whether the target directory of script is considered implementation details.

@epage
Copy link
Contributor Author

epage commented Jan 31, 2026

Should we inform rustfmt team that we're going to stabilize this and leave frontmatter style issue unresolved? I remembered that let-else has caused confusions and frustrations when we stabilized it in the language syntax side but rustfmt didn't format it: rust-lang/rustfmt#4914.

At least the rustfmt team should be aware of the potential incoming volume of issues regarding this.

View changes since this review

This was handled as part of the Frontmatter stabilization.

in the [TOML] format. It contains metadata that is needed to compile the package. Checkout
the `cargo locate-project` section for more detail on how cargo finds the manifest file.

The content of the manifest can also be embedded inside of the frontmatter inside of a Rust source file:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or a reference-like doc in src/doc/man/cargo.md, and we have another standalone guide? In manifest is not too bad but script is more than manifest.

github-merge-queue bot pushed a commit that referenced this pull request Feb 2, 2026
### What does this PR try to resolve?

As found on #16569

### How to test and review this PR?
epage added a commit to epage/cargo that referenced this pull request Feb 2, 2026
This is to make space for discussing scripts, see rust-lang#16569
github-merge-queue bot pushed a commit that referenced this pull request Feb 3, 2026
### What does this PR try to resolve?

This is to make space for discussing scripts, see #16569

### How to test and review this PR?
@ehuss ehuss moved this to FCP merge in Cargo status tracker Feb 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-build-execution Area: anything dealing with executing the compiler A-cli Area: Command-line interface, option parsing, etc. A-cli-help Area: built-in command-line help A-documenting-cargo-itself Area: Cargo's documentation A-manifest Area: Cargo.toml issues A-unstable Area: nightly unstable support Command-run disposition-merge FCP with intent to merge proposed-final-comment-period An FCP proposal has started, but not yet signed off. S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-cargo Team: Cargo

Projects

Status: FCP merge

Development

Successfully merging this pull request may close these issues.

Tracking Issue for cargo-script RFC 3424

4 participants