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

Fix build when using source generators (#6534) #6680

Merged
merged 1 commit into from
Jun 16, 2022

Conversation

singhashish-wpf
Copy link
Member

@singhashish-wpf singhashish-wpf commented Jun 14, 2022

Fixes #6522
Fixes dotnet/roslyn#60257

Description

Fixes the build when using source generators.

Customer Impact

Without this PR, customers cannot build a WPF application with source generators.

Regression

No. To my knowledge, it never worked.

Testing

Tested by building a blank WPF application that uses the Regex source generator. This PR fixed the build.

Risk

Low.

/cc @RussKie (It looks like your hunch about the Analyzers property was right)

Microsoft Reviewers: Open in CodeFlow
Microsoft Reviewers: Open in CodeFlow

@singhashish-wpf singhashish-wpf requested a review from a team as a code owner June 14, 2022 05:31
@ghost ghost assigned singhashish-wpf Jun 14, 2022
@ghost ghost added the PR metadata: Label to tag PRs, to facilitate with triage label Jun 14, 2022
@ghost ghost requested a review from dipeshmsft June 14, 2022 05:31
@singhashish-wpf singhashish-wpf added Servicing-consider and removed PR metadata: Label to tag PRs, to facilitate with triage labels Jun 14, 2022
@ghost ghost requested a review from SamBent June 14, 2022 05:32
@ghost
Copy link

ghost commented Jun 14, 2022

Hi @singhashish-wpf. Please make sure you've updated the PR description to use the Shiproom Template. Also, make sure this PR is not marked as a draft and is ready-to-merge.

To learn more about how to prepare a servicing PR click here.

@RussKie
Copy link
Member

RussKie commented Jun 14, 2022

/cc: @ThomasGoulet73

@rbhanda rbhanda added this to the 6.0.7 milestone Jun 14, 2022
@danmoseley
Copy link
Member

@jaredpar , tactics wondered whether a compiler feature like this ought to have a minimal test for each appmodel upstream. I see the Roslyn repo has some test projects that use WPF in connection with code analysis. If one of those used a source generator, would it have caught this bug?

@mmitche
Copy link
Member

mmitche commented Jun 16, 2022

Merge when ready, we are past 6.0.7 code complete.

@pchaurasia14 pchaurasia14 merged commit 7667366 into release/6.0 Jun 16, 2022
@pchaurasia14 pchaurasia14 deleted the build_source_gen branch June 16, 2022 05:14
@jlaanstra
Copy link

@singhashish-wpf After updating to the latest .NET 6 SDK released today, analyzers are now passed twice to the CoreCompile target in the WPF generated project. Some analyzers don't like this and fail with "The namespace already contains a type ".

Analyzers are now explicitly listed in the generated project and also imported by the ResolveLockFileAnalyzers target, resulting in duplicates.

@ThomasGoulet73
Copy link
Contributor

@jlaanstra Could you provide a repro ? Analyzers need to be run twice because of source generators and the way Baml compilation requires CoreCompile to be run twice (Once for the generated project and once for the main project).

@DavidBergstromSWE
Copy link

@singhashish-wpf Since update my WPF application using Microsoft MVVM Toolkit reports errors CS0579 (duplicate attribute) and CS0111 (duplicate member declaration) at compile time, possibly connected to source generators in the toolkit:
Screenshot_20220713

@jamesport079
Copy link

@singhashish-wpf Since update my WPF application using Microsoft MVVM Toolkit reports errors CS0579 (duplicate attribute) and CS0111 (duplicate member declaration) at compile time, possibly connected to source generators in the toolkit: Screenshot_20220713

same problem

@Chris-Dickerson
Copy link

DevExpress.Mvvm.CodeGenerators doesn't like this update either.
vs-code-gen-broken

@ThomasGoulet73
Copy link
Contributor

This seems to be a bug with Analyzers from nuget packages. I'll try to prepare a fix soon. This PR fixed Source Generators from the SDK but broke Source Generators coming from nuget packages.

@Chris-Dickerson
Copy link

Chris-Dickerson commented Jul 13, 2022

global.json workaround works for me -- #6792

@ThomasGoulet73
Copy link
Contributor

ThomasGoulet73 commented Jul 13, 2022

Edit: See #6792 (comment) for a better workaround.

Here's another workaround that works when using the SDK from .Net 6.0.7. Add this to the project file:

  <Target Name="WorkaroundRemoveAnalayzers" BeforeTargets="GenerateTemporaryTargetAssembly">
    <ItemGroup>
      <_TempWorkaroundAnalyzer Include="@(Analyzer)" />
      <Analyzer Remove="@(Analyzer)" />
    </ItemGroup>
  </Target>

  <Target Name="WorkaroundAddAnalayzers" AfterTargets="GenerateTemporaryTargetAssembly">
    <ItemGroup>
      <Analyzer Include="@(_TempWorkaroundAnalyzer)" />
    </ItemGroup>
  </Target>

This will revert the behavior to .Net 6.0.6 where Source Generators coming from Nuget packages will work while Source Generators coming from an SDK will not.

@jlaanstra
Copy link

jlaanstra commented Jul 13, 2022

@ThomasGoulet73 I have a binlog that I can't publicly share. Msft employes can reach out internally if they need it.

I'm using the following workaround which allows both scenarios to work:

<Target Name="RemoveDuplicateAnalyzers" BeforeTargets="CoreCompile">

        <RemoveDuplicates Inputs="@(Analyzer)">
            <Output
                TaskParameter="Filtered"
                ItemName="FilteredAnalyzer"/>
        </RemoveDuplicates>

        <ItemGroup>
            <Analyzer Remove="@(Analyzer)" />
            <Analyzer Include="@(FilteredAnalyzer)" />
        </ItemGroup>
    </Target>

@rmarinho
Copy link
Member

What will be the correct fix and where can i follow it ? on the .net sdk itself ?

@hahyes
Copy link

hahyes commented Jul 25, 2022

What will be the correct fix and where can i follow it ? on the .net sdk itself ?

In 6.0.9 release. Current fix is hanging in 6.0.9 milestone.

@LeonSpors
Copy link

@ThomasGoulet73 I have a binlog that I can't publicly share. Msft employes can reach out internally if they need it.

I'm using the following workaround which allows both scenarios to work:

<Target Name="RemoveDuplicateAnalyzers" BeforeTargets="CoreCompile">

        <RemoveDuplicates Inputs="@(Analyzer)">
            <Output
                TaskParameter="Filtered"
                ItemName="FilteredAnalyzer"/>
        </RemoveDuplicates>

        <ItemGroup>
            <Analyzer Remove="@(Analyzer)" />
            <Analyzer Include="@(FilteredAnalyzer)" />
        </ItemGroup>
    </Target>

Thanks for the workaround.

cmiles added a commit to cmiles/PointlessWaymarksProject that referenced this pull request Aug 24, 2022
@ghost ghost locked as resolved and limited conversation to collaborators Sep 8, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.