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

Question: Correct way to set up a SG project, which has a dependency to other projects/libs, to work in VS #774

Closed
egil opened this issue Apr 1, 2021 · 7 comments

Comments

@egil
Copy link

egil commented Apr 1, 2021

I have a source generator project that uses Blazor under the hood to generate the code. It does this via genzor.

The SG works perfectly from the command line (dotnet build/dotnet run), however, doesn't work within Visual Studio 2019 (16.10.0 Preview 1.0 nor 16.9.2).

I should have all dependencies set to with netstandard2.0/netcoreapp3.1, but I still get these warnings when I try to build in VS:

Severity	Code	Description	Project	File	Line	Suppression State
Warning	CS8785	Generator 'HelloWorldGenerator' failed to generate source. It will not contribute to the output and compilation errors may occur as a result. Exception was of type 'FileNotFoundException' with message 'Could not load file or assembly 'Microsoft.Extensions.DependencyInjection.Abstractions, Version=3.1.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60' or one of its dependencies. The system cannot find the file specified.'	GenzorSourceGeneratorsDemoApp	C:\Source\genzor\samples\GenzorSourceGeneratorsDemoApp\CSC	1	Active
Error	CS0103	The name 'GenzorHelloWorldGenerated' does not exist in the current context	GenzorSourceGeneratorsDemoApp	C:\Source\genzor\samples\GenzorSourceGeneratorsDemoApp\Program.cs	10	Active
Warning	CS8034	Unable to load Analyzer assembly C:\lib\netstandard2.0\Microsoft.AspNetCore.Components.dll: Could not find a part of the path 'C:\lib\netstandard2.0\Microsoft.AspNetCore.Components.dll'.	GenzorSourceGeneratorsDemoApp		1	Active
Warning	CS8034	Unable to load Analyzer assembly C:\lib\netstandard2.0\Microsoft.Extensions.Logging.Abstractions.dll: Could not find a part of the path 'C:\lib\netstandard2.0\Microsoft.Extensions.Logging.Abstractions.dll'.	GenzorSourceGeneratorsDemoApp		1	Active
Warning	CS8034	Unable to load Analyzer assembly C:\lib\netstandard2.0\Microsoft.Extensions.DependencyInjection.Abstractions.dll: Could not find a part of the path 'C:\lib\netstandard2.0\Microsoft.Extensions.DependencyInjection.Abstractions.dll'.	GenzorSourceGeneratorsDemoApp		1	Active
Warning	CS8034	Unable to load Analyzer assembly C:\lib\netstandard2.0\Microsoft.Bcl.AsyncInterfaces.dll: Could not find a part of the path 'C:\lib\netstandard2.0\Microsoft.Bcl.AsyncInterfaces.dll'.	GenzorSourceGeneratorsDemoApp		1	Active
Warning	CS8034	Unable to load Analyzer assembly C:\lib\netstandard2.0\genzor.dll: Could not find a part of the path 'C:\lib\netstandard2.0\genzor.dll'.	GenzorSourceGeneratorsDemoApp		1	Active
Warning	CS8034	Unable to load Analyzer assembly C:\lib\netstandard2.0\Microsoft.Extensions.DependencyInjection.dll: Could not find a part of the path 'C:\lib\netstandard2.0\Microsoft.Extensions.DependencyInjection.dll'.	GenzorSourceGeneratorsDemoApp		1	Active

The user (csproj) of the SG's look like this:

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

	<PropertyGroup>
		<OutputType>Exe</OutputType>
		<TargetFramework>netcoreapp3.1</TargetFramework>
	</PropertyGroup>

	<ItemGroup>
		<ProjectReference Include="..\GenzorSourceGenerators\GenzorSourceGenerators.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
	</ItemGroup>
	
</Project>

