Skip to content

GH3829: Defer Windows registry key creation until used. - #5011

Merged
devlead merged 1 commit into
cake-build:developfrom
Samyra312007:feature/gh-3829
Sep 22, 2026
Merged

devlead merged 1 commit into
cake-build:developfrom
Samyra312007:feature/gh-3829

Conversation

@Samyra312007

Copy link
Copy Markdown
Contributor

Fixes #3829

Pre-submit checklist

  • Submitted from a feature branch (feature/gh-3829), not master
  • No reformatting — only the 3 files specific to this fix are touched
  • New code has unit test coverage
  • All tests passing; compiles without errors or warnings

What are you seeing today? / What does this fix?

Serializing the build context on Linux/macOS fails with:

Error: Error getting value from 'CurrentUser' on 'Cake.Core.IO.WindowsRegistry'.

Repro (from the issue, Cake Frosting 2.1.0.0 on Ubuntu):

context.Information(context.ToJson(Formatting.Indented));

**Root cause:**

 WindowsRegistry  eagerly materialized the underlying  Microsoft.Win32.Registry 
base keys in its property getters:

// csharp
public IRegistryKey CurrentUser => new WindowsRegistryKey(Microsoft.Win32.Registry.CurrentUser);

On non-Windows platforms, dereferencing  Microsoft.Win32.Registry.*  throws
 PlatformNotSupportedException  (or returns  null  on newer .NET versions), so
any reflective consumer of  ICakeContext e.g. a JSON serializer walking the
context throws on the first registry property, even when the script never
touches the registry.

**What this PR changes:**

Defers creation of the underlying  RegistryKey  until an actual registry
operation is performed:
-  src/Cake.Core/IO/WindowsRegistry.cs  each  IRegistry  property now
returns a key wrapping a lazy  Func<RegistryKey>  factory instead of
dereferencing the hive immediately.
-  src/Cake.Core/IO/WindowsRegistryKey.cs  accepts a  Func<RegistryKey> 
factory; the key is created exactly once (double-checked locking) on first
 GetSubKeyNames()  /  OpenKey()  /  GetValue() .  Dispose()  never forces
creation. A  null  hive (newer .NET on unsupported platforms) is normalized
to  PlatformNotSupportedException  so real registry use fails predictably.
-  src/Cake.Core.Tests/Unit/IO/WindowsRegistryTests.cs, new tests.

**Why this approach:**

- No public API changes IRegistry  /  IRegistryKey ,  CoreModule  DI
registration, and existing consumers ( SignToolResolver ,  InnoSetupRunner )
are untouched.
- Behavior preserved actual registry usage on Linux/macOS still throws
 PlatformNotSupportedException , just at the point of use rather than at
property access.
- Reported scenario now works serializing the context on Ubuntu produces
the same shape as Windows:  "Registry": { "CurrentUser": {}, ... } .

**Test evidence:**

New tests (17) in  src/Cake.Core.Tests/Unit/IO/WindowsRegistryTests.cs :

┌──────────────┬─────┬─────────────────────────────────────────────────────────────────────────────────────────┐
│ Test group   │ Cou │ Verifies                                                                                │
│              │ nt  │                                                                                         │
├──────────────┼─────┼─────────────────────────────────────────────────────────────────────────────────────────┤
│ Each of the  │ 12  │ Property access does not throw on any platform; returns a non-null key                  │
│ 6 IRegistry  │     │                                                                                         │
│ properties   │     │                                                                                         │
│ DeferredKeyC │ 5   │ Factory not invoked on Dispose(); invoked exactly once across repeated use; factory     │
│ reation      │     │ exceptions propagate; PlatformNotSupportedException on actual use on non-Windows        │
│              │     │ (NonWindowsFact); real sub-key enumeration on Windows (WindowsFact)                     │
└──────────────┴─────┴─────────────────────────────────────────────────────────────────────────────────────────┘

Full suite -  Cake.Core.Tests , net10.0, Linux:

Total: 1850
Passed: 1774 
Skipped (platform-restricted): 76
Failed: 0      

Build: 0 warnings, 0 errors.

@devlead devlead left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for taking this on — the approach is the right fix for #3829.

WindowsRegistry currently materializes Microsoft.Win32.Registry.* in the hive getters, so any reflective walk of ICakeContext (including context.ToJson(...) on Linux/macOS) throws PlatformNotSupportedException wrapped as Error getting value from 'CurrentUser' on 'Cake.Core.IO.WindowsRegistry', even when the script never uses the registry.

Deferring native RegistryKey creation until GetSubKeyNames / OpenKey / GetValue keeps IRegistry / IRegistryKey and the CoreModule registration unchanged, leaves SignTool/InnoSetup on the same OpenKey path, and still fails at the point of actual registry use on non-Windows. Serializer output of empty hive objects ("CurrentUser": {}) matches the Windows shape from the issue because IRegistryKey only has methods, not properties.

