Fix generated code compilation errors when any description contains newlines - #193
Conversation
Co-authored-by: Tyrrrz <1935960+Tyrrrz@users.noreply.github.com>
Co-authored-by: Tyrrrz <1935960+Tyrrrz@users.noreply.github.com>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## prime #193 +/- ##
==========================================
- Coverage 90.50% 90.50% -0.01%
==========================================
Files 66 66
Lines 2339 2338 -1
Branches 282 282
==========================================
- Hits 2117 2116 -1
Misses 140 140
Partials 82 82 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
🟡 Changes recommended
The generator’s Encode(char?) still emits unescaped char literals, which can produce invalid generated C# for certain short-name values.
Once you've addressed the issues Copilot identified, you can request another Copilot review.
Pull request overview
This PR fixes a source-generator bug where multiline Description values (on [Command], [CommandOption], and [CommandParameter]) could produce uncompilable generated descriptor code due to raw newline characters in emitted string literals.
Changes:
- Switched generator string literal encoding to Roslyn’s
SymbolDisplay.FormatLiteralto correctly escape newlines and other special characters. - Added a regression test that compiles commands with multiline descriptions and validates the resulting help output contains the expected text.
File summaries
| File | Description |
|---|---|
| CliFx.Generators/Utils/CSharp.cs | Updates string encoding in generated code to use Roslyn literal formatting for correct escaping. |
| CliFx.Tests/HelpSpecs.cs | Adds a regression test covering multiline descriptions on command/option/parameter metadata. |
Review details
- Files reviewed: 2/2 changed files
- Comments generated: 1
- Review effort level: Lite
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Tyrrrz <1935960+Tyrrrz@users.noreply.github.com>
Multiline
Descriptionvalues on[Command],[CommandOption], or[CommandParameter]produced invalid generated code (*.Descriptor.g.cs), failing withCS1010: Newline in constant. The generator's string escaping only handled backslashes and quotes, leaving embedded newlines as literal line breaks inside the emitted string literal.CliFx.Generators/Utils/CSharp.cs: replaced the hand-rolledEscape/Encodelogic withMicrosoft.CodeAnalysis.CSharp.SymbolDisplay.FormatLiteral, which correctly escapes newlines and all other special characters when producing string literals. Removed the now-unusedEscapemethod.CliFx.Tests/HelpSpecs.cs: added a regression test compiling a command with multilineDescription(via raw string literal and\n) on[Command],[CommandOption], and[CommandParameter], asserting the help output renders correctly.Example that now compiles correctly:
Descriptionon[Command]/[CommandOption]/[CommandParameter]produces uncompilable generated code #192