Skip to content

Refuse DateTime.UtcNow, regions and a homemade FakeTimeProvider in the build - #568

Merged
kirill-abblix merged 3 commits into
developfrom
feature/time-and-region-bans-in-the-build
Sep 24, 2026
Merged

kirill-abblix merged 3 commits into
developfrom
feature/time-and-region-bans-in-the-build

Conversation

@kirill-abblix

@kirill-abblix kirill-abblix commented Sep 24, 2026 •

Copy link
Copy Markdown
Member

Three house rules were enforced by an editor hook that matched source text. This moves them into the build, or into tests that ask the compiler, and fixes every existing violation instead of suppressing it.

  • DateTime.UtcNow and DateTimeOffset.UtcNow, and with them DateTime.Now, DateTimeOffset.Now and DateTime.Today, are banned through Microsoft.CodeAnalysis.BannedApiAnalyzers and BannedSymbols.txt, as error RS0030 in every project. The existing uses were almost all in tests. They now read TimeProvider.System, and so does the static licensing code, which has no injected clock. The CIBA handler sample in the docs takes an injected TimeProvider, since hosts copy it.
  • #region is refused by NoRegionsTests, which reads the directives from the syntax tree. StyleCop's SA1124 would have done this at build time, but no StyleCop version loads into the .NET 11 compiler: every rule fails with CS8032. All existing regions are removed. Where a region title named the specification a group of constants comes from, the title stays as a plain comment.
  • A homemade FakeTimeProvider is refused by NoHomemadeFakeTimeProviderTests, which looks through the assemblies this repository builds for net11.0 (the two source generators build for netstandard2.0 and are not walked). An analyzer can ban using an API but not declaring a type. The assembly loader it shares with ResultArmsMustBeDistinguishableTests moves to TestInfrastructure/BuiltAssemblies.

The analyzer package is PrivateAssets="all", so it does not reach the published packages.

…e build

Three house rules were kept by an editor hook that matched source text.
They now sit where the compiler or a test that asks it decides:

- DateTime.UtcNow and DateTimeOffset.UtcNow are banned through
  Microsoft.CodeAnalysis.BannedApiAnalyzers and BannedSymbols.txt, as
  error RS0030 in every project. Every existing use is replaced: tests
  and the static licensing code read TimeProvider.System, and the CIBA
  doc sample takes an injected TimeProvider.
- #region is refused by NoRegionsTests, which reads directives from the
  syntax tree. StyleCop's SA1124 would have done this at build time,
  but no StyleCop version loads into the .NET 11 compiler (CS8032 for
  every rule). Every existing region is removed; where a region title
  named the specification a group of members comes from, the title
  stays as a comment.
- A type named FakeTimeProvider declared in any assembly built here is
  refused by NoHomemadeFakeTimeProviderTests, over the built assemblies.
  The loader it shares with ResultArmsMustBeDistinguishableTests moves
  to TestInfrastructure/BuiltAssemblies.
Removing the regions dropped three specification titles in
IanaClaimTypes (ETSI GS NFV-SEC 022, OAuth JWT Introspection, OAuth
Rich Authorization Requests), and left the error codes that no region
covered under the Dynamic Client Registration header. The titles are
back as comments, and those codes now sit above the first header.

DateTime.Now, DateTimeOffset.Now and DateTime.Today join the banned
list; nothing here used them. The Abblix.Jwt README sample reads its
expiry from a TimeProvider. The blank lines left beside braces where a
region ended are gone.

The FakeTimeProvider test also recognizes the names the compiler emits
for a generic or file-local type, and the loader's comment says that an
assembly which fails to load is skipped rather than reported.
@sonarqubecloud

Copy link
Copy Markdown

@kirill-abblix
kirill-abblix merged commit 30c188a into develop Sep 24, 2026
25 checks passed
@kirill-abblix
kirill-abblix deleted the feature/time-and-region-bans-in-the-build branch September 24, 2026 17:57
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.

1 participant