fix: PublishCommand returns non-zero exit code on error paths#266
Merged
sellakumaran merged 2 commits intomicrosoft:mainfrom Feb 17, 2026
Merged
Conversation
Switch SetHandler from typed parameters to InvocationContext pattern (matching ConfigCommand) to enable context.ExitCode = 1 on error paths. Previously, all ~27 error return paths in PublishCommand silently returned exit code 0 because the typed-parameter SetHandler signature returns Task (not Task<int>), so System.CommandLine always interprets the result as success. Changes: - Use InvocationContext-based SetHandler (same pattern as ConfigCommand) - Add isNormalExit flag tracking (default false) - Mark 4 normal exits: dry-run, skip-graph, missing-tenantId, success - Add finally block setting context.ExitCode = 1 when !isNormalExit - Uses context.ExitCode (graceful) instead of Environment.Exit (abrupt) Validated with real publish error (HTTP 400 'Must upload a newer version'): - BEFORE: exit code 0 (bug), full output - AFTER: exit code 1 (fixed), full output, no truncation Fixes microsoft#264
Contributor
There was a problem hiding this comment.
Pull request overview
This PR fixes a critical bug where the publish command always returned exit code 0, even when errors occurred. The root cause was that the SetHandler used a typed-parameter signature returning Task instead of Task<int>, which System.CommandLine interprets as always successful. This prevented calling scripts and CI/CD pipelines from detecting publish failures.
Changes:
- Refactored SetHandler to use InvocationContext pattern for exit code control
- Added isNormalExit flag to track successful completion paths
- Implemented finally block to set context.ExitCode = 1 for all error paths
…de documentation This commit addresses code review feedback on PR microsoft#266 by adding: 1. **Comprehensive Test Coverage** (BLOCKING issue resolved): - Created PublishCommandTests.cs with 7 tests covering exit code behavior - Tests verify error paths return exit code 1 - Tests verify normal paths (dry-run, skip-graph, success) return exit code 0 - Tests validate exception handling returns exit code 1 - All tests passing (7/7) 2. **Improved Code Documentation** (MEDIUM issue resolved): - Added explanatory comment in finally block about exit code pattern choice - Documented why pattern differs from ConfigCommand approach - Added rationale for defensive programming with isNormalExit flag 3. **Design Decision Documentation** (LOW issue resolved): - Added comment explaining missing tenantId treatment as normal exit - Clarifies that MOS publish success with optional Graph operations is exit code 0 Tests validate the fix for issue microsoft#264 where ~27 error paths incorrectly returned exit code 0, causing automation scripts to misreport failures as success. Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
sellakumaran
approved these changes
Feb 17, 2026
ajmfehr
approved these changes
Feb 17, 2026
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.
Summary
Fixes #264 - PublishCommand now correctly returns exit code 1 on all error paths.
Problem
All ~27 error
eturn;\ paths in \PublishCommand.cs\ silently returned exit code 0 because the \SetHandler\ used a typed-parameter signature (\�sync (bool, bool, string, string?, bool) => { ... }) which returns \Task\ (not \Task). System.CommandLine always interprets this as exit code 0.
This caused calling scripts (e.g., PowerShell automation) to incorrectly report success when publish operations failed.
Fix
Switch to the \InvocationContext\ pattern (matching \ConfigCommand):
Why context.ExitCode over Environment.Exit(1)?
Validation
Tested with real publish error (HTTP 400 'Must upload a newer version'):
Related