Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correct way to generate sources for an external project (with incompatible TargetFramework)? #10169

Open
lvde0 opened this issue May 23, 2024 · 0 comments
Assignees
Labels

Comments

@lvde0
Copy link

lvde0 commented May 23, 2024

I have the following project structure:

Project A

  • 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:

<PropertyGroup>
  <TargetFramework>netstandard2.1</TargetFramework>
</PropertyGroup>


...

<Target Name="GenerateSources" AfterTargets="AfterResolveReferences"  BeforeTargets="CoreCompile" Condition="'$(DesignTimeBuild)' != 'true'">

  <Exec Command="..." />

  <ItemGroup>
	  <Compile Include="Generated\**\*.cs" Exclude="@(Compile)" />
  </ItemGroup>
</Target>

<ItemGroup>
  <ProjectReference Include="..\ProjectToGenerateSourcesFor.csproj">
	  <ReferenceOutputAssembly>false</ReferenceOutputAssembly>
	  <SkipGetTargetFrameworkProperties>true</SkipGetTargetFrameworkProperties>
  </ProjectReference>
</ItemGroup>

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.

@lvde0 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 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants