C#C
C#4y ago
5 replies
Binto86

Could not load file or assembly 'System.Collections.Imutable' when using source generators

I am trying source generators, and now i am getting warning
Generator failed to generate source. It will not contribute to the output and compilation errors may occur as a result. Exception was of type 'FileNotFoundException' with message 'Could not load file or assembly 'System.Collections.Immutable, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies.

i am pretty sure that this error has something to do with
context.AdditionalFiles
as it didn't occur when i didn't use them, this is my code:
public void Execute(GeneratorExecutionContext context)
{
  var mainMethod = context.Compilation.GetEntryPoint(context.CancellationToken);
  for (int i = 0; i < context.AdditionalFiles.Length; i++)
  {
    var file = context.AdditionalFiles[i];
    if (file.Path.EndsWith(".py"))
    {
      var ClassName = Path.GetFileNameWithoutExtension(file.Path);
      string source = $@"
// <auto-generated/>
{PythonGenerator.GenerateClass(file.Path, mainMethod.ContainingNamespace.Name)}
";


      // Add the source code to the compilation
      context.AddSource($"{ClassName}.g.cs", source);
        }
      }
    }
Was this page helpful?