Preserve encoding and trailing newlines in dotnet-tools.json#53046
Open
Thomas-Shephard wants to merge 10 commits intodotnet:mainfrom
Open
Preserve encoding and trailing newlines in dotnet-tools.json#53046Thomas-Shephard wants to merge 10 commits intodotnet:mainfrom
Thomas-Shephard wants to merge 10 commits intodotnet:mainfrom
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR aims to make dotnet tool manifest edits non-invasive by preserving the original dotnet-tools.json file encoding (including BOM) and whether it ends with a trailing newline, while also fixing failures when reading UTF-16 manifests.
Changes:
- Update
ToolManifestEditorto capture original encoding + trailing-newline state on read and reuse it on write. - Switch JSON parsing from
JsonDocument.Parse(stream)to BOM-awareStreamReader+JsonDocument.Parse(text)to support UTF-16. - Add tests asserting BOM + trailing newline preservation for UTF-8 BOM and UTF-16 LE manifests.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
src/Cli/dotnet/ToolManifest/ToolManifestEditor.cs |
Captures encoding/newline metadata during read and uses it when writing updated manifests. |
test/dotnet.Tests/ToolManifestTests/ToolManifestEditorTests.cs |
Adds verification coverage for BOM + trailing newline preservation during manifest updates. |
Comments suppressed due to low confidence (1)
src/Cli/dotnet/ToolManifest/ToolManifestEditor.cs:166
DeserializeLocalToolsManifesthas mismatched/mis-indented braces around theusing (JsonDocument doc = JsonDocument.Parse(text))block (and the subsequentifstatements). As written, theusingblock is never closed and severalifblocks are not correctly scoped/indented, which should cause a compilation error and/or incorrect parsing flow. Please fix the brace structure so allroot.TryGet*parsing happens inside theJsonDocumentusing block, and ensure both theJsonDocumentandStreamReaderscopes are properly closed.
using (JsonDocument doc = JsonDocument.Parse(text))
{
JsonElement root = doc.RootElement;
if (root.TryGetInt32Value(JsonPropertyVersion, out var version))
{
serializableLocalToolsManifest.Version = version;
}
if (root.TryGetBooleanValue(JsonPropertyIsRoot, out var isRoot))
00f5cbf to
e93b692
Compare
e93b692 to
f1aed1d
Compare
martincostello
approved these changes
Mar 21, 2026
Author
|
@martincostello is there anything else I need to get this actually merged? |
Member
|
Wait for someone on the team to review it and merge it - there's nothing I can do. |
Author
|
Ah, must have got confused - thought you were part of the dotnet org. @dotnet-cli |
Member
|
I am, but I'm not a member of the .NET team or an MS employee. |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
dotnet toolcommands (install,update,uninstall) perform an invasive rewrite of thedotnet-tools.jsonmanifest that disregards original file formatting and encoding.Changes
ToolManifestEditorto be format-aware by capturing encoding and newline status during the read phase.JsonDocument.Parse(stream)withStreamReaderandJsonDocument.Parse(text)to support BOM detection and UTF-16.Writehelper to restore formatting during manifest updates.ToolManifestEditorTests.Fixes #53045