Skip to content

chore: Generate regular expressions at compile time - #1821

Merged
HofmeisterAn merged 1 commit into
developfrom
feature/use-generated-regular-expressions
Nov 15, 2026
Merged

HofmeisterAn merged 1 commit into
developfrom
feature/use-generated-regular-expressions

Conversation

@HofmeisterAn

@HofmeisterAn HofmeisterAn commented Nov 15, 2026 •

Copy link
Copy Markdown
Collaborator

What does this PR do?

Moves the regular expressions with a constant pattern into an internal RegexPatterns class per project (core, K3s, Commons). On .NET 7 and later they use [GeneratedRegex], on .NET Standard they fall back to a lazily created Regex instance. Call sites now use, e.g., RegexPatterns.ComposeProjectName().IsMatch(...).

Why is it important?

Removing the match timeouts in #1820 surfaced 12 SonarCloud SYSLIB1045 issues. Source-generated regular expressions resolve them and avoid parsing (and for the image reference pattern, RegexOptions.Compiled emitting) the regular expressions at runtime.

Related issues

Summary by CodeRabbit

  • Refactor
    • Standardized pattern matching across container configuration, Dockerfile parsing, image references, and input validation.
    • Existing behavior remains unchanged.

@HofmeisterAn HofmeisterAn added the chore A change that doesn't impact the existing functionality, e.g. internal refactorings or cleanups label Nov 15, 2026
@netlify

netlify Bot commented Nov 15, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for testcontainers-dotnet ready!

Name Link
🔨 Latest commit 2945dad
🔍 Latest deploy log https://app.netlify.com/projects/testcontainers-dotnet/deploys/6af97b3cb36051c7515556e5
😎 Deploy Preview https://deploy-preview-1821--testcontainers-dotnet.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@coderabbitai

coderabbitai Bot commented Nov 15, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The pull request centralizes regex patterns in generated and lazy providers. Existing builder, Dockerfile, image, and K3s code now uses these providers.

Changes

Regex Pattern Centralization

Layer / File(s) Summary
Shared regex pattern providers
src/Testcontainers/RegexPatterns.cs, src/Testcontainers.K3s/RegexPatterns.cs
The projects define shared regex patterns. On .NET 7 and later, the patterns use compile-time generation. Other targets use lazy regex instances.
Builder and Dockerfile pattern migration
src/Testcontainers/Builders/*, src/Testcontainers/Images/DockerfileArchive.cs, tests/Testcontainers.Commons/*
Builders and Dockerfile parsing use shared patterns for path prefixes, IDs, Compose names, Dockerfile instructions, and variables. The test parser uses its generated FROM pattern.
Image and kubeconfig pattern migration
src/Testcontainers/Images/DockerImage.cs, src/Testcontainers/Images/IgnoreFile.cs, src/Testcontainers/Images/MatchImage.cs, src/Testcontainers.K3s/K3sContainer.cs
Image tag, image reference, and regex-metacharacter matching use shared patterns. K3s kubeconfig replacement uses its shared server pattern.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Refactor

Possibly related PRs

Merge Risk: Minimal · up to 2945d

This change moves constant regular expressions into compile-time generated or lazily created providers without intended behavior changes. No concrete defects were identified, so merge risk is minimal.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the main change: regular expressions with constant patterns move to compile-time generation. The changeset adds RegexPatterns classes that use GeneratedRegex on .NET 7+ and call sites …
Description check Passed The description follows the template. It explains what changed (constant-pattern regexes moved into per-project RegexPatterns classes, GeneratedRegex on .NET 7+, lazy Regex on .NET Standard) and why (…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title names the main change: regular expressions with constant patterns move to compile-time generation. The changeset adds RegexPatterns classes that use GeneratedRegex on .NET 7+ and call sites that use them. The 'chore:' prefix is a conventional-commit type and does not hurt the title's clarity.

Full details: Description check

Explanation

The description follows the template. It explains what changed (constant-pattern regexes moved into per-project RegexPatterns classes, GeneratedRegex on .NET 7+, lazy Regex on .NET Standard) and why (SYSLIB1045 issues after #1820, avoiding runtime parsing and RegexOptions.Compiled emission). It links #1820 under Related issues. The 'How to test this PR' section is still the unfilled template placeholder, and the Related issues entry does not use Closes, Relates, Requires, or Supersedes as intended. Those are recommended sections, so the check passes.

Finishing Touches

Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

A rabbit checks each regex line
And finds the shared patterns in time
From Docker paths to K3s
Each match now has a home with ease
Then hops away beneath the pines

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 823cfb6b-6995-459c-b4dc-ec4479bbf037
Commits

Reviewing files that changed from the base of the PR and between 479ce92 and 2945dad.

Files selected for processing: 12

  • src/Testcontainers.K3s/K3sContainer.cs
  • src/Testcontainers.K3s/RegexPatterns.cs
  • src/Testcontainers/Builders/BuildKitImageFromDockerfileBuilder.cs
  • src/Testcontainers/Builders/ComposeBuilder.cs
  • src/Testcontainers/Builders/ImageFromDockerfileBuilder.cs
  • src/Testcontainers/Images/DockerImage.cs
  • src/Testcontainers/Images/DockerfileArchive.cs
  • src/Testcontainers/Images/IgnoreFile.cs
  • src/Testcontainers/Images/MatchImage.cs
  • src/Testcontainers/RegexPatterns.cs
  • tests/Testcontainers.Commons/DockerfileParser.cs
  • tests/Testcontainers.Commons/RegexPatterns.cs

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.

@HofmeisterAn
HofmeisterAn merged commit 4fec38e into develop Nov 15, 2026
163 of 164 checks passed
@HofmeisterAn
HofmeisterAn deleted the feature/use-generated-regular-expressions branch November 15, 2026 11:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

chore A change that doesn't impact the existing functionality, e.g. internal refactorings or cleanups

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant