Skip to content

[Source Generator] Source Generator works with dotnet cli but fails with TypeLoadException in VS #108164

@LittleLittleCloud

Description

@LittleLittleCloud

Description

The source generator can successfully generate source code when I compile using dotnet cli. It also successfully generates source code in VS UI when I expand the analyzer node. But it raises a TypeLoadException when I compile and run my code in VS by hitting control + F5.

Here is the link to the source code of source generator: https://github.com/tryAGI/AutoSDK/tree/main/src/libs/AutoSDK.SourceGenerators

Here is the dotnet --list-sdks

8.0.400 [C:\Program Files\dotnet\sdk]
9.0.100-preview.7.24407.12 [C:\Program Files\dotnet\sdk]

Here is the error log in VS:

Build started at 9:24 AM...
1>------ Build started: Project: SourceGen, Configuration: Debug Any CPU ------
1>CSC : warning CS8784: Generator 'SdkGenerator' failed to initialize. It will not contribute to the output and compilation errors may occur as a result. Exception was of type 'TypeLoadException' with message 'Could not load type 'AutoSDK.Models.ModelData' from assembly 'AutoSDK, Version=0.0.0.0, Culture=neutral, PublicKeyToken=603f13207e65c17b'.'.
1>SourceGen -> C:\Users\xiaoyuz\source\repos\SourceGen\bin\Debug\net8.0\SourceGen.dll
1>Done building project "SourceGen.csproj".

Here is the screenshot of which VS I am using:
image

Finally, the link to a minimal reproducable example for this bug.

Reproduction Steps

See above

Expected behavior

Source generator successfully generate source in VS

Actual behavior

It raise a TypeLoadException

Regression?

No response

Known Workarounds

No response

Configuration

No response

Other information

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    Status

    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions