Deterministic Builds & CI Builds
A build is deterministic when the same inputs always produce byte-for-byte identical outputs. That property lets you verify that a released binary really came from a given commit, lets build caches reuse outputs safely, and turns "did anything change?" into a checksum comparison. The C# compiler has supported it for years and the SDK turns it on by default, but there's one input people forget: the folder you build in.
Same folder, same bytes
mkdir lib && cd lib
cat > Lib.csproj <<'XML'
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
</PropertyGroup>
</Project>
XML
echo 'namespace Ada; public static class Calc { public static int Twice(int x) => 2 * x; }' > Calc.cs
build() { rm -rf bin obj; dotnet build -c Release -nologo -v:q "$@" > /dev/null; sha256sum bin/Release/net10.0/Lib.dll | cut -c1-16; }
dotnet msbuild -getProperty:Deterministic
build
build
echo "--- with Deterministic=false"
build -p:Deterministic=false
build -p:Deterministic=falsetrue
d324ddec77f076eb
d324ddec77f076eb
--- with Deterministic=false
200f054eb2b11e75
c668aa3cf19a1a92
With Deterministic=true (the default), two clean builds give the same hash. With it off, the compiler stamps a fresh module version ID (MVID) and a timestamp into each build, so no two builds match. Never turn it off.
Another folder, other bytes
Now copy the same project to a different directory and build there:
mkdir -p a b/deeper
for d in a b/deeper; do
cat > $d/Lib.csproj <<'XML'
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
</PropertyGroup>
</Project>
XML
echo 'namespace Ada; public static class Calc { public static int Twice(int x) => 2 * x; }' > $d/Calc.cs
done
for d in a b/deeper; do
(cd $d && dotnet build -c Release -nologo -v:q > /dev/null
echo "$d: $(sha256sum bin/Release/net10.0/Lib.dll | cut -c1-16) pdb path: $(grep -a -o '/[[:alnum:]/._]*Lib\.pdb' bin/Release/net10.0/Lib.dll)")
donea: 0df5e0eb6399d921 pdb path: /home/learner/a/obj/Release/net10.0/Lib.pdb
b/deeper: eff2e2f60c4ad674 pdb path: /home/learner/b/deeper/obj/Release/net10.0/Lib.pdb
The DLL records the full path of its PDB (so debuggers can find symbols), and the PDB records the full path of every source file. /home/learner/a/… and /home/learner/b/deeper/… differ, so the binaries differ. On developer machines that's harmless. In CI it means two agents with different workspace paths can never produce the same binary, and every binary leaks the build machine's directory layout.
ContinuousIntegrationBuild: map paths to /_/
Setting ContinuousIntegrationBuild=true tells the SDK this is an official build. It then maps every source root to a fixed prefix (/_/) with the compiler's -pathmap option. The source roots come from source control: the SDK includes the Microsoft.Build.Tasks.Git support (part of SourceLink) and asks git where the repository starts. So the projects must be in a git repository:
mkdir -p a b/deeper
for d in a b/deeper; do
cat > $d/Lib.csproj <<'XML'
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
</PropertyGroup>
</Project>
XML
echo 'namespace Ada; public static class Calc { public static int Twice(int x) => 2 * x; }' > $d/Calc.cs
(cd $d && git init -q && git add -A && git commit -qm "lib")
done
for d in a b/deeper; do
(cd $d && dotnet build -c Release -nologo -v:q -p:ContinuousIntegrationBuild=true > /dev/null
echo "$d: $(sha256sum bin/Release/net10.0/Lib.dll | cut -c1-16) pdb path: $(grep -a -o '/[[:alnum:]/._]*Lib\.pdb' bin/Release/net10.0/Lib.dll)")
done
cd a && dotnet msbuild -t:_SetPathMapFromSourceRoots -getProperty:PathMap -p:ContinuousIntegrationBuild=truea: 6eb5d3c1e33363fd pdb path: /_/obj/Release/net10.0/Lib.pdb
b/deeper: 6eb5d3c1e33363fd pdb path: /_/obj/Release/net10.0/Lib.pdb
/home/learner/.nuget/packages/=/_1/,/home/learner/a/=/_/,
Both directories now produce identical DLLs, and the embedded PDB path starts with /_/. PathMap shows the mapping MSBuild computed (it's set by an internal SDK target, _SetPathMapFromSourceRoots, so we ask for that target to run before reading the property; at evaluation time PathMap is still empty): the repository root becomes /_/ and the NuGet package folder /_1/, because packages can contribute source files too.
Note: Without a git repository there are no source roots, and
ContinuousIntegrationBuild=truehas nothing to map: the paths stay absolute. If you build from a source tarball, either keep the.gitfolder or defineSourceRootitems yourself.
Don't set ContinuousIntegrationBuild for local builds: with paths mapped, the debugger can't find your sources on disk without SourceLink. The usual pattern goes in Directory.Build.props and keys on the variables CI systems set:
<PropertyGroup Condition="'$(GITHUB_ACTIONS)' == 'true' or '$(TF_BUILD)' == 'true' or '$(CI)' == 'true'">
<ContinuousIntegrationBuild>true</ContinuousIntegrationBuild>
</PropertyGroup>(GITHUB_ACTIONS is set by GitHub Actions, TF_BUILD by Azure Pipelines, CI by GitLab and most others.)
What path mapping does
The compiler applies -pathmap:from=to to every path it writes into the PDB and DLL. A model:
def apply_pathmap(path, pathmap):
for entry in pathmap.split(","):
if not entry:
continue
src, dst = entry.split("=")
if path.startswith(src):
return dst + path[len(src):]
return path
pathmap = "/home/learner/.nuget/packages/=/_1/,/home/learner/a/=/_/,"
for p in ["/home/learner/a/obj/Release/net10.0/Lib.pdb",
"/home/learner/a/Calc.cs",
"/home/learner/.nuget/packages/newtonsoft.json/13.0.4/src/Shim.cs",
"/tmp/generated.cs"]:
print(f"{p:64} -> {apply_pathmap(p, pathmap)}")/home/learner/a/obj/Release/net10.0/Lib.pdb -> /_/obj/Release/net10.0/Lib.pdb
/home/learner/a/Calc.cs -> /_/Calc.cs
/home/learner/.nuget/packages/newtonsoft.json/13.0.4/src/Shim.cs -> /_1/newtonsoft.json/13.0.4/src/Shim.cs
/tmp/generated.cs -> /tmp/generated.cs
Files outside every mapped root (/tmp/generated.cs) keep their real path, so generated sources belong under obj/ inside the repository, where the mapping covers them.
Beyond the compiler
Determinism of DLLs is the foundation, not the whole story. Package archives (.nupkg) are a separate question, covered in the CI field notes. And the inputs must be pinned too: the same SDK version (global.json), the same package versions (lock files), the same source.
Test yourself
Two clean Release builds of the same commit in /src/a and /src/b produce different DLLs. Why, with Deterministic=true?
- Deterministic is ignored in Release
- The full PDB and source paths are embedded and differ between the folders
- The compiler adds random numbers
- NuGet restored different packages
What does ContinuousIntegrationBuild=true need to map paths?
- Nothing
- Source roots, normally discovered from the git repository
- An internet connection
- A Directory.Packages.props
Why not enable ContinuousIntegrationBuild for local developer builds?
- It makes builds slower
- Mapped paths stop the debugger from finding local source files without SourceLink
- It uploads the build
- It disables incremental builds