Skip to content

Preserve encoding and trailing newlines in dotnet-tools.json#53046

Open
Thomas-Shephard wants to merge 10 commits intodotnet:mainfrom
Thomas-Shephard:fix/tool-manifest-format-preservation
Open

Preserve encoding and trailing newlines in dotnet-tools.json#53046
Thomas-Shephard wants to merge 10 commits intodotnet:mainfrom
Thomas-Shephard:fix/tool-manifest-format-preservation

Conversation

@Thomas-Shephard
Copy link
Copy Markdown

@Thomas-Shephard Thomas-Shephard commented Feb 15, 2026

dotnet tool commands (install, update, uninstall) perform an invasive rewrite of the dotnet-tools.json manifest that disregards original file formatting and encoding.

Changes

  • Updated ToolManifestEditor to be format-aware by capturing encoding and newline status during the read phase.
  • Replaced JsonDocument.Parse(stream) with StreamReader and JsonDocument.Parse(text) to support BOM detection and UTF-16.
  • Added a Write helper to restore formatting during manifest updates.
  • Added verification tests in ToolManifestEditorTests.

Fixes #53045

Copilot AI review requested due to automatic review settings February 15, 2026 15:33
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

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 ToolManifestEditor to capture original encoding + trailing-newline state on read and reuse it on write.
  • Switch JSON parsing from JsonDocument.Parse(stream) to BOM-aware StreamReader + 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

  • DeserializeLocalToolsManifest has mismatched/mis-indented braces around the using (JsonDocument doc = JsonDocument.Parse(text)) block (and the subsequent if statements). As written, the using block is never closed and several if blocks are not correctly scoped/indented, which should cause a compilation error and/or incorrect parsing flow. Please fix the brace structure so all root.TryGet* parsing happens inside the JsonDocument using block, and ensure both the JsonDocument and StreamReader scopes 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))

@Thomas-Shephard Thomas-Shephard force-pushed the fix/tool-manifest-format-preservation branch from 00f5cbf to e93b692 Compare February 15, 2026 15:37
@Thomas-Shephard Thomas-Shephard force-pushed the fix/tool-manifest-format-preservation branch from e93b692 to f1aed1d Compare February 15, 2026 15:38
@Thomas-Shephard
Copy link
Copy Markdown
Author

@martincostello is there anything else I need to get this actually merged?

@martincostello
Copy link
Copy Markdown
Member

Wait for someone on the team to review it and merge it - there's nothing I can do.

@Thomas-Shephard
Copy link
Copy Markdown
Author

Ah, must have got confused - thought you were part of the dotnet org.

@dotnet-cli

@martincostello
Copy link
Copy Markdown
Member

I am, but I'm not a member of the .NET team or an MS employee.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

dotnet tool manifest updates strip trailing newlines and BOM

3 participants