C
C#3y ago
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.
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);
}
}
}
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);
}
}
}
3 Replies
333fred
333fred3y ago
Remember, #roslyn is a thing I suspect you have mismatched dependency versions between the version of VS you're using and the version of roslyn you're using
Binto86
Binto86OP3y ago
ok... and what can i do about it?
333fred
333fred3y ago
Line them up You need to use the appropriate version of roslyn for the version of VS you want to run on

Did you find this page helpful?