How to read build variables in Source Generators I used GitHub copilot to help me create a source generator that reads the schema of a database and generates C# ADO.net code that can call SQL Stored Procedures, Views and Functions in that database ( https://github.com/TheJuanitoLearnsShow/Puppy.Ado.SourceGenerator ). The connection string to the database is set on a build variable in the consuming csproj file. The problem is that copilot missed the step described in Microsoft’s Source Generator Cookbook . The missing step is to add a CompilerVisibleProperty element to an ItemGroup for each of the build variables you want the source generator to use. For example, my source generator uses two build variables: DbGen_EnableLiveSchema and DbGen_ConnectionString, so they are defined in the PropertyGroup element in the consuming csproj file: <DbGen_EnableLiveSchema>true</DbGen_EnableLiveSchema> <DbGen_ConnectionString>Data Source=.\sqlExpress;Database=AutomatedTESTS_AdoGen...
A blog about real-world software engineering and development problems and solutions.