Repository navigation
Write aspire.config.json with the OS line endings and a final newline - #21252
Conversation
System.Text.Json indents with Environment.NewLine and writes no final newline, so the same aspire.config.json came out CRLF on Windows and LF on Linux and macOS, and never ended with a newline. Some templates are also embedded and copied byte for byte, so their line endings followed the checkout instead of the product. Normalize at the source: - Add ConfigurationHelper.NormalizeJsonFileContent (CRLF to LF, exactly one final newline) and use it in every writer: AspireConfigFile.Save, aspire init (aspire.config.json and apphost.run.json), the shared settings writers (aspire config set/delete, aspire agent init MCP client configs) and the TypeScript AppHost migration. - Pin the embedded template aspire.config.json and apphost.run.json files to eol=lf in .gitattributes. Add tests for each writer, a guard over the embedded template resources, and update the agent init snapshots for the final newline. Fixes #21249 Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: c8717a87-0ee5-4296-a6fd-ea6e2d91a57e
|
🚀 Dogfood this PR with:
curl -fsSL https://raw.githubusercontent.com/microsoft/aspire/main/eng/scripts/get-aspire-cli-pr.sh | bash -s -- 21252Or
iex "& { $(irm https://raw.githubusercontent.com/microsoft/aspire/main/eng/scripts/get-aspire-cli-pr.ps1) } 21252" |
This comment has been minimized.
This comment has been minimized.
There was a problem hiding this comment.
🟢 Approval recommended
The formatting changes are narrowly scoped, have focused regression coverage, and have no identified blocking issues.
No open issues 🎉
What changed in this PR
Standardizes CLI-written JSON configuration files across operating systems, addressing #21249.
Changes:
- Normalizes serialized configs to LF with exactly one final newline.
- Pins embedded template configs to LF.
- Adds regression coverage and updates MCP configuration snapshots.
| File | Description |
|---|---|
tests/Aspire.Cli.Tests/Utils/LineEndingAssert.cs |
Adds shared newline assertions. |
tests/Aspire.Cli.Tests/Templating/EmbeddedTemplateConfigTests.cs |
Checks embedded config formatting. |
tests/Aspire.Cli.Tests/Snapshots/AgentInitClientSelectionTests.AgentInit_OpenCodeSiblingJson_RepairsInheritedDeprecatedCommand_scopeName=project_partialOverride=True.verified.json |
Adds expected final newline. |
tests/Aspire.Cli.Tests/Snapshots/AgentInitClientSelectionTests.AgentInit_OpenCodeSiblingJson_RepairsInheritedDeprecatedCommand_scopeName=project_partialOverride=False.verified.json |
Adds expected final newline. |
tests/Aspire.Cli.Tests/Snapshots/AgentInitClientSelectionTests.AgentInit_OpenCodeSiblingJson_RepairsInheritedDeprecatedCommand_scopeName=global_partialOverride=True.verified.json |
Adds expected final newline. |
tests/Aspire.Cli.Tests/Snapshots/AgentInitClientSelectionTests.AgentInit_OpenCodeSiblingJson_RepairsInheritedDeprecatedCommand_scopeName=global_partialOverride=False.verified.json |
Adds expected final newline. |
tests/Aspire.Cli.Tests/Snapshots/AgentInitClientSelectionTests.AgentInit_OpenCodeJsonc_PreservesExistingSettingsWhenAddingServer_scopeName=project.verified.json |
Adds expected final newline. |
tests/Aspire.Cli.Tests/Snapshots/AgentInitClientSelectionTests.AgentInit_OpenCodeJsonc_PreservesExistingSettingsWhenAddingServer_scopeName=global.verified.json |
Adds expected final newline. |
tests/Aspire.Cli.Tests/Snapshots/AgentInitClientSelectionTests.AgentInit_OpenCodeConfiguration_UsesExistingFormatAndPrecedence_scopeName=project_jsonExists=True_jsoncExists=True.verified.txt |
Reflects config’s final newline. |
tests/Aspire.Cli.Tests/Snapshots/AgentInitClientSelectionTests.AgentInit_OpenCodeConfiguration_UsesExistingFormatAndPrecedence_scopeName=project_jsonExists=True_jsoncExists=False.verified.txt |
Reflects config’s final newline. |
tests/Aspire.Cli.Tests/Snapshots/AgentInitClientSelectionTests.AgentInit_OpenCodeConfiguration_UsesExistingFormatAndPrecedence_scopeName=project_jsonExists=False_jsoncExists=False.verified.txt |
Reflects config’s final newline. |
tests/Aspire.Cli.Tests/Snapshots/AgentInitClientSelectionTests.AgentInit_OpenCodeConfiguration_UsesExistingFormatAndPrecedence_scopeName=global_jsonExists=True_jsoncExists=True.verified.txt |
Reflects config’s final newline. |
tests/Aspire.Cli.Tests/Snapshots/AgentInitClientSelectionTests.AgentInit_OpenCodeConfiguration_UsesExistingFormatAndPrecedence_scopeName=global_jsonExists=True_jsoncExists=False.verified.txt |
Reflects config’s final newline. |
tests/Aspire.Cli.Tests/Snapshots/AgentInitClientSelectionTests.AgentInit_OpenCodeConfiguration_UsesExistingFormatAndPrecedence_scopeName=global_jsonExists=False_jsoncExists=False.verified.txt |
Reflects config’s final newline. |
tests/Aspire.Cli.Tests/Snapshots/AgentInitClientSelectionTests.AgentInit_DeprecatedMcpCommand_RepairsSelectedConfigurationWithoutReplacingOtherSettings_clientId=opencode_scopeName=project.verified.json |
Adds expected final newline. |
tests/Aspire.Cli.Tests/Snapshots/AgentInitClientSelectionTests.AgentInit_DeprecatedMcpCommand_RepairsSelectedConfigurationWithoutReplacingOtherSettings_clientId=opencode_scopeName=global.verified.json |
Adds expected final newline. |
tests/Aspire.Cli.Tests/Snapshots/AgentInitClientSelectionTests.AgentInit_DeprecatedMcpCommand_RepairsSelectedConfigurationWithoutReplacingOtherSettings_clientId=copilot_scopeName=project.verified.json |
Adds expected final newline. |
tests/Aspire.Cli.Tests/Snapshots/AgentInitClientSelectionTests.AgentInit_DeprecatedMcpCommand_RepairsSelectedConfigurationWithoutReplacingOtherSettings_clientId=copilot_scopeName=global.verified.json |
Adds expected final newline. |
tests/Aspire.Cli.Tests/Snapshots/AgentInitClientSelectionTests.AgentInit_DeprecatedMcpCommand_RepairsSelectedConfigurationWithoutReplacingOtherSettings_clientId=claude_scopeName=project.verified.json |
Adds expected final newline. |
tests/Aspire.Cli.Tests/Snapshots/AgentInitClientSelectionTests.AgentInit_DeprecatedMcpCommand_RepairsSelectedConfigurationWithoutReplacingOtherSettings_clientId=claude_scopeName=global.verified.json |
Adds expected final newline. |
tests/Aspire.Cli.Tests/Snapshots/AgentInitClientSelectionTests.AgentInit_AliasedMcpDestinations_ConfigureEachSelectedServerContainer_pinned=True.verified.json |
Adds expected final newline. |
tests/Aspire.Cli.Tests/Snapshots/AgentInitClientSelectionTests.AgentInit_AliasedMcpDestinations_ConfigureEachSelectedServerContainer_pinned=False.verified.json |
Adds expected final newline. |
tests/Aspire.Cli.Tests/Migrations/TypeScriptAppHostMigrationTests.cs |
Checks migrated config formatting. |
tests/Aspire.Cli.Tests/Configuration/ConfigurationServiceTests.cs |
Tests CRLF normalization during set/delete. |
tests/Aspire.Cli.Tests/Configuration/ConfigurationHelperTests.cs |
Tests normalization and settings writers. |
tests/Aspire.Cli.Tests/Configuration/AspireConfigFileTests.cs |
Checks saved config formatting. |
tests/Aspire.Cli.Tests/Commands/NewCommandTemplateConfigPersistenceTests.cs |
Checks scaffolded config formatting. |
tests/Aspire.Cli.Tests/Commands/InitCommandTests.cs |
Checks new and merged config formatting. |
src/Aspire.Cli/Utils/ConfigurationHelper.cs |
Centralizes normalization for settings writers. |
src/Aspire.Cli/Migrations/TypeScriptAppHostMigration.cs |
Normalizes migrated config output. |
src/Aspire.Cli/Configuration/AspireConfigFile.cs |
Normalizes saved config output. |
src/Aspire.Cli/Commands/InitCommand.cs |
Normalizes both generated JSON configs. |
.gitattributes |
Pins embedded JSON configs to LF. |
Review effort: Balanced
|
Retrying the failed CI jobs for this pull request from the CI run attempt. The rerun is being tracked in the rerun attempt. |
|
Retrying the failed CI jobs for this pull request from the CI run attempt. The rerun is being tracked in the rerun attempt. |
|
Isn't writing CRLF on WIndows correct? And then rely on git normalizing line endings correctly for the OS on checkout. If aspire.config.json uses LF on Windows then people could get inconsistent line endings when they manually edit it. |
Ah, the problem is the JSON file is embedded in the CLI, and copies the file to the local filesystem. What about normalizing the JSON file line endings to the local system's when the embedded file is copied to the local filesystem? |
Address review feedback on #21252: instead of forcing LF, write aspire.config.json and the other JSON config files the CLI produces with the OS line break and exactly one final newline. - ConfigurationHelper.NormalizeJsonFileContent emits Environment.NewLine (CRLF on Windows, LF elsewhere) plus a single final newline. Only CRLF and LF are rewritten, so U+0085, U+2028 and U+2029 inside JSON strings are untouched. - CliTemplateFactory normalizes the embedded aspire.config.json and apphost.run.json as they are copied to disk, so a CLI built from a checkout with different line endings still writes the OS line break. - Drop the .gitattributes eol=lf pins and the embedded-resource guard test; the copy-time normalization replaces them and avoids the all-tests CI trigger. - Tests assert the OS line break and feed the opposite style, so they discriminate on every OS. A copy-path test covers every embedded config template. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: 38b1d961-84a4-42b6-be1a-e5479116ecbd
This comment has been minimized.
This comment has been minimized.
System.Text.Json already indents with Environment.NewLine, so text it serializes needs no line break normalization and only lacks the trailing newline. AspireConfigFile.Save, both writes in aspire init, the settings file writers and the TypeScript AppHost migration now append the final newline through ConfigurationHelper.AppendFinalNewline instead of normalizing line breaks. Normalization stays only where an embedded template is copied to disk, because that text carries the line breaks of the checkout that built the CLI and some templates have no final newline. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: 38b1d961-84a4-42b6-be1a-e5479116ecbd
This comment has been minimized.
This comment has been minimized.
|
Retrying the failed CI jobs for this pull request from the CI run attempt. The rerun is being tracked in the rerun attempt. |
|
Retrying the failed CI jobs for this pull request from the CI run attempt. The rerun is being tracked in the rerun attempt. |
Only append the OS newline when the text does not already end with a line break (LF or CRLF), so the helper never writes a second final newline. This follows the review suggestion to check for an existing final newline instead of always appending one. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: 38b1d961-84a4-42b6-be1a-e5479116ecbd
This comment has been minimized.
This comment has been minimized.
The only conflict was in tests/Aspire.Cli.Tests/Configuration/ConfigurationHelperTests.cs, where this branch and upstream both added tests at the same spot. Both sets of tests are kept. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: 38b1d961-84a4-42b6-be1a-e5479116ecbd
main added ConfigurationHelper.WriteSettingsFileAtomicallyAsync in #20805. aspire uses it to apply a Git configuration source to the project aspire.config.json, and it serialized straight to the stream, so the file ended without the newline that the other settings writers add. Serialize to text, run it through EnsureFinalNewline, and write the UTF-8 bytes without a byte order mark. System.Text.Json already indents with the OS line break. Add a test for a new file and for replacing an existing file that uses the other line break style and has no final newline. Four ConfigSourceServiceTests snapshots and one ConfigurationHelperTests snapshot gain the final newline. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: 38b1d961-84a4-42b6-be1a-e5479116ecbd
This comment has been minimized.
This comment has been minimized.
|
Retrying the failed CI jobs for this pull request from the CI run attempt. The rerun is being tracked in the rerun attempt. |
|
Retrying the failed CI jobs for this pull request from the CI run attempt. The rerun is being tracked in the rerun attempt. |
Tests selector2 / 100 PR test projects · 4 PR jobs, from 38 changed files. Selected PR test projects (2 / 100)
Selected PR jobs (4)
How these were chosen — grouped by what changed📦 affected project 🧪 🧪 🧪 🧪 🧪 🧪 🧪 🧪 🧪 🧪 🧪 🧪 🧪 🧪 🧪 🧪 🧪 🧪 🧪 🧪 🧪 🧪 🧪 🧪 🧪 🧪 🧪 🧪 🧪 🧪 🧪 🧪 🧪 Job reasons
Selection computed for commit |
|
Retrying the failed CI jobs for this pull request from the CI run attempt. The rerun is being tracked in the rerun attempt. |
|
✅ No documentation update needed. Skipped: proven false positive.
|
Description
The CLI wrote
aspire.config.jsonwith whatever line break and final newline the command, the OS and the template happened to produce.System.Text.Jsonindents withEnvironment.NewLineand writes no trailing newline, and several templates are embedded and copied as-is, so they carried the line breaks of the checkout that built the CLI. The same scaffold therefore differed betweenaspire newandaspire init, and between CLI builds.This writes the JSON config files the CLI manages (
aspire.config.json,apphost.run.json, and the settings and MCP client config files it updates) with the OS line break (Environment.NewLine: CRLF on Windows, LF on Linux and macOS) and exactly one final newline (UTF-8, no BOM). Serialized configs already get the OS line break fromSystem.Text.Json, so the CLI only appends the final newline it never writes. The embeddedaspire.config.jsonandapphost.run.jsontemplates are normalized as they are copied to disk, so they match what the CLI serializes.An earlier revision forced LF on every OS and pinned the embedded templates with
.gitattributes. Following James Newton-King (@JamesNK)'s review, files now use the OS line break instead: it matches what an editor produces on that OS, lets git normalize the files like any other text file, and avoids inconsistent line breaks when people edit the file by hand. Also following his review, line breaks are normalized only where an embedded template is copied, because text the serializer produces already uses the OS line break.Fixes #21249
What changed
ConfigurationHelper.EnsureFinalNewlineadds the OS newline thatSystem.Text.Jsonnever writes, unless the text already ends with a line break (LF or CRLF), so it never produces a second final newline. Every JSON config writer uses it on the serializer output, without normalizing line breaks:AspireConfigFile.Save, both writes inaspire init(aspire.config.jsonandapphost.run.json),ConfigurationHelper.WriteSettingsFileandWriteSettingsFileAsync(aspire config setanddelete, the settings key-normalization rewrite, and the MCP client configs written byaspire agent init),WriteSettingsFileAtomicallyAsync(below), and the TypeScript AppHost migration.ConfigurationHelper.WriteSettingsFileAtomicallyAsync, added tomainby Implement project-bound Git configuration sources for Aspire CLI #20805, is what a Git configuration source uses to apply its settings to the project'saspire.config.json, so it needed the same contract. It now serializes to text, passes it throughEnsureFinalNewlineand writes it as UTF-8 without a BOM. It also writes the source cache'scheck.json, which gains the final newline as a side effect. The cache's other files (the compact snapshotaspire.config.json,metadata.jsonand thecurrentpointer) are internal to the CLI and unchanged.ConfigurationHelper.NormalizeJsonFileContentrewrites CRLF and LF to the OS line break and ends the text with exactly one newline. It is used only for the embedded templates below, whose text carries the line breaks of the checkout that built the CLI. It rewrites only CRLF and LF instead of callingReplaceLineEndings, which would also rewrite U+0085, U+2028 and U+2029, all legal raw characters inside a JSON string. The overload that takes the line break lets tests cover both styles on any OS.CliTemplateFactory.CopyTemplateTreeToDiskAsyncapplies it to the embeddedaspire.config.jsonandapphost.run.jsononly. Other template files are copied unchanged, which matters formvnwandgradlewthat must stay LF. The method is nowinternalso a test can call it..gitattributeschange: theeol=lfpins and the embedded-resource guard test from the first revision are gone.WriteSettingsFileAtomically_WritesOsLineBreaksAndSingleFinalNewline, which writes a new file and replaces an existing one that has the other line break and no final newline), a copy-path test for the embedded config files of every template that has one (CliTemplateFactoryConfigLineEndingTests), tests thatEnsureFinalNewlineadds one OS newline to serializer output and keeps an existing final LF or CRLF, and a sharedLineEndingAsserthelper. The copy-path andNormalizeJsonFileContenttests feed the line break that is not the OS one, and theEnsureFinalNewlinetheory feeds both LF and CRLF, so they fail on every OS if the conversion or the guard is missing.Reviewer notes
core.autocrlf=true,text=auto).aspire agent init, so those files now end with a newline. That is 20AgentInitClientSelectionTestssnapshots with one added newline each: the 14.verified.jsonsnapshots lose their\ No newline at end of filemarker, and in the 6.verified.txtsnapshots, which print each file's text followed by a line break, it shows as a blank line before the closing}. They are LF and Verify normalizes line endings, so they match on Windows too. Narrowing the change toaspire.config.jsononly is small if you prefer it.mainadded, which compare what the writers produce: fourConfigSourceServiceTestssnapshots of the project'saspire.config.jsonafter a Git configuration source is materialized or updated (written by the atomic writer), and oneConfigurationHelperTestssnapshot of a settings file after key normalization (written byWriteSettingsFile). Each differs only by that final newline, and Verify normalizes line endings, so they match on Windows too.main. The two upstream changes that overlap this PR are Implement project-bound Git configuration sources for Aspire CLI #20805 (project-bound Git configuration sources, which addedWriteSettingsFileAtomicallyAsync) and Add directory and Git template engine support to aspire new #21263 (directory and Git template engine support inaspire new). The merge had one conflict, inConfigurationHelperTests.cs, resolved by keeping the tests from both sides. The merge commit (f1ce547379) holds only that resolution, and the atomic writer change is the separate commit after it (7fd42afa5e).apphost.ts,package.jsonand so on) andapphost.cskeep the line breaks of the build checkout.ScaffoldingService, which writes the scaffold files of a TypeScript or other guest AppHost (such aspackage.json) as the language support produces them, is not touched.Validation
Windows,
core.autocrlf=true, on the PR head7fd42afa5e(which includesmainatf990a9017d), withDOTNET_ROOTset to the repo's.dotnetand first onPATH.dotnet test --project tests/Aspire.Cli.Tests/Aspire.Cli.Tests.csproj --no-launch-profile -p:SkipNativeBuild=true -- --filter-not-trait "quarantined=true" --filter-not-trait "outerloop=true"-> 8576 tests: 8485 passed, 1 failed, 90 skipped (9m 11s). The failure is not a line-ending assertion:TemporaryWorkspaceTests.RunGitAsync_Timeout_StopsProcessAndReportsPartialOutput: its git alias runssleep 60and the bash bundled with git on this machine has nosleep(exit code 127), so it fails the same way when run alone.dotnet testform and--filter-class. The first is on7fd42afa5e; the other three are onb17f81c16c, beforemainwas merged, so their counts do not include the testsmainadded:WriteSettingsFileAtomicallyAsyncreverted, both cases ofWriteSettingsFileAtomically_WritesOsLineBreaksAndSingleFinalNewline(new file and existing file) fail. They pass with it restored.CliTemplateFactoryConfigLineEndingTestsfail. They pass with it restored.EnsureFinalNewlinereturning its input unchanged, 24 tests fail: 4 of the 34ConfigurationHelperTests(EnsureFinalNewline_AddsOneOsNewLineToSerializedJson,TryNormalizeSettingsFile_RewritesNonOsLineBreaksWithOsLineBreaks, and bothWriteSettingsFiletests) and 20AgentInitClientSelectionTestssnapshot cases, including the OpenCode global / existingopencode.jsonone. They pass with it restored.EndsWith('\n')guard removed (it always appends), exactly the 2EnsureFinalNewline_KeepsExistingFinalNewlinecases fail (LF and CRLF) and the other 32 of the 34ConfigurationHelperTestspass.aspire.dllbuilt from this branch at7fd42afa5e), checked byte by byte for line breaks, final newline and BOM. Theaspire initandaspire newruns pass--use-config-source false, the option frommainthat declines the Git configuration source those commands propose:aspire config set channel stagingon an existing config with no final newline -> CRLF only, one final newline, no BOM.aspire agent init --agents opencode --scope project --skills none --mcp --non-interactive, in a fresh workspace (which now writesopencode.jsonc) and in one with an existingopencode.json(updated in place) -> CRLF only, exactly one final newline, no BOM.aspire init --language csharp --file-based(aspire.config.jsonandapphost.run.json) andaspire init --language typescript(aspire.config.json) -> CRLF only, one final newline, no BOM.aspire new aspire-empty --version 13.5.0 --language csharp, which has no--channel, so the copied files are not re-saved by the serializer. The embedded templates in this checkout are LF (aspire.config.jsonhas 5 LF,apphost.run.jsonhas 30), and the generatedaspire.config.jsonandapphost.run.jsoncame out CRLF only (5 and 30 CRLF), one final newline, no BOM.Environment.NewLine, so CI covers them); the otheraspire newtemplates end to end, because a dev-build CLI cannot resolve a template version without a package feed (the copy-path test covers their embedded config files); the end-to-end test project.Checklist
<remarks />and<code />elements on your triple slash comments?