Skip to content

#45 Enable AOT/trim analyzers and source-generate key regex - #58

Merged
matthewdevenny merged 2 commits into
mainfrom
matt/45-polish
Jul 30, 2026
Merged

matthewdevenny merged 2 commits into
mainfrom
matt/45-polish

Conversation

@matthewdevenny

Copy link
Copy Markdown
Contributor

Part of #45 (AOT / trimming compatibility). One of three independent PRs splitting that issue.

What

  • Enable <IsAotCompatible>true</IsAotCompatible> on the core (NatsDistributedCache) and hybrid (NatsHybridCacheExtensions) packages.
  • Convert the RegexOptions.Compiled key-validation regex in NatsExtensions to a source-generated [GeneratedRegex] partial method (pattern semantics preserved exactly, \A…\z anchors and / = . chars kept).
  • Regenerate the NuGet lock files.
  • Add unit tests for the key-validation regex accept/reject behavior.

Notes

  • With the analyzers active, both packages build with zero IL2xxx/IL3xxx warnings on net8.0 and net10.0 under -p TreatWarningsAsErrors=true — including the generic NatsHybridCacheSerializer<T> / serializerRegistry.GetSerializer<T>() path, so no [RequiresUnreferencedCode]/[RequiresDynamicCode] forwarding was needed.
  • RegexOptions.Compiled is not flagged by the IL analyzers, but it degrades under AOT (Reflection.Emit → interpreter fallback); the conversion is proactive hardening.
  • The only lock-file delta is the SDK-implicit, build-time-only Microsoft.NET.ILLink.Tasks package that enabling the analyzers pulls in. It has PrivateAssets and never flows to package consumers. Regenerating is required because the repo enforces RestoreLockedMode=true + a CI drift check.

Testing

  • dotnet build -p TreatWarningsAsErrors=true: 0 warnings / 0 errors, net8.0 + net10.0.
  • Unit tests: 122/122 pass (both TFMs). Integration tests: 78/78 pass.

🤖 Generated with Claude Code

Enable `IsAotCompatible` on the core and hybrid packages; both build clean
with zero IL2xxx/IL3xxx warnings under `-p TreatWarningsAsErrors=true` on
net8.0 and net10.0, including the generic NatsHybridCacheSerializer<T> path.

Convert the `RegexOptions.Compiled` key-validation regex in NatsExtensions to
a source-generated `[GeneratedRegex]` partial method (pattern semantics
preserved) so it does not fall back to the interpreter under AOT.

Enabling the analyzers pulls in the SDK-implicit, build-time-only
`Microsoft.NET.ILLink.Tasks` package, so the NuGet lock files are regenerated
(that one package is the only delta; PrivateAssets, never flows to consumers).

Add unit tests covering the key-validation regex accept/reject behavior.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Signed-off-by: Matthew DeVenny <matt@codecargo.com>
Copilot AI review requested due to automatic review settings July 28, 2026 15:48

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 advances issue #45’s AOT/trimming compatibility work by enabling AOT/trim analyzers for the core and hybrid packages, replacing the runtime-compiled key-validation regex with a source-generated regex, and adding unit coverage to ensure key-validation behavior remains unchanged.

Changes:

  • Enabled AOT/trim analyzer behavior via <IsAotCompatible>true</IsAotCompatible> in both NatsDistributedCache and NatsHybridCacheExtensions.
  • Replaced the RegexOptions.Compiled key-validation regex field with a [GeneratedRegex] source-generated regex in NatsExtensions.
  • Added unit tests to validate accepted/rejected key behavior and regenerated RID-specific NuGet lockfiles.

Reviewed changes

Copilot reviewed 12 out of 12 changed files in this pull request and generated no comments.

Show a summary per file
File Description
test/UnitTests/Extensions/NatsExtensionsKeyValidationTests.cs Adds unit tests asserting key-validation accept/reject behavior via TryPutWithTtlAsync.
src/NatsDistributedCache/NatsExtensions.cs Converts key-validation regex to source-generated [GeneratedRegex] and updates call site.
src/NatsDistributedCache/NatsDistributedCache.csproj Enables AOT compatibility/analyzer behavior for the core package.
src/NatsHybridCacheExtensions/NatsHybridCacheExtensions.csproj Enables AOT compatibility/analyzer behavior for the hybrid extensions package.
src/NatsDistributedCache/packages.win-x64.lock.json Lockfile regen to include Microsoft.NET.ILLink.Tasks (SDK/analyzer-induced).
src/NatsDistributedCache/packages.linux-x64.lock.json Lockfile regen to include Microsoft.NET.ILLink.Tasks (SDK/analyzer-induced).
src/NatsDistributedCache/packages.linux-arm64.lock.json Lockfile regen to include Microsoft.NET.ILLink.Tasks (SDK/analyzer-induced).
src/NatsDistributedCache/packages.osx-arm64.lock.json Lockfile regen to include Microsoft.NET.ILLink.Tasks (SDK/analyzer-induced).
src/NatsHybridCacheExtensions/packages.win-x64.lock.json Lockfile regen to include Microsoft.NET.ILLink.Tasks (SDK/analyzer-induced).
src/NatsHybridCacheExtensions/packages.linux-x64.lock.json Lockfile regen to include Microsoft.NET.ILLink.Tasks (SDK/analyzer-induced).
src/NatsHybridCacheExtensions/packages.linux-arm64.lock.json Lockfile regen to include Microsoft.NET.ILLink.Tasks (SDK/analyzer-induced).
src/NatsHybridCacheExtensions/packages.osx-arm64.lock.json Lockfile regen to include Microsoft.NET.ILLink.Tasks (SDK/analyzer-induced).

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

The ILLink.Tasks version in the lock files floats with the SDK/runtime patch
level. Regenerate against the current .NET 8/10 SDK so the entries match what
CI resolves (10.0.10 / 8.0.29), fixing the "NuGet lock files are out of date"
drift check. Only Microsoft.NET.ILLink.Tasks changes; no other packages.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Signed-off-by: Matthew DeVenny <matt@codecargo.com>
@matthewdevenny
matthewdevenny requested a review from mtmk July 28, 2026 19:43

@mtmk mtmk 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.

LGTM

@matthewdevenny
matthewdevenny merged commit 137eb25 into main Jul 30, 2026
2 checks passed
@matthewdevenny
matthewdevenny deleted the matt/45-polish branch July 30, 2026 00:04
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.

3 participants