Route dotnet-ef diagnostic output to stderr, fix premature help exit code#37744
Route dotnet-ef diagnostic output to stderr, fix premature help exit code#37744
Conversation
…x ShouldHelp for exit codes Co-authored-by: roji <1862641+roji@users.noreply.github.com>
Co-authored-by: roji <1862641+roji@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
This PR adjusts dotnet ef CLI output to better follow stdout/stderr conventions so that command output can be safely piped/redirected, and it fixes a help-triggering edge case that previously caused “help + exit 0” when additional arguments were present.
Changes:
- Route diagnostic output (error/warn/info/verbose) to stderr, while keeping data output on stdout (with a stdout-only compatibility path when
--prefix-outputis enabled). - Add
AnsiConsole.Errorto support colored output to stderr (mirroring the existingAnsiConsole.Out). - Update
ShouldHelplogic in thedotnet-efwrapper to consider full remaining args, preventing premature help display for inputs likedotnet ef database -c CtxA update.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| src/ef/Reporter.cs | Redirects diagnostics to stderr via a new WriteStdErr helper, preserving stdout-only behavior under --prefix-output. |
| src/ef/AnsiConsole.cs | Adds AnsiConsole.Error backed by Console.Error for colored stderr output. |
| src/dotnet-ef/RootCommand.cs | Fixes ShouldHelp to use the full remaining argument list so help only triggers for true “top-level only” invocations. |
AndriySvyryd
left a comment
There was a problem hiding this comment.
@roji How did you test this?
My idea was to manually test it after the next preview gets released, but if you want to test now and have a way of doing it, let me know. |
It's pretty easy to install the local build, something like this: |
|
Thanks @AndriySvyryd, I managed to manually test thanks to your instructions. The fix was good - reporter messages such as "Build starting..." and "Build succeeded." indeed started appearing on stderr instead of stdout. However, EF logging messages still appears on stdout, which is not the right behavior; for example, when executing I pushed an additional commit which redirects Console.Out to Err entirely, so that logging gets written there; Console.Out is still captured and used by the Reporter so that e.g. the SQL for the migration does end up in stdout and not err. |
|
Did you verify the error code when help is shown due to an invalid argument? |
|
Yes, I did.
|
dotnet efwrites diagnostic messages ("Build started...", "Build succeeded.") to stdout, breaking piping and redirection (e.g.,dotnet ef migrations list | ...ordotnet ef migrations script > migrate.sql). Additionally, misplaced options likedotnet ef database -c CtxA updatesilently show help and exit 0.Reporter: stderr for diagnostics, stdout for data
WriteError,WriteWarning,WriteInformation,WriteVerbose→ stderrWriteData→ stdout (unchanged)--prefix-outputis active (PMC/MSBuild), all output stays on stdout for backward compatibilityAnsiConsole: add stderr writer
AnsiConsole.Errorbacked byConsole.Errorfor colored stderr output, mirroring existingAnsiConsole.OutShouldHelp: check full args, not just command prefix
ShouldHelppreviously usedcommands(args up to first-flag) to decide whether to show help —dotnet ef database -c CtxA updateyieldedcommands = ["database"], triggering help with exit 0args.Count(all remaining args) so help only triggers when the user genuinely typed just a top-level command with nothing elseOriginal prompt
💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.