Skip to content

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

Open
dyegoaurelio wants to merge 1 commit into
masterfrom
disable-directive
Open

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

Conversation

@dyegoaurelio
Copy link
Copy Markdown
Contributor

@dyegoaurelio dyegoaurelio commented May 9, 2026

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 restriction to own-line-only simplifies matching between original and formatted text.

Implemented as a two-step process: the pretty printer preserves directives in their correct positions, then a post-processor finds matching directive pairs in both the original and formatted output and splices the original code regions back in.


I tried to disable this feature on strict mode.
Not applying the post processor using the cli option was fine, however it was very tricky to make sure the special directive syntax (/*nixfmt:disable*/) gets formatted into regular comments.

I thought it looked weird to allow the directive to stay but format stuff bellow it, so I kept it on both modes.

closes #91

@github-actions
Copy link
Copy Markdown

github-actions Bot commented May 9, 2026

Nixpkgs diff

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
restriction to own-line-only simplifies matching between original and
formatted text.

Implemented as a two-step process: the pretty printer preserves
directives in their correct positions, then a post-processor finds
matching directive pairs in both the original and formatted output and
splices the original code regions back in.
@Mic92
Copy link
Copy Markdown
Member

Mic92 commented May 11, 2026

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
go (Just start) ((i, False) : rest) = (start, Just i) : go Nothing rest
go (Just start) ((_, True) : rest) = go (Just start) rest -- nested disable, ignore

-- | Advance the scan state by processing a line character by character.
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Pair reviewed this during the formatting team meeting. We (@MattSturgeon and I) are not in love with re-implementing parsing logic here, given that we already have a CST parser in the codebase.

Have you investigated alternate implementations? For example, tracking the original position of these tokens and then tweaking the pretty printer to honor them?

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

Mic92 commented May 23, 2026

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

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

4 participants