Additional Files for Roslyn Analyzers and Source Generators Not Working Correctly

I followed the documentation, but it didn’t work. I tested this in both Unity 6000.0.49f1 and 6000.0.53f1.

My Generator Code:

Log(context, $"Found {context.AdditionalFiles.Length} additional files.");
var i = 0;
foreach (var additionalFile in context.AdditionalFiles)
{
    Log(context, $"[{++i}]{additionalFile.Path}");
    var additionalFileContent = additionalFile.GetText(context.CancellationToken)?.ToString();
    Log(context, additionalFileContent);
    Log(context, "====================");
}
string requiredSuffix = $".additionalfile";
var stuFiles = context.AdditionalFiles.Where(f => f.Path.Contains(requiredSuffix)).ToList();
Log(context, $"Found {stuFiles.Count} additional files to process with suffix '{requiredSuffix}'.");
if (stuFiles.Count == 0)
{
    Log(context,
        "No additional files found. If this is unexpected, check your file naming convention, e.g., 'MyNode.[Analyzer Name].NodeGenerator.additionalfile'.");
    Log(context, "NodeGenerator finished. No work to do.");
    return;
}

Output Log:

error SSG_INFO: [MyGenerator] NodeGenerator execution started.
error SSG_INFO: [MyGenerator] Targeting assembly '[AssemblyName]' with root namespace '[AssemblyName]'.
error SSG_INFO: [MyGenerator] Found 1 additional files.
error SSG_INFO: [MyGenerator] [1]*\\Library/Bee/artifacts/1903b0aEDbg.dag/[AssemblyName].UnityAdditionalFile.txt
error SSG_INFO: [MyGenerator] [MyAssemblyRootPath]
error SSG_INFO: [MyGenerator] ====================
error SSG_INFO: [MyGenerator] Found 0 additional files to process with suffix '.additionalfile'.
error SSG_INFO: [MyGenerator] No additional files found. If this is unexpected, check your file naming convention, e.g., 'MyNode.[Analyzer Name].additionalfile'.
error SSG_INFO: [MyGenerator] NodeGenerator finished. No work to do.

Observations from the Log:

  1. My generator is not receiving a list of .additionalfile files. It only receives one file.
  2. The file path is:Library/Bee/artifacts/191cb0aEDbg.dag/[AssemblyName].UnityAdditionalFile.txt This reveals that Unity’s build system (Bee) is not passing .stu/.additionalfile files directly to the generator. Instead, it creates a temporary manifest file named [AssemblyName].UnityAdditionalFile.txt.
  3. The content of the manifest file appears to be a list of paths (though in this case, it only contains one path—the root path, which is odd but hints at the underlying mechanism).

Questions:

  • Has anyone else tried this? Did it work for you?
  • Is my Unity version incorrect, or is this a bug?
  • Are there alternative solutions to successfully implement AdditionalFiles? I need support for multiple .additionalfile files.

Would appreciate any insights or workarounds!

Check the results of this method:

Also worth highlighting are the version requirements, they are all too often ignored and that may cause issues:

  1. In Visual Studio, create a C# class library project that targets .NET Standard 2.0 and name the project ExampleSourceGenerator.
  2. Install the Microsoft.CodeAnalysis.Csharp NuGet package for the project. Your source generator must use Microsoft.CodeAnalysis.Csharp 4.3 to work with Unity.

Thanks for reply. I also learned a lot about Roslyn from your post.

Finally figured out the issue! The problem was with my .additionalfile naming. I mistakenly named it as FileName.[Analyzer Name].additianfile.stu.

​Why did this happen?​
Initially, I named it simply FileName.stu. Later, after checking the documentation, I tried renaming it ​​in Unity​​ to match the required format. However, I overlooked a critical detail: Unity’s Project window ​​doesn’t display the .stu suffix​​—it only shows FileName. So, when I renamed it to FileName.[Analyzer Name].additianfile, the actual filename became FileName.[Analyzer Name].additianfile.stu. A silly oversight on my part!

After correcting the naming, it worked perfectly. Thanks to Unity for providing the .additionalfile mechanism—it’s a significant improvement over the old csc.rsp approach.

​Question​​: Does csc.rsp even support individual files? If anyone knows, please clarify!