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

Source generator not running on build when imported from nuget #52427

Closed
AlexMacocian opened this issue Apr 6, 2021 · 11 comments
Closed

Source generator not running on build when imported from nuget #52427

AlexMacocian opened this issue Apr 6, 2021 · 11 comments
Assignees
Labels
Area-Compilers Bug New Feature - Source Generators Source Generators Resolution-Duplicate The described behavior is tracked in another issue Resolution-External The behavior lies outside the functionality covered by this repository

Comments

@AlexMacocian
Copy link

AlexMacocian commented Apr 6, 2021

Version Used:
NetStandard 2.0 in generator project
Net5.0-windows in consumer project
Microsoft.CodeAnalysis.Analyzers 3.3.2 in generator project
Microsoft.CodeAnalysis.CSharp 3.9.0 in generator project
Langversion preview in both projects
VS Enterprise 16.9.3

Steps to Reproduce:

  1. Create some project and define a source generator inside it.
  2. Package the solution using <None Include="$(OutputPath)\$(AssemblyName).dll" Pack="true" PackagePath="analyzers/dotnet/cs" Visible="false" />
  3. Create consumer project and import previously created nuget package.

Expected Behavior:
After restarting VS, source generator is active and creates new files. They can be observed in Solution Explorer under the Dependencies/Analyzers/{Analyzer-name}/
When building, these generated files are detected and included in the compilation.

Actual Behavior:
Files are generated and they can be observed in Solution Explorer.
When building the solution, the build fails with error CS0246, type or namespace could not be found.

More info
I'm working on a source generator where you can annotate fields in classes with an attribute and then the generator will build the entire cruft of dependency properties in another file. The implementation works and I've tested it on a test project that imported the generator project directly. Sources are generated and accounted for during compilation.

In the project I'm consuming them now, I'm pulling the generator from nuget. In the IDE, the sources are being generated. I can see the files in Solution Explorer. I can also navigate to them with ctrl+click. Both the attribute source as well as the partial implementation of the class that I used it in are generated. Issue comes when I try to run the project. Msbuild reports that it cannot find the GenerateDependencyPropertyAttribute I use to annotate the fields. VS sees it and I'm able to navigate to it but msbuild doesn't.

I suppose this is probably some mistake in the project configuration but I cannot find any more information about it.

Generator csproj

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>netstandard2.0</TargetFramework>
    <GeneratePackageOnBuild>true</GeneratePackageOnBuild>
    <PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
    <Authors>Alexandru Macocian</Authors>
    <Description>Source generator library.</Description>
    <PackageLicenseFile>LICENSE</PackageLicenseFile>
    <Version>0.1.2</Version>
    <LangVersion>latest</LangVersion>
    <EnableNETAnalyzers>true</EnableNETAnalyzers>
    <IncludeBuildOutput>false</IncludeBuildOutput>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.2" PrivateAssets="all">
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
    <PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="3.9.0" PrivateAssets="all" />
  </ItemGroup>

  <ItemGroup>
    <None Include="LICENSE">
      <Pack>True</Pack>
      <PackagePath></PackagePath>
    </None>
  </ItemGroup>
  
  <ItemGroup>
    <!-- Package the generator in the analyzer directory of the nuget package -->
    <None Include="$(OutputPath)\$(AssemblyName).dll" Pack="true" PackagePath="analyzers/dotnet/cs" Visible="false" />
  </ItemGroup>
  
</Project>

Consumer csproj

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net5.0-windows</TargetFramework>
    <UseWPF>true</UseWPF>
    <UseWindowsForms>true</UseWindowsForms>
    <Platforms>AnyCPU;x64</Platforms>
    <CopyLocalLockFileAssemblies>false</CopyLocalLockFileAssemblies>
    <LangVersion>preview</LangVersion>
    <EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
    <CompilerGeneratedFilesOutputPath>obj</CompilerGeneratedFilesOutputPath>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="WpfExtended.SourceGeneration" Version="0.1.2" PrivateAssets="all" />
  </ItemGroup>
@dotnet-issue-labeler dotnet-issue-labeler bot added Area-Compilers untriaged Issues and PRs which have not yet been triaged by a lead labels Apr 6, 2021
@jaredpar jaredpar added Bug New Feature - Source Generators Source Generators and removed untriaged Issues and PRs which have not yet been triaged by a lead labels Apr 20, 2021
@jaredpar
Copy link
Member

