Skip to content

Stabilize cargo script#16569

Open
epage wants to merge 3 commits intorust-lang:masterfrom
epage:script-stable
Open

Stabilize cargo script#16569
epage wants to merge 3 commits intorust-lang:masterfrom
epage:script-stable

Conversation

@epage
Copy link
Copy Markdown
Contributor

@epage epage commented Jan 30, 2026

View all comments

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.rs loads config relative to foo
  • Pseudo-config defaults
    [build]
    build-dir = "{cargo-cache-home}/build/{workspace-path-hash}"
    target-dir = "{build-dir}/target"
    [resolver]
    lockfile-path = "{build-dir}/{workspace-path-hash}/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 (fix(script): Make the lockfile script-specific independent of build-dir #16619)
  • 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 (fix(script): Load config relative to the script #16620)
  • 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
Copy Markdown
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 thread src/bin/cargo/commands/run.rs Outdated
Comment on lines +99 to +100
match manifest_path.is_file() {
true => {}
Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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 thread src/bin/cargo/commands/run.rs
@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
Copy Markdown
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
Copy Markdown
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:

Concerns:

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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
Member

rustfmt styling frontmatter according to the Style Guide

rust-analyzer handling manifest in frontmatter

playground support

All these teams actually

@weihanglo
Copy link
Copy Markdown
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
Copy Markdown
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.

Comment thread src/bin/cargo/main.rs Outdated
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
Copy Markdown
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.

@rustbot

This comment has been minimized.

@rustbot

This comment has been minimized.

@rust-rfcbot rust-rfcbot added finished-final-comment-period FCP complete to-announce and removed final-comment-period FCP — a period for last comments before action is taken labels Feb 20, 2026
@rust-rfcbot
Copy link
Copy Markdown
Collaborator

The final comment period, with a disposition to merge, as per the review above, is now complete.

As the automated representative of the governance process, I would like to thank the author for their work and everyone else who contributed.

@rustbot

This comment has been minimized.

@rustbot

This comment has been minimized.

@rustbot

This comment has been minimized.

@rustbot

This comment has been minimized.

}

#[cargo_test(nightly, reason = "-Zscript is unstable")]
#[cargo_test(nightly, reason = "`#[feature(frontmatter]` hasn't hit stable yet")]
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

That attribute seems typoed throughout, shouldn't it be:

Suggested change
#[cargo_test(nightly, reason = "`#[feature(frontmatter]` hasn't hit stable yet")]
#[cargo_test(nightly, reason = "`#![feature(frontmatter)]` hasn't hit stable yet")]

@rustbot

This comment has been minimized.

Comment thread src/doc/man/cargo.md Outdated

`cargo` _script_ operate on system or user level, not project level.
This means that the local [configuration discovery] is ignored.
Instead, the configuration discovery begins at `$CARGO_HOME/config.toml`.
Copy link
Copy Markdown
Member

@Mark-Simulacrum Mark-Simulacrum Apr 8, 2026

Choose a reason for hiding this comment

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

View changes since the review

Is this documentation outdated? If I'm interpreting #16620 correctly, Cargo will read config.toml from $SCRIPT_FILE_DIRECTORY/config.toml, or am I misreading that PR?

The comment in that PR notes that it operates like cargo install --path, which I think means these docs should say what that does? (https://doc.rust-lang.org/nightly/cargo/commands/cargo-install.html#configuration-discovery)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks. This has gone through several changes and I forgot to make sure this aligned with the latest version.

@ehuss
Copy link
Copy Markdown
Contributor

ehuss commented Apr 9, 2026

I should have done this earlier. I think there are some concerns that are worth addressing before merging.

The concern about edition behavior is described in rust-lang/rust#152254.

@rfcbot concern edition-behavior

Additionally, I think there is a security concern that we should take very seriously. The interaction of cargo-script with RFC#3279 could make it very easy to expose a user to dangerous behavior. This is also described somewhat in rust-lang/rust#152254.

@rfcbot concern security

@rust-rfcbot rust-rfcbot added the proposed-final-comment-period An FCP proposal has started, but not yet signed off. label Apr 9, 2026
@rustbot

This comment has been minimized.

@ehuss ehuss moved this from FCP merge to FCP blocked in Cargo status tracker Apr 14, 2026
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Apr 14, 2026

☔ The latest upstream changes (possibly #16851) made this pull request unmergeable. Please resolve the merge conflicts.

@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Apr 17, 2026

This PR was rebased onto a different master commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

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 finished-final-comment-period FCP complete 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 to-announce

Projects

Status: FCP blocked

Development

Successfully merging this pull request may close these issues.

Tracking Issue for cargo-script RFC 3424

8 participants