The SG csproj looks like this:

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

	<PropertyGroup>
		<TargetFramework>netstandard2.0</TargetFramework>
		<RazorLangVersion>3.0</RazorLangVersion>
		<RootNamespace>GenzorSourceGenerators</RootNamespace>
	</PropertyGroup>

	<ItemGroup>
		<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="3.9.0" PrivateAssets="all" />
		<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.2" PrivateAssets="all" />
	</ItemGroup>
	
	<ItemGroup>
		<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="$(DotNet3Version)" GeneratePathProperty="true" PrivateAssets="all" />
		<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="$(DotNet3Version)" GeneratePathProperty="true" PrivateAssets="all" />
		<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="$(DotNet3Version)" GeneratePathProperty="true" PrivateAssets="all" />
		<PackageReference Include="Microsoft.AspNetCore.Components" Version="$(DotNet3Version)" GeneratePathProperty="true" PrivateAssets="all" />
		<PackageReference Include="Microsoft.Bcl.AsyncInterfaces" Version="1.1.1" GeneratePathProperty="true" PrivateAssets="all" />
		<PackageReference Include="genzor" Version="$(GenzorVersion)" GeneratePathProperty="true" PrivateAssets="all" />
	</ItemGroup>

	<PropertyGroup>
		<GetTargetPathDependsOn>$(GetTargetPathDependsOn);GetDependencyTargetPaths</GetTargetPathDependsOn>
	</PropertyGroup>
	
	<Target Name="GetDependencyTargetPaths">
		<ItemGroup>
			<TargetPathWithTargetPlatformMoniker Include="$(PKGMicrosoft_AspNetCore_Components)\lib\netstandard2.0\Microsoft.AspNetCore.Components.dll" IncludeRuntimeDependency="false" />
			<TargetPathWithTargetPlatformMoniker Include="$(PKGMicrosoft_Extensions_DependencyInjection)\lib\netstandard2.0\Microsoft.Extensions.DependencyInjection.dll" IncludeRuntimeDependency="false" />
			<TargetPathWithTargetPlatformMoniker Include="$(PKGMicrosoft_Extensions_DependencyInjection_Abstractions)\lib\netstandard2.0\Microsoft.Extensions.DependencyInjection.Abstractions.dll" IncludeRuntimeDependency="false" />
			<TargetPathWithTargetPlatformMoniker Include="$(PKGMicrosoft_Extensions_Logging_Abstractions)\lib\netstandard2.0\Microsoft.Extensions.Logging.Abstractions.dll" IncludeRuntimeDependency="false" />
			<TargetPathWithTargetPlatformMoniker Include="$(PKGMicrosoft_Bcl_AsyncInterfaces)\lib\netstandard2.0\Microsoft.Bcl.AsyncInterfaces.dll" IncludeRuntimeDependency="false" />
			<TargetPathWithTargetPlatformMoniker Include="$(PKGgenzor)\lib\netstandard2.0\genzor.dll" IncludeRuntimeDependency="false" />
		</ItemGroup>
	</Target>

</Project>

The referenced package genzors csproj (hosted on GPR)
looks like this:

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

	<PropertyGroup>
		<TargetFrameworks>net5.0;netstandard2.0</TargetFrameworks>
		<RootNamespace>Genzor</RootNamespace>
		<AssemblyName>Genzor</AssemblyName>
	</PropertyGroup>

	<PropertyGroup>
		<PackageId>genzor</PackageId>
		<Title>genzor</Title>
		<Description>
			Genzor is a library ideally suited for generating many files spanning many folders, using Blazor component model to represent the generated output.
		</Description>
	</PropertyGroup>

	<ItemGroup Condition="'$(TargetFramework)' == 'net5.0'">
		<PackageReference Include="Microsoft.AspNetCore.Components" Version="$(DotNet5Version)" />
		<PackageReference Include="Microsoft.Extensions.Logging" Version="5.0.0" />
	</ItemGroup>
	
	<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
		<PackageReference Include="Microsoft.AspNetCore.Components" Version="$(DotNet3Version)" />
		<PackageReference Include="Microsoft.Extensions.Logging" Version="$(DotNet3Version)" />
	</ItemGroup>

</Project>

The sample sample project is here: https://github.com/egil/genzor/tree/main/samples

I feel like I might be missing something obvious, but hope one of you can point me in the right direction 🙏

@SQL-MisterMagoo
Copy link

@egil It's your global.json I believe.

If you add a version all the errors go away, e.g.

{
  "sdk": {
    "version": "3.0",
    "rollForward": "latestMajor",
    "allowPrerelease": false
  }
}

I don't know why - you're not supposed to need a version with latestMajor, but it's something to do with VS and the way it handles SDKs I guess.

@egil
Copy link
Author

egil commented Apr 1, 2021

Gah, of course. Thanks @SQL-MisterMagoo. I'll give it a go when I get home.

