Additional files for Roslyn analyzers and source generators
Define additional text files for Roslyn analyzers or source generators to use.
Read time 3 minutesLast updated 13 days ago
You can add additional files to your project for a Roslyn analyzer or source generator to consume. The Unity Editor recognizes files as additional files for Roslyn analyzers if they have the .additionalfile extension and are somewhere in the folder or one of its subfolders.
AssetsNaming additional files
To be passed to the compilation pipeline, files must be named according to the format . Files that lack the component are imported but not passed to the compilation pipeline. The component is case-sensitive and must match the name of the analyzer the additional file targets. The component must not contain a period () character.
Filename.[Analyzer Name].additionalfile[Analyzer Name][Analyzer Name]Filename.Additional file filtering
Each compiled assembly receives a list of additional files based on the analyzers running for that assembly definition, filtered by the . For example, consider a project with the following assemblies and analyzers:
[Analyzer Name]- Assembly1 uses an analyzer named
Analyzer1 - Assembly2 uses an analyzer named
Analyzer2 - Assembly3 uses both and
Analyzer1Analyzer2
If the project contains four additional files named , , , and , then Unity passes the following list of additional files with the respective assemblies:
FileA.Analyzer1.additionalfileFileB.Analyzer2.additionalfileFileC.additionalfileFileD.Analyzer3.additionalfile- Assembly1:
FileA.Analyzer1.additionalfile - Assembly2:
FileB.Analyzer2.additionalfile - Assembly3: ,
FileA.Analyzer1.additionalfileFileB.Analyzer2.additionalfile
Since has no component and references an analyzer that isn't present in the project, Unity doesn't pass either one to the compilation pipeline.
FileC[Analyzer Name]FileDRetrieving available additional files from code
Analyzers can retrieve the full list of additional files included in the compiled assembly from the analyzer context. This list comprises all additional files included in the assembly, not just the ones matching the name of the current analyzer. The following example demonstrates this:
using System.IO;using System.Linq;using Microsoft.CodeAnalysis;using Microsoft.CodeAnalysis.CSharp.Syntax;namespace SourceGeneratorTest1;[Generator]public class SG2 : ISourceGenerator{ public void Execute(GeneratorExecutionContext context) { var pathOfFileWithTypeName = context.AdditionalFiles.FirstOrDefault(file => file.Path.Contains("GenerateType.SourceGenerator.Test.2.additionalfile")); if (pathOfFileWithTypeName == null) { // no additional file found, do not generate a type. return; } var @namespace = context.Compilation.SyntaxTrees.First().GetRoot().DescendantNodes().OfType<NamespaceDeclarationSyntax>().FirstOrDefault()?.Name?.ToString() ?? "NoNamespace"; // an additional file has been passed; read the type name from the file. string generatedTypeName = File.ReadAllText(pathOfFileWithTypeName.Path); context.AddSource( "SG2.generated.cs", $$""" namespace {{@namespace}} { public partial class {{generatedTypeName}} { } } """); } public void Initialize(GeneratorInitializationContext context) { }}
In the previous filtering example, and can both retrieve additional files for Assembly3 that are named after either of them. Each analyzer is responsible for checking if there is any data it can use in the additional files.
Analyzer1Analyzer2You can also retrieve the list of additional files included for a given assembly by using the ScriptCompilerOptions.RoslynAdditionalFilePaths property in Editor code.