A few things to address before merge:

  1. Formatting in WindowsRegistryKey — the class is flush-left and the standard third license line was dropped. Cake builds with EnforceCodeStyleInBuild and Release TreatWarningsAsErrors, so this is likely to fail CI as IDE0055.
  2. Regression test for the reported scenario — the new tests cover property access and deferred use, but none walk all IRegistry properties the way a JSON serializer does. A single reflection test over typeof(IRegistry).GetProperties() (Cake.Core.Tests has no Newtonsoft reference) would lock in #3829.
  3. CI on this PR is red across AppVeyor / Azure / Bitrise / TeamCity while develop is green. Please get a green run (or confirm it is fork-infra only) before we merge.

Nit: Lazy<RegistryKey> (ExecutionAndPublication) would replace the hand-rolled double-checked locking; Cake already uses Lazy<T> elsewhere. Not a blocker if you prefer to keep the explicit factory.

Happy to re-review once the formatting and test are in.

Comment thread src/Cake.Core/IO/WindowsRegistryKey.cs Resolved
Comment thread src/Cake.Core/IO/WindowsRegistryKey.cs Resolved
@Samyra312007

Copy link
Copy Markdown
Contributor Author

@devlead I would like to confirm are these CI failures are related to my PR or are these the existing failures.

@devlead

devlead commented Sep 22, 2026

Copy link
Copy Markdown
Member

@devlead I would like to confirm are these CI failures are related to my PR or are these the existing failures.

All CI on target branch is green and confirmed by checking out this PR and executing

dotnet tool restore 
dotnet cake

(build.sh / build.ps1 in repo root will do similar)
That errors occur locally too, so it's issues introduced in this PR.

- src\Cake.Core\IO\WindowsRegistryKey.cs(9,1): error IDE0055: Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055)
- src\Cake.Core\IO\WindowsRegistryKey.cs(10,1): error IDE0055: Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055)

More guidance on contributing can be read in our Contribution Guidelines

@devlead devlead changed the title (GH-3829) Defer Windows registry key creation until used. GH3829: Defer Windows registry key creation until used. Sep 22, 2026
WindowsRegistry eagerly materialized Microsoft.Win32.Registry base keys in
its property getters, which throws PlatformNotSupportedException (or
returns null on newer .NET versions) on Linux/macOS. This broke reflective
consumers such as serializing the build context to JSON on non-Windows
platforms.

Defer creation of the underlying RegistryKey until an actual registry
operation is performed, so accessing IRegistry properties is throw-free on
all platforms while actual registry use still fails with
PlatformNotSupportedException at the point of use.

- fixes cake-build#3829

@devlead devlead left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM 👍

@devlead
devlead merged commit bd50e1b into cake-build:develop Sep 22, 2026
18 checks passed
@devlead

devlead commented Sep 22, 2026

Copy link
Copy Markdown
Member

@Samyra312007 your changes have been merged, thanks for your contribution 👍