@chsienki FYI

@chsienki
Copy link
Contributor

@AlexMacocian Do you have a repro solution somewhere? I tried building https://github.com/AlexMacocian/WpfExtended/ and swapping out the reference with the nuget package here https://www.nuget.org/packages/WpfExtended.SourceGeneration/ but couldn't get anything to repro.

@AlexMacocian
Copy link
Author

@chsienki I tried using the WpfExtended.SourceGeneration nuget in some wpf project. I'll build a test solution and push it to github. I'll post the link once it's there.

By not getting anything to repro, do you mean that for you it was functional? Were you able to build and run the test project with the nuget reference?

@AlexMacocian
Copy link
Author

@chsienki I've uploaded a test project here:
https://github.com/AlexMacocian/SourceGenerationTest

I'm seeing the same issue. I can see the generated attribute both in the Analyzers subdirectory in the Solution Explorer as well as I can navigate to it using CTRL+RightClick in VS2019. I can also navigate to the generated partial class and see the code. No errors are detected. But when I try to build, I get the following errors:
Build started... 1>------ Build started: Project: SourceGenerationTest, Configuration: Debug Any CPU ------ 1>C:\Users\Owner\source\repos\SourceGenerationTest\SourceGenerationTest\MainWindow.xaml.cs(2,22,2,32): error CS0234: The type or namespace name 'Extensions' does not exist in the namespace 'System.Windows' (are you missing an assembly reference?) 1>C:\Users\Owner\source\repos\SourceGenerationTest\SourceGenerationTest\MainWindow.xaml.cs(11,10,11,36): error CS0246: The type or namespace name 'GenerateDependencyPropertyAttribute' could not be found (are you missing a using directive or an assembly reference?) 1>C:\Users\Owner\source\repos\SourceGenerationTest\SourceGenerationTest\MainWindow.xaml.cs(11,10,11,36): error CS0246: The type or namespace name 'GenerateDependencyProperty' could not be found (are you missing a using directive or an assembly reference?) 1>C:\Users\Owner\source\repos\SourceGenerationTest\SourceGenerationTest\MainWindow.xaml.cs(11,37,11,49): error CS0246: The type or namespace name 'InitialValue' could not be found (are you missing a using directive or an assembly reference?) 1>Done building project "SourceGenerationTest_gnujgmlu_wpftmp.csproj" -- FAILED. ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Are you able to repro the issue now?

@AlexMacocian
Copy link
Author

@chsienki Any update pls? Could you pls try the test above?

@chsienki
Copy link
Contributor

chsienki commented May 4, 2021

@AlexMacocian Thanks for the project, I'll take a look today and see if I can repro it / figure out whats going on.

@AlexMacocian
Copy link
Author

@chsienki Thanks a lot! If there's anything I can do to help, please tell me. I'm sorry I can't provide more info, I tried to figure it out myself but I am kinda stuck and really don't get why it's not working as expected.

@sharwell
Copy link
Member

sharwell commented May 5, 2021

Done building project "SourceGenerationTest_gnujgmlu_wpftmp.csproj"

@chsienki This looks like a duplicate of dotnet/wpf#3404

@AlexMacocian
Copy link
Author

@sharwell Wow, thanks! Sorry, I didn't realize this was a duplicate. Thanks a lot for linking it. Adding the following property works for me.

<IncludePackageReferencesDuringMarkupCompilation>true</IncludePackageReferencesDuringMarkupCompilation>

Please close this issue if you so consider.

@sharwell
Copy link
Member

sharwell commented May 5, 2021

@AlexMacocian duplicates are no problem. glad to hear it's working now! 😄

@sharwell sharwell closed this as completed May 5, 2021
@sharwell
Copy link
Member

sharwell commented May 5, 2021

Duplicate of dotnet/wpf#3404

@sharwell sharwell marked this as a duplicate of dotnet/wpf#3404 May 5, 2021
@sharwell sharwell added Resolution-Duplicate The described behavior is tracked in another issue Resolution-External The behavior lies outside the functionality covered by this repository labels May 5, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Compilers Bug New Feature - Source Generators Source Generators Resolution-Duplicate The described behavior is tracked in another issue Resolution-External The behavior lies outside the functionality covered by this repository
Projects
None yet
Development

No branches or pull requests

4 participants