Skip to content

Update dsc.exe to have single process exit point - #1693

Merged
Steve Lee (SteveL-MSFT) merged 4 commits into
PowerShell:mainfrom
SteveL-MSFT:dsc-main-exit
Aug 27, 2026
Merged

Update dsc.exe to have single process exit point#1693
Steve Lee (SteveL-MSFT) merged 4 commits into
PowerShell:mainfrom
SteveL-MSFT:dsc-main-exit

Conversation

@SteveL-MSFT

Copy link
Copy Markdown
Member

PR Summary

There's a known issue using std::process::exit() on Windows that prevents code coverage data from being collected. Fix is to switch to having main() return ExitCode which accomplishes the same thing. The previous code in dsc.exe had many calls to exit() which is not ideal since there are now multiple ways for the process to exit. Changed many functions in the exe to return a Result<> instead which wraps the exit code it would have returned so now there's a single place where that gets returned.

ExitCode::from() takes a u8 so changed the consts to u8 instead of i32.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

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 refactors the dsc CLI to avoid calling std::process::exit() throughout the codebase, instead propagating std::process::ExitCode via Result so the process exits through a single return path (improving Windows code coverage collection).

Changes:

  • Replaces many exit(<code>) calls with Result<_, ExitCode> propagation across CLI helper functions and subcommand handlers.
  • Updates CLI exit code constants to u8 to align with ExitCode::from(u8).
  • Adjusts output and input utilities (write_object, get_input, config root initialization) to return Result rather than terminating the process.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.

File Description
dsc/src/util.rs Converts exit-code constants to u8 and updates utility helpers to return Result<_, ExitCode> instead of exiting.
dsc/src/subcommand.rs Updates subcommand entrypoints/helpers to return Result<(), ExitCode> and propagate failures upward.
dsc/src/resource_command.rs Changes resource command handlers to return Result<(), ExitCode> and replace exit() with propagated ExitCode.
dsc/src/main.rs Makes main() return ExitCode and routes subcommands through dsc_main() -> Result<(), ExitCode>.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread dsc/src/main.rs Outdated
Comment thread dsc/src/subcommand.rs Outdated

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Everything here looks good to me, I do think that switching to handling exit at the top level will be more maintainable in the long run.

Looking at the updated logic for the functions, especially the emitting of errors in-place and selecting an exit code to return, made me think that in a future PR (I will file an issue if we agree this is coherent) we could:

  1. Define a mapping for DscError to ExitCode by implementing TryFrom<DscError> for ExitCode to minimize needing to do so at all these callsites.
  2. Return the DscError type instead of an ExitCode
  3. Emit the final terminating error in the root handler before exiting with the required code.

This would also make it easier from a maintainer perspective to work with terminating errors and will eventually be very useful when we want to emit miette diagnostics for more verbose and useful errors (much easier to define handling/emitting in one place for the terminating error).

@SteveL-MSFT
Steve Lee (SteveL-MSFT) added this pull request to the merge queue Aug 27, 2026
Merged via the queue into PowerShell:main with commit 12be466 Aug 27, 2026
37 of 39 checks passed
@SteveL-MSFT
Steve Lee (SteveL-MSFT) deleted the dsc-main-exit branch August 27, 2026 00:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants