Skip to content

feat: add /*nixfmt:disable*/ and /*nixfmt:enable*/ comment directives#388

Open
dyegoaurelio wants to merge 2 commits into
masterfrom
disable-directive
Open

feat: add /*nixfmt:disable*/ and /*nixfmt:enable*/ comment directives#388
dyegoaurelio wants to merge 2 commits into
masterfrom
disable-directive

Conversation

@dyegoaurelio

@dyegoaurelio dyegoaurelio commented May 9, 2026

Copy link
Copy Markdown
Contributor

Allow users to disable formatting for specific regions by wrapping them
in /nixfmt:disable/ and /nixfmt:enable/ block comments. Directives
must be on their own line. Unclosed disable extends to end of file.

The block comment syntax /nixfmt:disable/ was chosen because it cannot
appear naturally in formatted output, making detection unambiguous.

The directives are resolved during parsing: the parser state tracks the
currently open region and slices its raw text from the input when it
closes. The pretty printer emits a zero-width marker at the region
start and the raw text at its end; the renderer lays out the region
content as usual, keeping its cursor and indentation state exact, but
suppresses the output between the two and prints the raw text verbatim.

closes #91

@github-actions

github-actions Bot commented May 9, 2026

Copy link
Copy Markdown

Nixpkgs diff

@Mic92

Mic92 commented May 11, 2026

Copy link
Copy Markdown
Member

I implemented this feature. Fuzzer couldn't find any issues. I will wait for a release of nixfmt-rs until the pr has been accepted in this repo.
I also added some documentation about it, might be worth to have the same here.

Comment thread src/Nixfmt/Postprocess.hs Outdated
@nixos-discourse

Copy link
Copy Markdown

This pull request has been mentioned on NixOS Discourse. There might be relevant details there:

https://discourse.nixos.org/t/formatting-team-meeting-2026-05-12/77586/1

@Mic92

Mic92 commented May 23, 2026

Copy link
Copy Markdown
Member

I reworked my code to now emit comment directives and remove the post processing code. I also noticed some idempotency issue with directives inside ${}. I don't know if we want to support this at all. It came up during fuzzing: Mic92/nixfmt-rs#89

@nixos-discourse

Copy link
Copy Markdown

This pull request has been mentioned on NixOS Discourse. There might be relevant details there:

https://discourse.nixos.org/t/formatting-team-meeting-2026-05-26/77856/1

@gabyx

gabyx commented Jun 6, 2026

Copy link
Copy Markdown
Member

This is a really needed feature, the only thing we need to make a wider adoption for non-nixpkgs folks, where certain aligned stuff should stay as it is, due to verbosity otherwise.

❤️

@Mic92

Mic92 commented Jun 7, 2026

Copy link
Copy Markdown
Member

This now part of nixfmt-rs 0.5, but might have subtle changes based on how nixfmt decides to implement this feature: https://github.com/Mic92/nixfmt-rs/releases/tag/0.5.0

@nixos-discourse

Copy link
Copy Markdown

This pull request has been mentioned on NixOS Discourse. There might be relevant details there:

https://discourse.nixos.org/t/formatting-team-meeting-2026-06-23/78492/1

@dyegoaurelio dyegoaurelio force-pushed the disable-directive branch 2 times, most recently from 10d239c to 7fc5a1e Compare July 1, 2026 20:43
Capturing the formatter output with command substitution strips
trailing newlines and echo re-adds exactly one, so the expected-output
files could not represent output that does not end with a newline.
Write and compare through a temporary file instead.

@MattSturgeon MattSturgeon left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I approve of the new AST+state-based approach. I approve of the new test coverage, which seems very thorough!

I haven't reviewed the implementation in detail yet, but the parts I've looked at seem like a good direction for the wider architecture.

Let's discuss (and hopefully merge) during Tuesday's meeting!

Allow users to disable formatting for specific regions by wrapping them
in /*nixfmt:disable*/ and /*nixfmt:enable*/ block comments. Directives
must be on their own line. Unclosed disable extends to end of file.

The block comment syntax /*nixfmt:disable*/ was chosen because it cannot
appear naturally in formatted output, making detection unambiguous.

The directives are resolved during parsing: the parser state tracks the
currently open region and slices its raw text from the input when it
closes. The pretty printer emits a zero-width marker at the region
start and the raw text at its end; the renderer lays out the region
content as usual, keeping its cursor and indentation state exact, but
suppresses the output between the two and prints the raw text verbatim.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

Comment directive to disable nixfmt

6 participants