Skip to content

Instantly share code, notes, and snippets.

@aelij
Last active August 15, 2024 16:46
Show Gist options
  • Save aelij/b20271f4bd0ab1298e49068b388b54ae to your computer and use it in GitHub Desktop.
Save aelij/b20271f4bd0ab1298e49068b388b54ae to your computer and use it in GitHub Desktop.
Deterministic Builds in C#

Deterministic Builds in C#

Status

The C# compiler (Roslyn) supports deterministic builds since Visual Studio 2015. This means that compiling assemblies under the same conditions (permalink) would produce byte-for-byte equivalent binaries.

If you also intend on shipping non-portable (permalink) PDBs, you must also make sure that paths in the PDBs are absolute using path mapping. The recommended way would be to map the enlistment (repo) root to a fixed path, such as C:\.

For more information, see this blog post.

How To

Option 1: Use the .NET Foundation's DotNet.ReproducibleBuilds package.

Option 2:

  • Deterministic requires Visual Studio 2015 (Roslyn version 1.0).
  • PathMap requires Visual Studio 2015 Update 3 (Roslyn version 1.3).

In MSBuild (.csproj) - assuming you've defined the property EnlistmentRoot somewhere:

<Project>
  <PropertyGroup>
    <Deterministic>true</Deterministic>
    <PathMap>$(EnlistmentRoot)=C:\</PathMap>
  </PropertyGroup>
</Project>

In the compiler command-line:

> csc /deterministic /pathmap:C:\Source\MyRepo=C:\
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment