dotnet / roslyn-sdk

Roslyn-SDK templates and Syntax Visualizer
MIT License
515 stars 256 forks source link

Testing Generators with Exposed MSBuild Properties #1188

Open dansiegel opened 1 month ago

dansiegel commented 1 month ago

Description

Some Generators require access to MSBuild properties to have additional context to be able to properly generate. How can I inject values into the Generator Test so that the generator can access properties that would normally be provided from MSBuild:

context.AnalyzerConfigOptions.GlobalOptions.TryGetValue("build_property.SomeProperty", out var value);
Youssef1313 commented 1 month ago

You can do it this way:

TestState.AnalyzerConfigFiles.Add(("/.globalconfig", """
    is_global = true
    build_property.SomeProperty = value
    """));
dansiegel commented 1 month ago

thanks @Youssef1313 if that's in the docs I couldn't find it. That seems to have done the trick!