Skip to content

Development Environment Setup

Because source generators operate as compiler extensions, they require environment settings different from typical .NET projects. Without correctly configuring from the start—target framework constraints (netstandard2.0), Roslyn package dependencies, IDE source generator recognition settings, etc.—you will face build errors whose causes are difficult to identify in later stages.

This chapter configures the environment needed for source generator development from start to finish.

  1. .NET 10 SDK installation and verification
    • Configure the SDK version needed for building source generators
  2. IDE setup (Visual Studio 2022 / VS Code)
    • Configure IDE options for source generator debugging and viewing generated code
  3. Understanding required NuGet packages
    • The meaning of Microsoft.CodeAnalysis.CSharp and the PrivateAssets="all" setting

Terminal window
# Install using winget
winget install Microsoft.DotNet.SDK.10
# Or download from the official website
# https://dotnet.microsoft.com/download/dotnet/10.0
Terminal window
# Install using Homebrew
brew install --cask dotnet-sdk
# Or download from the official website
Terminal window
dotnet --version
# Example output: 10.0.100
dotnet --list-sdks
# Example output: 10.0.100 [C:\Program Files\dotnet\sdk]

Minimum version: 17.12 or higher

Required Workloads
==================
1. .NET desktop development
2. ASP.NET and web development (optional)
Verify Installation
===================
Visual Studio Installer → Modify → Check workloads

Source generator debugging settings:

Tools → Options → Text Editor → C# → Advanced
☑ Enable "Show files generated by source generators"

Required extensions:

Extension Installation
======================
1. C# Dev Kit (ms-dotnettools.csdevkit)
- C# language support, IntelliSense, debugging
2. .NET Install Tool (ms-dotnettools.vscode-dotnet-runtime)
- .NET SDK version management
3. EditorConfig for VS Code (editorconfig.editorconfig)
- Code style consistency

Recommended settings.json:

{
"dotnet.defaultSolution": "Functorium.sln",
"omnisharp.enableRoslynAnalyzers": true,
"omnisharp.enableEditorConfigSupport": true,
"csharp.semanticHighlighting.enabled": true
}

Terminal window
# Create source generator project
dotnet new classlib -n MySourceGenerator -f netstandard2.0
# Create test project
dotnet new xunit -n MySourceGenerator.Tests -f net10.0

Core packages needed for the source generator project:

<!-- MySourceGenerator.csproj -->
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<!-- Source generators must target netstandard2.0 -->
<TargetFramework>netstandard2.0</TargetFramework>
<LangVersion>latest</LangVersion>
<!-- Mark as source generator component -->
<IsRoslynComponent>true</IsRoslynComponent>
<!-- Required when packaged as an analyzer -->
<EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules>
<!-- Enable nullable reference types -->
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<!-- Roslyn code analysis API -->
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.12.0" PrivateAssets="all" />
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.11.0" PrivateAssets="all" />
</ItemGroup>
</Project>
PackagePurpose
Microsoft.CodeAnalysis.CSharpRoslyn C# compiler API (Syntax, Semantic)
Microsoft.CodeAnalysis.AnalyzersAnalyzer development rule validation

Source generators operate as compiler extensions, so they must run in various .NET environments:

Source Generator Execution Environments
=======================================
1. Visual Studio (Windows)
- Based on .NET Framework 4.7.2
2. VS Code + OmniSharp
- Based on .NET Core
3. dotnet CLI
- Based on .NET 8/10
4. JetBrains Rider
- Own runtime
→ netstandard2.0 is required to work in all environments

Note: The source generator project itself targets netstandard2.0, but the generated code can use net10.0 syntax.

// Source generator (runs in netstandard2.0)
[Generator]
public class MyGenerator : IIncrementalGenerator
{
public void Initialize(...)
{
// Generated code can use C# 13 syntax
var code = """
public class Generated
{
// C# 13 collection expressions
public int[] Numbers => [1, 2, 3];
}
""";
}
}

Special reference settings are needed to use a source generator in other projects:

<!-- Project using the source generator -->
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<!-- Source generator project reference -->
<ProjectReference Include="..\MySourceGenerator\MySourceGenerator.csproj"
OutputItemType="Analyzer"
ReferenceOutputAssembly="false" />
</ItemGroup>
</Project>
PropertyDescription
OutputItemType="Analyzer"Recognized as an analyzer/generator
ReferenceOutputAssembly="false"Excluded from runtime references (compile-time only)

MySolution/
├── MySolution.sln
├── src/
│ ├── MySourceGenerator/ # Source generator project
│ │ ├── MySourceGenerator.csproj # netstandard2.0
│ │ └── MyGenerator.cs
│ │
│ └── MyApp/ # Project using the source generator
│ ├── MyApp.csproj # net10.0
│ └── Program.cs
└── tests/
└── MySourceGenerator.Tests/ # Test project
├── MySourceGenerator.Tests.csproj
└── GeneratorTests.cs

A summary of the core configuration elements for source generator development environments.

ItemValue
.NET SDK10.0 or higher
IDEVisual Studio 2022 (17.12+) or VS Code
Generator target frameworknetstandard2.0 (required)
Core NuGetMicrosoft.CodeAnalysis.CSharp 4.12.0+
Project referenceOutputItemType=“Analyzer”

Q1: Why must source generator projects target netstandard2.0?

Section titled “Q1: Why must source generator projects target netstandard2.0?”

A: The Roslyn compiler runs on various runtimes including Visual Studio, VS Code, dotnet CLI, and JetBrains Rider. The only target commonly supported by all of them is netstandard2.0, so this target is required for source generators to work in all environments.

Q2: Why is PrivateAssets="all" specified for the Microsoft.CodeAnalysis.CSharp package?

Section titled “Q2: Why is PrivateAssets="all" specified for the Microsoft.CodeAnalysis.CSharp package?”

A: When a source generator is distributed as a NuGet package, if the Roslyn package is transitively included, it may conflict with the consumer project’s Roslyn version. PrivateAssets="all" prevents this dependency from being exposed externally.

Q3: The generator project is netstandard2.0, but can the generated code use the latest C# syntax?

Section titled “Q3: The generator project is netstandard2.0, but can the generated code use the latest C# syntax?”

A: Only the generator itself needs to be netstandard2.0 compatible. The code the generator outputs is just a string, so it can use any C# syntax that matches the consumer project’s LangVersion.


With the development environment ready, the next chapter examines the csproj settings and data model structure of source generator projects in detail.

02. Project Structure