Skip to content

Reject unformatted lockfiles in preview - #385

Closed
zaniebot wants to merge 3 commits into
mainfrom
zb/check-lock-format
Closed

zaniebot wants to merge 3 commits into
mainfrom
zb/check-lock-format

Conversation

@zaniebot

@zaniebot zaniebot commented Jul 23, 2026 •

Copy link
Copy Markdown
Collaborator

Lockfiles can retain valid but non-canonical whitespace, creating avoidable merge conflicts while --locked continues to accept them. Add the lockfile-format-check preview feature and stream toml_parser tokens during locked checks to reject malformed indentation and separators with the offending line, keeping formatting out of the stable lockfile contract and allowing offline validation without a refresh.

@zaniebot

Copy link
Copy Markdown
Collaborator Author

Benchmark

I benchmarked the final streaming toml_parser check on a normal-sized lockfile and the 15.8 MB dependency lock that motivated this change. The late-format case adds one trailing space immediately before the final newline, so the TOML remains valid and the first formatting difference is on the final line; the checker must scan the entire file.

Fixture Bytes / lines Parse Parse + check Paired added cost Check only
uv lock, formatted 292,865 / 1,649 1.697 ms 1.855 ms +0.154 ms 0.145 ms
uv lock, diff at end 292,866 / 1,649 1.671 ms 1.824 ms +0.158 ms 0.145 ms
large lock, formatted 15,833,578 / 302,888 238.104 ms 253.807 ms +16.164 ms 12.766 ms
large lock, diff at end 15,833,579 / 302,888 239.593 ms 254.910 ms +14.433 ms 12.719 ms

For comparison, serializing the already-parsed Lock and comparing the resulting TOML adds substantially more work:

Fixture Exact serialize + compare
uv lock, formatted / diff at end 2.384 ms / 2.362 ms
large lock, formatted / diff at end 265.284 ms / 271.681 ms

The streaming check adds about 0.15 ms for the normal lock and about 13-16 ms for the large lock (roughly 6-9% of deserialization), including the worst case where the first formatting error is at the end. Exact reserialization is about 16-21x more expensive than the streaming check here and roughly doubles the large-lock parse cost.

Measurements used optimized Rust (rustc 1.97.0, -C opt-level=3) on arm64 macOS. Parse and parse-plus-check samples were interleaved with alternating order to reduce drift: 120 paired parse / 500 check-only samples for the normal lock and 20 / 160 for the large lock. The checker streams tokens and does not collect the token vector. It checks serialization shape/whitespace, not semantic key ordering; an exact byte-for-byte canonicality check would require the more expensive reserialization path.

@zanieb
zanieb marked this pull request as ready for review July 23, 2026 16:27
@astral-automations-bot

Copy link
Copy Markdown

Promoted to #20646.

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.

3 participants