Skip to content

Question: how could I compile code with source generator #67801

Closed Answered by thomasclaudiushuber
WeihanLi asked this question in Q&A
Discussion options

You must be logged in to vote

Hi @WeihanLi ,

OK, got it. I looked at your project. You're creating the raw compilation object. On that one, the source generator did not run. That's the reason why you're getting the error.

You need to kick it off with a GeneratorDriver like I do here in this file: https://github.com/thomasclaudiushuber/mvvmgen/blob/main/src/MvvmGen.SourceGenerators.Tests/ViewModelGeneratorTests/Base/ViewModelGeneratorTestBase.cs

Then you'll get another Compilation object that includes the generated sources.

It works like this for your project:

 var inputCompilation = CSharpCompilation.Create(
            assemblyName,
            syntaxTrees: new[] { syntaxTree },
            references: references,
  …

Replies: 4 comments

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by WeihanLi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
Area-Compilers untriaged Issues and PRs which have not yet been triaged by a lead
3 participants
Converted from issue

This discussion was converted from issue #67296 on April 13, 2023 17:26.