Skip to content

Conversation

@epage
Copy link
Contributor

@epage epage commented Oct 16, 2025

The hope is this will improve the editor experience for cargo scripts even if there isn't full support yet. This is a first step towards #15318.

This copies over Cargo's frontmatter parser, rather than using rustc's because rustc's is embedded in the tokenizer and requires the lexer to be stateful. r-a intentionally prevents the tokenizer from being stateful.

        // Re-create the tokenizer from scratch every token because `GuardedStrPrefix` is one token in the lexer
        // but we want to split it to two in edition <2024.

Edits to Cargo's frontmatter parser were kept to a minimum with documentation linking it back to the source, for easier editing.

Cargo could expose this as a crate but not feeling up for taking on a new crate atm and we could get into a weird position with release cycles as most internal crates follow the release train, being published on stabilization.

Cargo's frontmatter parser counts the end of a shebang slightly differently than rustc_lexer, causing some other tests to be updated. Cargo's behavior more closely represents the Reference. While that is non-normative, I think this representation makes more logical sense.

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Oct 16, 2025
Copy link
Member

@ShoyuVanilla ShoyuVanilla left a comment

Choose a reason for hiding this comment

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

I believe that the implementation is good but I'm not totally confident with this "extra parser for frontmatter" design choice(I think it would be okay though). @rust-lang/rust-analyzer sorry for the ping, but do you have any ideas?

@Veykril
Copy link
Member

Veykril commented Oct 20, 2025

Yea I think this is fine. I don't think lifting this out of cargo as a crate is worth it either (even if there is a risk of "desyncing" here (maybe the cargo side could get a comment saying that changes there should ideally be mirrored to r-a). Either way I think this is a decent way to handle this for now especially given that this is a non-invasive change 👍

Thanks Ed!

@Veykril Veykril added this pull request to the merge queue Oct 20, 2025
Merged via the queue into rust-lang:master with commit e78de70 Oct 20, 2025
15 checks passed
@rustbot rustbot removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Oct 20, 2025
@epage epage deleted the frontmatter branch October 20, 2025 14:49
@lnicola
Copy link
Member

lnicola commented Oct 24, 2025

changelog feat (first contribution) parse script frontmatter

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants