You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Generates a JSON file on AfterTargets="PostBuildEvent"
Project B
References project A via ProjectReference to ensure correct build order
Consumes the JSON file inside AfterTargets="AfterResolveReferences" BeforeTargets="CoreCompile" and generates source code.
This seems to work. However it requires the TargetFramework of project A to be compatible with project B, even though project B does not rely on any of its code. There are a bunch of issues addressing this problem (See #2661 or #4795), but none of the workarounds described there seem to work.
I am generally wondering if there's some kind of "best practice" for dealing with this code generation scenario?
EDIT: I just found this blog post which uses <UndefineProperties>TargetFramework</UndefineProperties>. It seems to fix the incompatible TargetFramework issue, but it somehow breaks the AfterResolveReferences target. It's called multiple times now for some reason.
EDIT2: After some more testing I made the hack above work. The problem is using multiple TargetFrameworks in the referencing project. Targeting just one framework and including the referenced project with <SkipGetTargetFrameworkProperties>true</SkipGetTargetFrameworkProperties> seems to do the trick. So for anybody struggling with this in the future:
This calls a source generator after the referenced project is built and includes generated sources in the compilation. It also works if the referenced project targets a different TargetFramework. I still have no idea if that's the best way to do it though. So would be nice if someone more knowledgeable than me could chime in.
The text was updated successfully, but these errors were encountered:
lvde0
changed the title
Correct way to generate sources for an external ör
Correct way to generate sources for an external project?
May 23, 2024
lvde0
changed the title
Correct way to generate sources for an external project?
Correct way to generate sources for an external project (with incompatible TargetFramework)?
May 23, 2024
I have the following project structure:
Project A
AfterTargets="PostBuildEvent"
Project B
ProjectReference
to ensure correct build orderAfterTargets="AfterResolveReferences" BeforeTargets="CoreCompile"
and generates source code.This seems to work. However it requires the
TargetFramework
of project A to be compatible with project B, even though project B does not rely on any of its code. There are a bunch of issues addressing this problem (See #2661 or #4795), but none of the workarounds described there seem to work.I am generally wondering if there's some kind of "best practice" for dealing with this code generation scenario?
EDIT: I just found this blog post which uses
<UndefineProperties>TargetFramework</UndefineProperties>
. It seems to fix the incompatibleTargetFramework
issue, but it somehow breaks theAfterResolveReferences
target. It's called multiple times now for some reason.EDIT2: After some more testing I made the hack above work. The problem is using multiple
TargetFrameworks
in the referencing project. Targeting just one framework and including the referenced project with<SkipGetTargetFrameworkProperties>true</SkipGetTargetFrameworkProperties>
seems to do the trick. So for anybody struggling with this in the future:This calls a source generator after the referenced project is built and includes generated sources in the compilation. It also works if the referenced project targets a different
TargetFramework
. I still have no idea if that's the best way to do it though. So would be nice if someone more knowledgeable than me could chime in.The text was updated successfully, but these errors were encountered: