-
Notifications
You must be signed in to change notification settings - Fork 40
Remove UTF-8 BOM from changelog command outputs #3243
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
lcawl
wants to merge
6
commits into
main
Choose a base branch
from
bom-fix
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
c5712da
Remove UTF-8 BOM from changelog command output
lcawl a3d8b50
dotnet formatted
lcawl de3f48a
Merge branch 'main' into bom-fix
lcawl 5f8c3da
Merge branch 'main' into bom-fix
lcawl 460f419
Address feedback
lcawl e1811ae
Merge branch 'main' into bom-fix
lcawl File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
src/services/Elastic.Changelog/Utilities/ChangelogUtf8Normalization.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| // Licensed to Elasticsearch B.V under one or more agreements. | ||
| // Elasticsearch B.V licenses this file to you under the Apache 2.0 License. | ||
| // See the LICENSE file in the project root for more information | ||
|
|
||
| using System; | ||
|
|
||
| namespace Elastic.Changelog.Utilities; | ||
|
|
||
| /// <summary> | ||
| /// Utilities for normalizing UTF-8 encoding in changelog YAML files. | ||
| /// Ensures YAML output is UTF-8 without BOM for better tooling compatibility and review ergonomics. | ||
| /// </summary> | ||
| public static class ChangelogUtf8Normalization | ||
| { | ||
| /// <summary> | ||
| /// UTF-8 Byte Order Mark character (U+FEFF). | ||
| /// </summary> | ||
| public const char Utf8BomChar = '\uFEFF'; | ||
|
|
||
| /// <summary> | ||
| /// UTF-8 Byte Order Mark as byte sequence (EF BB BF). | ||
| /// </summary> | ||
| public static readonly byte[] Utf8BomBytes = [0xEF, 0xBB, 0xBF]; | ||
|
|
||
| /// <summary> | ||
| /// Strips the leading UTF-8 BOM character from a string if present. | ||
| /// YAML should be UTF-8 without BOM for tooling and review ergonomics. | ||
| /// </summary> | ||
| /// <param name="text">The text to normalize</param> | ||
| /// <returns>Text with leading BOM character removed if it was present</returns> | ||
| public static string StripLeadingUtf8BomChar(string text) | ||
| { | ||
| if (string.IsNullOrEmpty(text)) | ||
| return text; | ||
|
|
||
| return text.StartsWith(Utf8BomChar) ? text.Substring(1) : text; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Checks if a byte span starts with the UTF-8 BOM sequence (EF BB BF). | ||
| /// </summary> | ||
| /// <param name="bytes">The byte span to check</param> | ||
| /// <returns>True if the span starts with UTF-8 BOM bytes</returns> | ||
| public static bool HasUtf8Bom(ReadOnlySpan<byte> bytes) => bytes.Length >= 3 && | ||
| bytes[0] == 0xEF && | ||
| bytes[1] == 0xBB && | ||
| bytes[2] == 0xBF; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe we can simply do
WriteAllTextwithnew UTF8Encoding(false)instead ofEncoding.UTF8.That way we allocate less strings, see also: https://danielwertheim.se/utf-8-bom-adventures-in-c/
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
460f419 is my attempt to address this comment as well as #3243 (review)
The AI summary for that change is as follows:
Summary of Changes in 460f419
This commit addresses PR review feedback by consolidating BOM helper duplication and optimizing UTF-8 write style:
🔄 Consolidated BOM/String Normalization
Utf8TextNormalization.csinElastic.Documentation/Text/with enhanced BOM stripping that removes all consecutive leading U+FEFF characters (handles double-BOM scenarios)ReleaseNotesSerialization.cs: Removed privateStripLeadingUtf8BomCharmethod, now uses sharedUtf8TextNormalization.StripLeadingUtf8BomChangelogUtf8Normalization.cs: Now serves as a thin forwarder to the shared implementation, maintaining backward compatibility⚡ Optimized UTF-8 File Writes
Replaced
WriteAllBytesAsync(path, Encoding.UTF8.GetBytes(content))withWriteAllTextAsync(path, content, Utf8NoBom)in all changelog writers:ChangelogFileWriter.csChangelogBundlingService.csChangelogBundleAmendService.csGitHubReleaseChangelogService.csChangelogPrepareArtifactService.csEach now uses
static readonly UTF8Encoding Utf8NoBom = new(encoderShouldEmitUTF8Identifier: false)to avoid per-write byte array allocations.✅ Enhanced Testing
Utf8TextNormalizationTests.cswith comprehensive shared utility testsChangelogUtf8NormalizationTests.csThe changes eliminate code duplication, improve memory efficiency, and provide more robust BOM handling across the codebase.