Development Environment Setup
Overview
Section titled “Overview”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.
Learning Objectives
Section titled “Learning Objectives”Core Learning Objectives
Section titled “Core Learning Objectives”- .NET 10 SDK installation and verification
- Configure the SDK version needed for building source generators
- IDE setup (Visual Studio 2022 / VS Code)
- Configure IDE options for source generator debugging and viewing generated code
- Understanding required NuGet packages
- The meaning of
Microsoft.CodeAnalysis.CSharpand thePrivateAssets="all"setting
- The meaning of
.NET 10 SDK Installation
Section titled “.NET 10 SDK Installation”Windows
Section titled “Windows”# Install using wingetwinget install Microsoft.DotNet.SDK.10
# Or download from the official website# https://dotnet.microsoft.com/download/dotnet/10.0# Install using Homebrewbrew install --cask dotnet-sdk
# Or download from the official websiteVerify Installation
Section titled “Verify Installation”dotnet --version# Example output: 10.0.100
dotnet --list-sdks# Example output: 10.0.100 [C:\Program Files\dotnet\sdk]IDE Setup
Section titled “IDE Setup”Visual Studio 2022 (Recommended)
Section titled “Visual Studio 2022 (Recommended)”Minimum version: 17.12 or higher
Required Workloads==================
1. .NET desktop development2. ASP.NET and web development (optional)
Verify Installation===================Visual Studio Installer → Modify → Check workloadsSource generator debugging settings:
Tools → Options → Text Editor → C# → Advanced
☑ Enable "Show files generated by source generators"VS Code
Section titled “VS Code”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 consistencyRecommended settings.json:
{ "dotnet.defaultSolution": "Functorium.sln", "omnisharp.enableRoslynAnalyzers": true, "omnisharp.enableEditorConfigSupport": true, "csharp.semanticHighlighting.enabled": true}Creating a Source Generator Project
Section titled “Creating a Source Generator Project”1. Create a Class Library Project
Section titled “1. Create a Class Library Project”# Create source generator projectdotnet new classlib -n MySourceGenerator -f netstandard2.0
# Create test projectdotnet new xunit -n MySourceGenerator.Tests -f net10.02. Install Required NuGet Packages
Section titled “2. Install Required NuGet Packages”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>3. Package Descriptions
Section titled “3. Package Descriptions”| Package | Purpose |
|---|---|
Microsoft.CodeAnalysis.CSharp | Roslyn C# compiler API (Syntax, Semantic) |
Microsoft.CodeAnalysis.Analyzers | Analyzer development rule validation |
Why netstandard2.0?
Section titled “Why netstandard2.0?”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 environmentsNote: 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]; } """; }}Project Reference Settings
Section titled “Project Reference Settings”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>| Property | Description |
|---|---|
OutputItemType="Analyzer" | Recognized as an analyzer/generator |
ReferenceOutputAssembly="false" | Excluded from runtime references (compile-time only) |
Example Project Structure
Section titled “Example Project Structure”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.csSummary at a Glance
Section titled “Summary at a Glance”A summary of the core configuration elements for source generator development environments.
| Item | Value |
|---|---|
| .NET SDK | 10.0 or higher |
| IDE | Visual Studio 2022 (17.12+) or VS Code |
| Generator target framework | netstandard2.0 (required) |
| Core NuGet | Microsoft.CodeAnalysis.CSharp 4.12.0+ |
| Project reference | OutputItemType=“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.