Skip to content

Stripping utf8-bom before formatting files - #576

Closed
KaranPradhan266 wants to merge 2 commits into
Kotlin:mainfrom
KaranPradhan266:fix/ignore-utf8-bom
Closed

Stripping utf8-bom before formatting files#576
KaranPradhan266 wants to merge 2 commits into
Kotlin:mainfrom
KaranPradhan266:fix/ignore-utf8-bom

Conversation

@KaranPradhan266

Copy link
Copy Markdown
Contributor

Fixes: #565

Strip UTF-8 BOM before formatting

Problem

Some Kotlin source files may begin with a UTF-8 Byte Order Mark (BOM, \uFEFF).
When such a file is passed to the formatter, the BOM is treated as part of the source text rather than metadata. This leads to an error while parsing.

Although BOMs are unnecessary for UTF-8 and discouraged for Kotlin source files, they can still appear due to editor defaults or generated files.

Solution

This change strips a leading UTF-8 BOM (\uFEFF) from file contents before formatting. By removing the BOM at input time:

  • The formatter operates on clean Kotlin source text
  • Behavior for files without a BOM is unchanged
  • Formatting becomes robust against files saved with a UTF-8 BOM

This aligns with Kotlin tooling expectations and prevents BOM-related formatting errors without affecting valid source code.

Notes

  • The BOM is removed only when it appears at the start of the file
  • No formatting changes occur beyond this normalization step

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Dec 13, 2025
@hick209

hick209 commented Dec 17, 2025

Copy link
Copy Markdown
Collaborator

Overall it LGTM.
Can you update the CHANGELOG.md before we merge this?

@KaranPradhan266

Copy link
Copy Markdown
Contributor Author

@hick209 done!, Thanks for reviewing it!

@meta-codesync

meta-codesync Bot commented Dec 17, 2025

Copy link
Copy Markdown

@hick209 has imported this pull request. If you are a Meta employee, you can view this in D89381568.

@meta-codesync

meta-codesync Bot commented Dec 17, 2025

Copy link
Copy Markdown

@hick209 merged this pull request in d7b2226.

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

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Merged

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Ktfmt should ignore the UTF-8 BOM.

3 participants