C# "CS0246: type or namespace name could not be found" in CI
The compiler saw a type or namespace name it cannot resolve. CS0246 almost always means a missing reference (package or project) or a missing using - not a syntax problem. In CI it frequently comes from a package that restored locally but not on the runner.
What this error means
Build fails with CS0246 naming the unresolved type/namespace, often hinting "are you missing a using directive or an assembly reference?". Deterministic for a given source + reference set.
Program.cs(7,9): error CS0246: The type or namespace name 'JsonSerializer' could not be
found (are you missing a using directive or an assembly reference?)Common causes
A package or project reference is missing
The type lives in a NuGet package or sibling project that is not referenced, so the compiler cannot find it.
A using directive is missing
The type exists in a referenced assembly but the namespace is not imported, so the unqualified name does not resolve.
How to fix it
Add the missing reference
- Identify which package or project defines the type.
- Add the
PackageReferenceorProjectReference. - Restore and rebuild.
<ItemGroup>
<PackageReference Include="System.Text.Json" Version="8.0.4" />
</ItemGroup>Add the using directive
- Add the namespace import for the type at the top of the file (or via global usings).
- Rebuild to confirm resolution.
using System.Text.Json;How to prevent it
- Ensure restore runs and succeeds before build in CI.
- Use global usings for ubiquitous namespaces to reduce per-file gaps.
- Commit a lock file so the same packages resolve on every runner.