Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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.

However, things get a bit more complicated when Windows (non-portable (permalink)) PDBs are involved. Currently, PDBs themselves are always non-deterministic. Additionally, the compiler adds the full path of the PDB to the assembly. This latter limitation might be fully addressed in a future Visual Studio 2017 Update, but currently (starting with Visual Studio 2015 Update 3), there's a compiler feature flag (pdb-path-determinism) that makes this path relative (temporary workaround).

There's also an option to map paths if you build from different enlistment paths (important if you use [CallerFilePath] or #line directives).

For more information, see this blog post and the umbrella issue.

How To

  • Deterministic requires Visual Studio 2015 (Roslyn version 1.0).
  • pdb-path-determinism requires Visual Studio 2015 Update 3 (Roslyn version 1.3).

In MSBuild (.csproj):

<Project>
  <PropertyGroup>
    <Deterministic>true</Deterministic>
    <Features>pdb-path-determinism</Features>
  </PropertyGroup>
</Project>

In the compiler command-line:

> csc /deterministic /features:pdb-path-determinism
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment