Pulumi Error: '.NET build failed' — C# Program Compile Fix
Fix Pulumi '.NET build failed' running a C# program: missing SDK, NuGet restore failures, csproj target framework, and compile errors surfaced during preview.
- #pulumi
- #iac
- #troubleshooting
- #errors
Stuck on this Pulumi error? Get the free incident triage checklist
A one-page PDF — the exact steps to isolate, fix, and verify a production error like this one. No spam, unsubscribe anytime.
Exact Error Message
error: failed to load language plugin dotnet: could not build project:
Program.cs(31,20): error CS0246: The type or namespace name 'Bucket'
could not be found (are you missing a using directive or an assembly reference?)
Build FAILED.
error: .NET build failed
You may also see error NETSDK1045: The current .NET SDK does not support targeting .NET x.0, error NU1101: Unable to find package Pulumi.Aws, or MSB1003: Specify a project or solution file. All of these are the dotnet build step failing before Pulumi can run your program.
What It Means
For C#/.NET projects, Pulumi’s dotnet language host compiles your program with the dotnet CLI before evaluating any resources. When that build fails — a compile error, a NuGet restore failure, a missing SDK, or a bad target framework — Pulumi has no runnable program and stops with .NET build failed, echoing the MSBuild/Roslyn diagnostic.
This is a standard .NET build error surfaced through Pulumi. The remedy is whatever dotnet build needs to succeed: the right SDK installed, packages restored, and the code compiling cleanly.
Common Causes
- A missing
usingdirective or assembly reference (CS0246) for a Pulumi type. - The installed .NET SDK does not support the
TargetFrameworkin the.csproj(NETSDK1045). - NuGet restore cannot find or reach the
Pulumi.*packages (NU1101, offline feed, proxy). - A
Pulumi.yamlmainpointing at a directory with no.csproj, or multiple project files (MSB1003). - Version drift between
PulumiandPulumi.Aws/provider packages producing type mismatches. - An ordinary C# compile error (
CS xxxx) in the program.
Diagnostic Commands
Reproduce the build directly for the clearest diagnostics:
dotnet build -v normal
Confirm which SDKs are installed and the target framework in use:
dotnet --list-sdks
cat *.csproj
Restore packages explicitly to isolate NuGet problems:
dotnet restore
Check the Pulumi project runtime and entry point:
cat Pulumi.yaml
Step-by-Step Resolution
-
Read the error code.
CSxxxxis a compile error,NETSDKxxxxis an SDK/target mismatch, andNUxxxxis a package-restore failure — each has a different fix. -
For a
CS0246missing-type error, add the correctusingand make sure the provider package is referenced in the.csproj:
using Pulumi;
using Pulumi.Aws.S3;
<ItemGroup>
<PackageReference Include="Pulumi" Version="3.*" />
<PackageReference Include="Pulumi.Aws" Version="6.*" />
</ItemGroup>
- For
NETSDK1045, align theTargetFrameworkwith an installed SDK fromdotnet --list-sdks(or install the newer SDK):
<TargetFramework>net8.0</TargetFramework>
- For
NU1101restore failures, restore against the public feed and clear a corrupt cache if needed:
dotnet nuget locals all --clear
dotnet restore --source https://api.nuget.org/v3/index.json
- If
MSB1003appears, pointPulumi.yamlat the folder containing exactly one.csproj:
name: my-app
runtime: dotnet
main: ./MyApp
- Confirm a clean build, then run the preview:
dotnet build && pulumi preview
When a CS diagnostic is cryptic, paste the offending lines and the error into a prompt from the Pulumi prompt library to get the missing reference or type usage explained.
Prevention
- Pin
Pulumiand provider packages to compatible major versions in the.csprojand upgrade them together. - Run
dotnet buildin CI beforepulumi previewso compile and restore errors fail early with full context. - Keep the
TargetFrameworkon a framework your CI and local SDKs both install; checkdotnet --list-sdks. - Commit a
nuget.configwhen using a private feed so restore is reproducible across machines. - Keep one
.csprojper Pulumi project directory (or setmainexplicitly) to avoid ambiguous-project errors.
Related Errors
failed to load language plugin dotnet— the dotnet language host itself could not start.error: Running program '...' failed with an unhandled exception— a runtime error after a successful build.NU1102: Unable to find package ... with version— a restore version-constraint problem.no Pulumi.yaml project file found— you are running outside the project directory.
Frequently Asked Questions
Why does Pulumi build my C# project? The dotnet language host compiles your program with dotnet build before evaluating resources, so any build failure stops Pulumi before a plan exists.
How do I fix NETSDK1045? Either lower the TargetFramework to a version your installed SDK supports or install the newer .NET SDK; dotnet --list-sdks shows what you have.
Restore fails only in CI — why? CI usually starts with an empty NuGet cache and may lack access to a private feed; add a nuget.config and run dotnet restore explicitly.
Can I precompile to speed up preview? Yes, but the build must still succeed; fixing the underlying compile or restore error is what clears .NET build failed. For more .NET setup guidance, see the Pulumi guides.
Fixed it? Get 500 Pulumi & DevOps AI prompts — free
500 battle-tested, copy-paste AI prompts engineered by a senior systems engineer — every one with fill-in placeholders and safety/back-out notes. Drop your email and it's yours.
- 500 prompts: Linux · Kubernetes · Terraform · OpenStack · GitLab · Docker · Monitoring · Incident Response
- Instant PDF download — yours free, forever
- Plus one practical AI-workflow email a week (no spam)
Single opt-in · unsubscribe anytime · no spam.
Did this fix your issue?
Get 500 Battle-Tested DevOps AI Prompts — Free
500 battle-tested, copy-paste AI prompts engineered by a senior systems engineer — every one with fill-in placeholders and safety/back-out notes. Drop your email and it's yours.
- 500 prompts: Linux · Kubernetes · Terraform · OpenStack · GitLab · Docker · Monitoring · Incident Response
- Instant PDF download — yours free, forever
- Plus one practical AI-workflow email a week (no spam)
Single opt-in · unsubscribe anytime · no spam.