@egil
Copy link
Author

egil commented Apr 1, 2021

@SQL-MisterMagoo it didnt fix bit for me unfortunately. If you ran a dotnet build from the commandline, the generated files might already be in the obj folder.

This is the output of my dotnet --info:

❯ dotnet --info
.NET SDK (reflecting any global.json):
 Version:   6.0.100-preview.2.21155.3
 Commit:    1a9103db2d

Runtime Environment:
 OS Name:     Windows
 OS Version:  10.0.19042
 OS Platform: Windows
 RID:         win10-x64
 Base Path:   C:\Program Files\dotnet\sdk\6.0.100-preview.2.21155.3\

Host (useful for support):
  Version: 6.0.0-preview.2.21154.6
  Commit:  3eaf1f316b

.NET SDKs installed:
  3.1.407 [C:\Program Files\dotnet\sdk]
  5.0.102 [C:\Program Files\dotnet\sdk]
  5.0.104 [C:\Program Files\dotnet\sdk]
  5.0.200-preview.21077.7 [C:\Program Files\dotnet\sdk]
  5.0.201 [C:\Program Files\dotnet\sdk]
  6.0.100-preview.2.21155.3 [C:\Program Files\dotnet\sdk]

.NET runtimes installed:
  Microsoft.AspNetCore.All 2.1.23 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.All 2.1.26 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.App 2.1.23 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 2.1.26 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 3.1.9 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 3.1.11 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 3.1.13 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 5.0.2 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 5.0.4 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 6.0.0-preview.2.21154.6 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 2.1.23 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.1.26 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 3.1.9 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 3.1.10 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 3.1.11 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 3.1.13 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 5.0.2 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 5.0.4 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 6.0.0-preview.2.21154.6 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.WindowsDesktop.App 3.1.9 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 3.1.10 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 3.1.11 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 3.1.13 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 5.0.2 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 5.0.4 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 6.0.0-preview.2.21154.2 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

@SQL-MisterMagoo
Copy link

@egil You are right, I hadn't tested far enough, but I have now.

If you make the change to global.json, you resolve the problem with .NET5 dependencies in VS 2019.

If you also downgrade your DotNet3Version from 3.1.13 to 3.1.0 in both Directory.Build.props files (genzor and samples), then the sample projects all build and run in both CLI and VS 2019 - at least they do for me.

Again, I don't know why - but the error I was getting was that it couldn't load Microsoft.Extensions.DependencyInjection.Abstractions, Version=3.1.0.0 - this made me think maybe something is forcing a downgrade, which led me to try downgrading to 3.1.0, which worked.

@b-straub
Copy link

b-straub commented Apr 2, 2021

There are some limitations for SG when running from VS
dotnet/roslyn#47087 (comment)

The issue is different, but I remember there are other issues that point out, that VS is not using .Net Core yet.
dotnet/roslyn#49249 (comment)

@egil
Copy link
Author

egil commented Apr 2, 2021

@egil You are right, I hadn't tested far enough, but I have now.

If you make the change to global.json, you resolve the problem with .NET5 dependencies in VS 2019.

If you also downgrade your DotNet3Version from 3.1.13 to 3.1.0 in both Directory.Build.props files (genzor and samples), then the sample projects all build and run in both CLI and VS 2019 - at least they do for me.

Again, I don't know why - but the error I was getting was that it couldn't load Microsoft.Extensions.DependencyInjection.Abstractions, Version=3.1.0.0 - this made me think maybe something is forcing a downgrade, which led me to try downgrading to 3.1.0, which worked.

That makes sense. Did you get the sample working by referencing the genzor project directly? If so, how?

@egil
Copy link
Author

egil commented Apr 2, 2021

@SQL-MisterMagoo Confirmed it also works on my computer. The trick was to set the versions to 3.1.0.

I am still getting a bunch of CS8034 Unable to load Analyzer assembly C:\lib\netstandard2.0\Microsoft.Extensions.Logging.Abstractions.dll: Could not find a part of the path 'C:\lib\netstandard2.0\Microsoft.Extensions.Logging.Abstractions.dll'. in Visual Studio, but otherwise it seems to work.

Anyway, ill close this issue and check for related issues to CS8034, and if not, create one.

@egil egil closed this as completed Apr 2, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants