Bennewitz.Ninja.AutoVersioning
2026.4.1124
Prefix Reserved
See the version list below for details.
dotnet add package Bennewitz.Ninja.AutoVersioning --version 2026.4.1124
NuGet\Install-Package Bennewitz.Ninja.AutoVersioning -Version 2026.4.1124
<PackageReference Include="Bennewitz.Ninja.AutoVersioning" Version="2026.4.1124"> <PrivateAssets>all</PrivateAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets> </PackageReference>
<PackageVersion Include="Bennewitz.Ninja.AutoVersioning" Version="2026.4.1124" />
<PackageReference Include="Bennewitz.Ninja.AutoVersioning"> <PrivateAssets>all</PrivateAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets> </PackageReference>
paket add Bennewitz.Ninja.AutoVersioning --version 2026.4.1124
#r "nuget: Bennewitz.Ninja.AutoVersioning, 2026.4.1124"
#:package Bennewitz.Ninja.AutoVersioning@2026.4.1124
#addin nuget:?package=Bennewitz.Ninja.AutoVersioning&version=2026.4.1124
#tool nuget:?package=Bennewitz.Ninja.AutoVersioning&version=2026.4.1124
Bennewitz.Ninja.AutoVersioning
A Roslyn incremental source generator that injects assembly metadata attributes and build-time constants into .NET projects at compile time — no AssemblyInfo.cs, no disk writes, full IDE caching.
Requirements
.NET SDK 6.0 or later (requires Roslyn 4.0+ for IIncrementalGenerator support). The consuming project may target any framework — .NET Framework 4.x, .NET Standard, .NET 6+, etc.
Quick Start
1. Add the package:
<PackageReference Include="Bennewitz.Ninja.AutoVersioning" Version="x.x.x" />
2. Enable the generator in your root Directory.Build.props (or any .props / .csproj imported by your build):
<PropertyGroup>
<GenerateAutoVersionedAssemblyInfo>true</GenerateAutoVersionedAssemblyInfo>
<AssemblyCompany>YourCompany</AssemblyCompany>
<AssemblyProduct>YourProduct</AssemblyProduct>
<CommitSha Condition="'$(CommitSha)' == ''">$(GITHUB_SHA)</CommitSha>
<PublicVersion Condition="'$(PublicVersion)' == ''">$(MY_VERSION_VAR)</PublicVersion>
</PropertyGroup>
Note: The package's auto-imported
Build.targetssuppresses the 8 SDK-generated attributes that this generator also emits (e.g.AssemblyVersion,AssemblyCopyright), so you do not need to disable anything yourself. Do not add<GenerateAssemblyInfo>false</GenerateAssemblyInfo>: it is redundant, and broader than intended — that switch disables every SDK-generated attribute, includingInternalsVisibleTodeclared via MSBuild<InternalsVisibleTo>items. (A hand-writtenInternalsVisibleToin a compiled.csfile is unaffected either way.)
A ready-to-use template is available at Directory.Build.props.template.
3. Build. The generator runs automatically — no further setup needed.
Tip: View the generated files in your IDE under
Dependencies → Analyzers → *SourceGenerators.
Generated Output
Given a build on 2026-04-29 at 14:35 UTC for company Acme Corp, product Acme App, commit abc1234def, and public version 3.1.0:
AutoVersionedAssemblyInfo.g.cs
using System.Reflection;
using System.Runtime.CompilerServices;
[assembly: AssemblyDescription("Assembly Version: 2026.2.429.1435")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyCompany("Acme Corp")]
[assembly: AssemblyProduct("Acme App v2026.2.429.1435")]
[assembly: AssemblyCopyright("Copyright 2026 [Release]")]
[assembly: AssemblyVersion("2026.2.429.1435")]
[assembly: AssemblyFileVersion("2026.2.429.1435")]
[assembly: AssemblyInformationalVersion("Built with ♥ abc1234def")]
[assembly: AssemblyMetadata("CommitSha", "abc1234def")]
[assembly: AssemblyMetadata("GITHUB_SHA", "abc1234def")]
[assembly: AssemblyMetadata("PublicVersion", "3.1.0")]
DirectoryBuildInfo.g.cs
// <auto-generated/>
namespace Bennewitz.Ninja.AutoVersioning
{
/// <summary>Build-time constants generated by the source generator.</summary>
public static class DirectoryBuildInfo
{
public const string BuildRelease = "Version 2026.2.429.1435 [built 2026-04-29 14:35:00 (UTC+00:00)]";
}
}
Use DirectoryBuildInfo.BuildRelease wherever you need the build version at runtime — error trackers, log enrichment, health endpoints — without reading environment variables.
Configuration Reference
| MSBuild Property | Description | Required |
|---|---|---|
GenerateAutoVersionedAssemblyInfo |
Set to true to enable the generator |
Yes |
AssemblyCompany |
Value for [assembly: AssemblyCompany(...)] |
Yes |
AssemblyProduct |
Value for [assembly: AssemblyProduct(...)] |
Yes |
CopyrightHolder |
Name shown in the copyright attribute — defaults to AssemblyCompany if omitted |
No |
CommitSha |
Git commit SHA | No |
PublicVersion |
Your app's public-facing version string | No |
Configuration |
Debug / Release — set automatically by MSBuild. An empty value counts as Debug for the hold below, because the package's props are imported before the SDK applies its default |
Auto |
BuildTimestamp |
Build start time in yyyyMMddHHmmss format, captured at MSBuild evaluation time, before any project compiles. In Debug it is held to the day — midnight — so repeated builds reuse one value and an unchanged build compiles nothing; every other configuration captures the current time. Set it yourself to override either, on CI or per project: dotnet build -p:BuildTimestamp=$(date +%Y%m%d%H%M%S) |
Auto |
AutoVersion |
Output. The four-part version YEAR.QUARTER.MMdd.HHmm the generator stamps, derived from BuildTimestamp during property evaluation so MSBuild can read it too. Overridable |
Auto |
AutoPackageVersion |
Output. The same version without the time component: YEAR.QUARTER.MMdd. Overridable |
Auto |
Using the version from MSBuild
The generator calculates the version inside the Roslyn pipeline, which is too late for anything
MSBuild decides. PackageVersion is the common casualty: it is evaluated long before compilation,
so a library packs as NuGet's default 1.0.0 while the assembly inside that package is stamped
2026.3.913.1613. The package and its own contents disagree, and nothing reports it.
AutoVersion and AutoPackageVersion are derived during property evaluation from the same
BuildTimestamp the generator is handed, so they are available early enough to assign:
<PropertyGroup>
<PackageVersion>$(AutoPackageVersion)</PackageVersion>
</PropertyGroup>
Neither property is assigned to anything automatically — opting in is explicit, because this
package should not quietly take over PackageVersion for every project that references it.
Three-part for packages on purpose: four-part versions are not SemVer, which matters for anything published publicly, and
Pack.ps1already defaults to that form.
GenerateAutoVersionedAssemblyInfo may be set anywhere the build sees it — Directory.Build.props,
another .props file, or the .csproj itself. The SDK attribute suppressions live in the package's
auto-imported Build.targets, which NuGet imports after the project body, so the flag is honoured
regardless of where it is declared.
CI Provider Mappings
Map CommitSha from whichever variable your provider sets. The CI-flag column is listed for
convenience only — this package does not read it; it is what you would condition your own MSBuild
targets on (e.g. Condition="'$(GITHUB_ACTIONS)' == 'true'").
| Provider | Commit SHA | CI flag |
|---|---|---|
| GitHub Actions | $(GITHUB_SHA) |
$(GITHUB_ACTIONS) |
| GitLab CI | $(CI_COMMIT_SHA) |
$(GITLAB_CI) |
| Bitbucket Pipelines | $(BITBUCKET_COMMIT) |
$(CI) |
Version Format
The generator uses CalVer: YEAR.QUARTER.MMDD.HHmm
| Part | Example | Meaning |
|---|---|---|
| Major | 2026 |
Calendar year |
| Minor | 2 |
Quarter (1–4) |
| Build | 429 |
Month + day (MMdd as integer) |
| Revision | 1435 |
Hour + minute (HHmm as integer) |
Reading a stamp back
The generator ships as an analyzer: your compiler loads it, and your code never sees it — so nothing in your project can turn one of those versions back into the moment it was stamped. The companion package Bennewitz.Ninja.AutoVersioning.Abstractions is the small library that can.
<PackageReference Include="Bennewitz.Ninja.AutoVersioning.Abstractions" Version="x.x.x" />
using Bennewitz.Ninja.AutoVersioning;
var assembly = new FileInfo(typeof(Thing).Assembly.Location);
if (BuildVersion.TryGetFromFile(assembly, out var version))
{
Console.WriteLine(version!.Version); // 2026.4.1119.1613
Console.WriteLine(version.BuildDateTime); // the moment this assembly was stamped
}
It targets netstandard2.0, carries no dependencies, and neither package requires the other — install either on its own, though reading a stamp back is of limited use if nothing is stamping it. TryGetFromFile returns false rather than throwing for a version it does not recognise, such as an unstamped 1.0.0.0, so it is safe to call on any assembly. Its own README documents the rest.
The job it exists for: asserting in your own tests that a built artifact carries the stamp this build gave it.
How It Works
This is a modern C# IIncrementalGenerator. Compared to legacy ISourceGenerator approaches it:
- Runs entirely in-memory — zero files written to disk
- Uses the Roslyn caching pipeline — the IDE (Rider, Visual Studio) only re-runs the generator when its inputs change, eliminating background compilation loops
- Reads MSBuild properties via
AnalyzerConfigOptionsProvider— safe, no direct environment variable reads
The package auto-imports two files via NuGet: Build.props, which declares the CompilerVisibleProperty items and derives AutoVersion / AutoPackageVersion, and Build.targets, which suppresses the SDK's conflicting AssemblyInfo attributes (preventing CS0579 duplicate attribute errors when the generator is enabled). The suppressions live in the .targets because NuGet imports it after the consuming project's body, so GenerateAutoVersionedAssemblyInfo is honoured wherever it is set.
Timestamp Consistency in Multi-Project Builds
BuildTimestamp is captured at property evaluation rather than when a project compiles — but that is captured once per project, not once per build. A Debug build is held to the day and so is exact at any size. A clock-stamped build spreads: measured on 150 projects with default parallelism, every one of 25 builds used more than one value, up to 12, spread a mean of 8 seconds, and 2 of those 25 produced assemblies whose versions were a minute apart inside one build. Four projects showed none of this; the spread grows with project count. ⛔ In a solution of any real size, pass -p:BuildTimestamp on any build whose version must be uniform — see the TL;DR below. Full measurements, by project count and build mode, are in docs/MultiProjectBuildTimestamps.md, with the alternatives considered.
A Debug build holds its stamp to the day. A stamp is an input to compilation, so a fresh one every build is a reason to recompile every project every time — and because a recompiled project yields a new reference assembly, that propagates to everything referencing it. Holding Debug to midnight costs the version nothing: AutoPackageVersion is already only precise to the day. Measured on four projects, an unchanged Debug build went from compiling 4 of 4 to 0 of 4, and a body edit in the root library from 4 of 4 to 1 of 4.
The cost is that a Debug assembly no longer records the time of day it was built: DirectoryBuildInfo.BuildRelease reads 00:00:00, which is the stamp reported faithfully rather than the instant the build happened. Release is unaffected and keeps a stamp per build.
TL;DR: For CI, or any build outside Debug where an exact guarantee matters more than convenience, use
-p:BuildTimestamp=...to fix the value explicitly instead of relying on evaluation-time capture. It overrides the Debug hold too, so a project that needs its own build time can set one for itself. Ready-to-use wrappers are available atBuild.ps1.template(Windows/PowerShell 7+) andBuild.sh.template(Linux/macOS/bash).
Diagnostics
| ID | Severity | Description |
|---|---|---|
BNAV00 |
Warning | Generator is installed but GenerateAutoVersionedAssemblyInfo is not set to true (single-line signal) |
BNAV01 |
Warning | Generator threw an unexpected exception |
BNAV02 |
Error | AssemblyCompany is not configured |
BNAV03 |
Error | AssemblyProduct is not configured |
BNAV04 |
Warning | Multi-line setup snippet emitted alongside BNAV00 — shows the required Directory.Build.props configuration |
BNAV05 |
Error | BuildTimestamp is set but is not a yyyyMMddHHmmss timestamp. Fix or remove it: the assembly would otherwise be stamped from the clock while MSBuild derives AutoVersion and AutoPackageVersion from the same bad value, so a package can carry a version that disagrees with the assembly inside it |
BNAV05 is the generator's half. Build.props refuses the same stamp independently, leaving AutoVersion and AutoPackageVersion empty rather than wrong and failing the build with an uncoded MSBuild error — because a project can use $(AutoPackageVersion) without enabling the generator, and that project is the one that packs the version. The two are kept apart, and neither message names the other, so the output says which half spoke.
Renamed after 2026.3.916. Up to and including that version the IDs were BAUTOVERSIONING00–04. The numbers are unchanged, so BAUTOVERSIONING04 is now BNAV04: rename any suppression or filter that names an old ID, or it stops matching — a NoWarn, an .editorconfig severity entry or a #pragma warning disable. TreatWarningsAsErrors is unaffected, since it does not name IDs. The prefix is BN plus the product's initials, shared across the Bennewitz.Ninja packages, and it will not change again.
Building Locally
Requires PowerShell 7+ and the .NET SDK.
./Pack.ps1
The script prompts for a version (defaults to CalVer YYYY.Q.MMDD) and produces .nupkg files in packages/Debug/ and packages/Release/.
License
MIT © 2026 Brian Bennewitz
Learn more about Target Frameworks and .NET Standard.
This package has no dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 2026.4.1202 | 48 | 12/3/2026 |
| 2026.4.1124 | 215 | 11/24/2026 |
| 2026.4.1116 | 623 | 11/16/2026 |
| 2026.4.1031 | 3,463 | 10/31/2026 |
| 2026.3.928 | 820 | 9/28/2026 |
| 2026.3.916 | 6,981 | 9/16/2026 |
| 2026.3.914 | 1,738 | 9/14/2026 |
| 2026.3.819 | 2,188 | 8/19/2026 |
| 2026.3.701 | 18,435 | 7/1/2026 |
| 2026.2.522 | 829 | 5/21/2026 |
| 2026.2.521 | 144 | 5/21/2026 |
| 2026.2.515 | 146 | 5/15/2026 |
| 2026.2.501 | 195 | 5/1/2026 |
| 2026.2.429 | 190 | 4/29/2026 |