kubaflo added a commit to dotnet/maui that referenced this pull request Nov 26, 2026
Updated [cake.tool](https://github.com/cake-build/cake) from 6.3.0 to
7.0.0.

<details>
<summary>Release notes</summary>

_Sourced from [cake.tool's
releases](https://github.com/cake-build/cake/releases)._

## 7.0.0

As part of this release we had [106
issues](https://github.com/cake-build/cake/milestone/98?closed=1)
closed.



__Breaking changes__

- [__#​2835__](cake-build/cake#2835) Migrate
Cake.Tool from Autofac to Microsoft.Extensions.DependencyInjection by
[patriksvensson](https://github.com/patriksvensson) resolved in
[__!5010__](cake-build/cake#5010) by
[devlead](https://github.com/devlead)
- [__#​4997__](cake-build/cake#4997) Remove
Unsupported TFMS .NET 8 & 9 (net8.0 & net9.0) by
[devlead](https://github.com/devlead) resolved in
[__!5045__](cake-build/cake#5045) by
[devlead](https://github.com/devlead)
- [__#​5001__](cake-build/cake#5001) Update
LatestPotentialBreakingChange to 7.0.0 by
[devlead](https://github.com/devlead) resolved in
[__!5012__](cake-build/cake#5012) by
[devlead](https://github.com/devlead)
- [__#​5003__](cake-build/cake#5003) Remove
obsolete members / methods by [devlead](https://github.com/devlead)
resolved in [__!5012__](cake-build/cake#5012) by
[devlead](https://github.com/devlead)
- [__#​5060__](cake-build/cake#5060) Make
unified dependency graph the default when running multiple targets by
[devlead](https://github.com/devlead) resolved in
[__!5061__](cake-build/cake#5061) by
[devlead](https://github.com/devlead)
- [__#​5079__](cake-build/cake#5079) Update
Spectre.Console.Cli to 0.57.2 by [devlead](https://github.com/devlead)
resolved in [__!5080__](cake-build/cake#5080) by
[devlead](https://github.com/devlead)
- [__#​5169__](cake-build/cake#5169) Make
environment-defaulted DotNet switches tri-state to allow explicit
opt-out by [devlead](https://github.com/devlead) resolved in
[__!5170__](cake-build/cake#5170) by
[devlead](https://github.com/devlead)

__Bugs__

- [__#​2646__](cake-build/cake#2646) Incorrect
translation of literal constants used as optional parameter values (when
constants are defined in a separate file) by
[Meertman](https://github.com/Meertman) resolved in
[__!5017__](cake-build/cake#5017) by
[devlead](https://github.com/devlead)
- [__#​3829__](cake-build/cake#3829) Error:
Error getting value from 'CurrentUser' on
'Cake.Core.IO.WindowsRegistry'. by
[vCillusion](https://github.com/vCillusion) resolved in
[__!4802__](cake-build/cake#4802) by
[wyf027](https://github.com/wyf027),
[__!5011__](cake-build/cake#5011) by
[Samyra312007](https://github.com/Samyra312007)
- [__#​3830__](cake-build/cake#3830)
[Cake.Frosting] has exit code -1 for any kind of error by
[juwens](https://github.com/juwens) resolved in
[__!3831__](cake-build/cake#3831) by
[juwens](https://github.com/juwens),
[__!5015__](cake-build/cake#5015) by
[devlead](https://github.com/devlead)
- [__#​5095__](cake-build/cake#5095) Fix Unix
GetSpecialPath HOME and Program Files mapping by
[devlead](https://github.com/devlead) resolved in
[__!5096__](cake-build/cake#5096) by
[devlead](https://github.com/devlead)
- [__#​5155__](cake-build/cake#5155)
Interpolated strings bind to string log overloads instead of
FormattableString overloads by [devlead](https://github.com/devlead)
resolved in [__!5156__](cake-build/cake#5156) by
[devlead](https://github.com/devlead)
- [__#​5157__](cake-build/cake#5157) Using
declarations without parentheses on the first line are hoisted as using
alias directives by [devlead](https://github.com/devlead) resolved in
[__!5158__](cake-build/cake#5158) by
[devlead](https://github.com/devlead)

__Documentation__

- [__#​5024__](cake-build/cake#5024) Add Cake
DSL examples to BuildSystem and CI provider APIs by
[devlead](https://github.com/devlead) resolved in
[__!5025__](cake-build/cake#5025) by
[devlead](https://github.com/devlead)
- [__#​5026__](cake-build/cake#5026) Add a
Cake DSL example to GetCallerInfo by
[devlead](https://github.com/devlead) resolved in
[__!5027__](cake-build/cake#5027) by
[devlead](https://github.com/devlead)
- [__#​5028__](cake-build/cake#5028) Add Cake
DSL examples to Task, Does, and CakeTaskBuilder by
[devlead](https://github.com/devlead) resolved in
[__!5029__](cake-build/cake#5029) by
[devlead](https://github.com/devlead)
- [__#​5030__](cake-build/cake#5030) Add Cake
DSL examples to ICakeContext, IFileSystem, ProcessArgumentBuilder, and
ToolSettings by [devlead](https://github.com/devlead) resolved in
[__!5031__](cake-build/cake#5031) by
[devlead](https://github.com/devlead)

__Features__

- [__#​1269__](cake-build/cake#1269) Request
for Task.Tool("nuget:?package=etc") by [jnm2](https://github.com/jnm2)
- [__#​2471__](cake-build/cake#2471) Install a
nuget tool on demand by [Roemer](https://github.com/Roemer) resolved in
[__!5009__](cake-build/cake#5009) by
[devlead](https://github.com/devlead)
- [__#​4660__](cake-build/cake#4660) Consider
adding IFileSystemInfo returning APIs in addition to IFile and
IDirectory by [paulomorgado](https://github.com/paulomorgado) resolved
in [__!4704__](cake-build/cake#4704) by
[Abde1rahman1](https://github.com/Abde1rahman1)
- [__#​4998__](cake-build/cake#4998) Add .NET
11 (net11.0) TFM by [devlead](https://github.com/devlead) resolved in
[__!5044__](cake-build/cake#5044) by
[devlead](https://github.com/devlead)
- [__#​4999__](cake-build/cake#4999) C# 15
Scripting Support by [devlead](https://github.com/devlead) resolved in
[__!5046__](cake-build/cake#5046) by
[devlead](https://github.com/devlead)
- [__#​5066__](cake-build/cake#5066) Drop dead
runtime checks and Task.Run wrappers by
[devlead](https://github.com/devlead) resolved in
[__!5067__](cake-build/cake#5067) by
[devlead](https://github.com/devlead)
- [__#​5070__](cake-build/cake#5070) Add
GitHub Actions AzureLogin for OIDC by
[devlead](https://github.com/devlead) resolved in
[__!5071__](cake-build/cake#5071) by
[devlead](https://github.com/devlead)
- [__#​5081__](cake-build/cake#5081) Add IFile
content read/write extensions aligned with System.IO.File by
[devlead](https://github.com/devlead) resolved in
[__!5082__](cake-build/cake#5082) by
[devlead](https://github.com/devlead)
- [__#​5085__](cake-build/cake#5085) Add
opt-in .NET 11 Process API support to Cake by
[devlead](https://github.com/devlead) resolved in
[__!5086__](cake-build/cake#5086) by
[devlead](https://github.com/devlead)
- [__#​5087__](cake-build/cake#5087) Add Tar
and Untar aliases for creating and extracting tar archives by
[devlead](https://github.com/devlead) resolved in
[__!5088__](cake-build/cake#5088) by
[devlead](https://github.com/devlead)
- [__#​5089__](cake-build/cake#5089) Add
DotNetUpdatePackage aliases for dotnet package update by
[devlead](https://github.com/devlead) resolved in
[__!5090__](cake-build/cake#5090) by
[devlead](https://github.com/devlead)
- [__#​5103__](cake-build/cake#5103) Add
DotNetDownloadPackage aliases for dotnet package download by
[devlead](https://github.com/devlead) resolved in
[__!5104__](cake-build/cake#5104) by
[devlead](https://github.com/devlead)
- [__#​5105__](cake-build/cake#5105) Add
DotNetSlnMigrate aliases for sln to slnx by
[devlead](https://github.com/devlead) resolved in
[__!5106__](cake-build/cake#5106) by
[devlead](https://github.com/devlead)
- [__#​5107__](cake-build/cake#5107) Add
DotNetProjectConvert aliases for file-based apps by
[devlead](https://github.com/devlead) resolved in
[__!5108__](cake-build/cake#5108) by
[devlead](https://github.com/devlead)
- [__#​5109__](cake-build/cake#5109) Add
DotNetNuGetWhy aliases for package dependency graphs by
[devlead](https://github.com/devlead) resolved in
[__!5111__](cake-build/cake#5111) by
[devlead](https://github.com/devlead)
- [__#​5112__](cake-build/cake#5112) Add
DotNetNuGetLocalsList and Clear aliases by
[devlead](https://github.com/devlead) resolved in
[__!5113__](cake-build/cake#5113) by
[devlead](https://github.com/devlead)
- [__#​5115__](cake-build/cake#5115) Add .NET
11 DotNetRun settings (environment, file, device, artifacts-path,
no-cache) by [devlead](https://github.com/devlead) resolved in
[__!5116__](cake-build/cake#5116) by
[devlead](https://github.com/devlead)
- [__#​5125__](cake-build/cake#5125) Add
ProcessSettings.StandardInput for StartProcess by
[devlead](https://github.com/devlead) resolved in
[__!5126__](cake-build/cake#5126) by
[devlead](https://github.com/devlead)
 ... (truncated)

Commits viewable in [compare
view](cake-build/cake@v6.3.0...v7.0.0).
</details>

[![Dependabot compatibility
score](https://dependabot-badges.githubapp.com/badges/compatibility_score?dependency-name=cake.tool&package-manager=nuget&previous-version=6.3.0&new-version=7.0.0)](https://docs.github.com/en/github/managing-security-vulnerabilities/about-dependabot-security-updates#about-compatibility-scores)

Dependabot will resolve any conflicts with this PR as long as you don't
alter it yourself. You can also trigger a rebase manually by commenting
`@dependabot rebase`.

[//]: # (dependabot-automerge-start)
[//]: # (dependabot-automerge-end)

---

<details>
<summary>Dependabot commands and options</summary>
<br />

You can trigger Dependabot actions by commenting on this PR:
- `@dependabot rebase` will rebase this PR
- `@dependabot recreate` will recreate this PR, overwriting any edits
that have been made to it
- `@dependabot show <dependency name> ignore conditions` will show all
of the ignore conditions of the specified dependency
- `@dependabot ignore this major version` will close this PR and stop
Dependabot creating any more for this major version (unless you reopen
the PR or upgrade to it yourself)
- `@dependabot ignore this minor version` will close this PR and stop
Dependabot creating any more for this minor version (unless you reopen
the PR or upgrade to it yourself)
- `@dependabot ignore this dependency` will close this PR and stop
Dependabot creating any more for this dependency (unless you reopen the
PR or upgrade to it yourself)


</details>

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Jakub Florkowski <kubaflo123@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Error: Error getting value from 'CurrentUser' on 'Cake.Core.IO.WindowsRegistry'.

2 participants