Skip to content

Fix registry resource what-if for non-existing key - #1692

Open
Steve Lee (SteveL-MSFT) wants to merge 5 commits into
PowerShell:mainfrom
SteveL-MSFT:registry-whatif
Open

Fix registry resource what-if for non-existing key#1692
Steve Lee (SteveL-MSFT) wants to merge 5 commits into
PowerShell:mainfrom
SteveL-MSFT:registry-whatif

Conversation

@SteveL-MSFT

@SteveL-MSFT Steve Lee (SteveL-MSFT) commented Aug 25, 2026

Copy link
Copy Markdown
Member

PR Summary

The registry library didn't handle the case where it's what-if and the key didn't exist and _exist: false which simply returned nothing as nothing needed to be done, however, this empty result causes the set operation to fail with an error.

The fix here is if this is what-if and the key doesn't exist, then add metadata recognizing that the key already doesn't exist so nothing would be done.

This required one other change to allow additional properties to DeleteResult which gets thrown away. The reason for this is to simplify resource development so they can return the same struct output for delete as in other cases instead of special casing within the resource.

Due to a known issue with the rust code cov tools, needed to update how the main process exits to return an exit code as the current use of process::exit() somehow causes the coverage data to not be collected.

For this config:

$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json
resources:
- name: remove the value
  type: Microsoft.Windows/Registry
  properties:
    keyPath: HKCU\Software\NotExist
    valueName: V
    _exist: false

now results in this output (instead of an error):

executionInformation:
  duration: PT5.4564664S
  endDatetime: 2026-08-25T14:33:44.902565600-07:00
  executionType: whatIf
  operation: set
  securityContext: restricted
  startDatetime: 2026-08-25T14:33:39.446099200-07:00
  version: 3.4.0-preview.1
metadata:
  Microsoft.DSC:
    duration: PT5.4564299S
    endDatetime: 2026-08-25T14:33:44.902529100-07:00
    executionType: whatIf
    operation: set
    securityContext: restricted
    startDatetime: 2026-08-25T14:33:39.446099200-07:00
    version: 3.4.0-preview.1
results:
- executionInformation:
    duration: PT0.066319S
    whatIf:
    - Key 'HKCU\Software\NotExist' not found, would do nothing
  metadata:
    Microsoft.DSC:
      duration: PT0.066319S
    whatIf:
    - Key 'HKCU\Software\NotExist' not found, would do nothing
  name: remove the value
  type: Microsoft.Windows/Registry
  result:
    beforeState:
      keyPath: HKCU\Software\NotExist
      _exist: false
    afterState:
      keyPath: HKCU\Software\NotExist
      _exist: false
    changedProperties: []
messages: []
hadErrors: false

PR Context

Fix #1691

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 fixes dsc config set --what-if for the Windows Registry resource when the desired state is removal (_exist: false) but the target key does not exist, preventing empty/invalid JSON output that previously caused JSON: EOF while parsing a value failures.

Changes:

  • Add what-if handling for delete operations when the registry key is missing (emit a “would do nothing” what-if message instead of returning no output).
  • Relax DeleteResult deserialization to allow/ignore extra properties returned by resources for delete what-if output.
  • Add a regression test and a localized message string for the non-existing-key delete what-if path.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

File Description
resources/registry/tests/registry.config.whatif.tests.ps1 Adds a config-level what-if regression test for removing a non-existing registry key.
lib/dsc-lib/src/dscresources/invoke_result.rs Removes strict unknown-field rejection for DeleteResult so delete outputs can include extra fields without failing parsing.
lib/dsc-lib-registry/src/lib.rs Implements delete what-if behavior for missing keys by returning metadata instead of None.
lib/dsc-lib-registry/locales/en-us.toml Adds the localized what-if message for “key not found, would do nothing”.

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

Comment thread lib/dsc-lib-registry/src/lib.rs
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
@SteveL-MSFT

Steve Lee (SteveL-MSFT) commented Aug 26, 2026

Copy link
Copy Markdown
Member Author

Code coverage collection failure is due to rust-lang/rust#77553

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
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.

--what-if fails on a Registry resource with _exist: false

2 participants