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

File New Project Analyzer does not build #514

Closed
KSchlobohm opened this issue Apr 9, 2020 · 33 comments · Fixed by #531
Closed

File New Project Analyzer does not build #514

KSchlobohm opened this issue Apr 9, 2020 · 33 comments · Fixed by #531

Comments

@KSchlobohm
Copy link

Repro steps

  1. File -> New Project
  2. Choose the Analyzer with Code Fix (.NET Standard) template

Expected behavior

  1. Using the Vsix project as the startup project will start a new Visual Studio instance with the UPPERCASE analyzer example loaded.

Actual behavior

  1. NuGet restore does not work due to ambiguous project names. This blocks debugging.
  2. Unit test project does not compile.
@jinujoseph jinujoseph transferred this issue from dotnet/roslyn-analyzers Apr 20, 2020
@jinujoseph
Copy link
Contributor

cc @jmarolf

@sharwell
Copy link
Member

sharwell commented May 3, 2020

@KSchlobohm We've recently made substantial changes to the analyzer templates. Can you clarify which version of Visual Studio you used to create the analyzer project?

@KSchlobohm
Copy link
Author

Visual Studio 2019 Version 16.6.0 Preview 4.0

@jmarolf jmarolf self-assigned this May 6, 2020
@poke
Copy link

poke commented May 22, 2020

This also applies to both the release version 16.6.0 and also version 16.7.0 Preview 1.0.

As for the error with the test project, it has references to the following packages:

<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Analyzer.Testing.MSTest" Version="1.0.1-beta1.*" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.CodeFix.Testing.MSTest" Version="1.0.1-beta1.*" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.CodeRefactoring.Testing.MSTest" Version="1.0.1-beta1.*" />

But neither of these packages are published on NuGet. I was able to fix the references by adding the following to restore them from the preview feed:

<PropertyGroup>
  <RestoreAdditionalProjectSources>https://dotnet.myget.org/F/roslyn-analyzers/api/v3/index.json;$(RestoreAdditionalProjectSources)</RestoreAdditionalProjectSources>
</PropertyGroup>

But then I also had to fix the test by replacing the Verify:

-using Verify = Microsoft.CodeAnalysis.CSharp.CodeFix.Testing.MSTest.CodeFixVerifier<
+using Verify = Microsoft.CodeAnalysis.CSharp.Testing.MSTest.CodeFixVerifier<
     AnalyzerTest.AnalyzerTestAnalyzer,
     AnalyzerTest.AnalyzerTestCodeFixProvider>;

// …

-            await Verify.VerifyCSharpDiagnosticsAsync(test);
+            await Verify.VerifyAnalyzerAsync(test);

// …

-            await Verify.VerifyCSharpFixAsync(test, expected, fixtest);
+            await Verify.VerifyCodeFixAsync(test, expected, fixtest);

This fixed the test project.

As for the “Ambiguous project name 'Analyzer1'” error, I was able to fix that by changing the assembly name of the Vsix project:

 <PropertyGroup>
   <TargetFramework>net472</TargetFramework>
   <RootNamespace>Analyzer1.Vsix</RootNamespace>
-  <AssemblyName>Analyzer1</AssemblyName>
+  <AssemblyName>Analyzer1Vsix</AssemblyName>
 </PropertyGroup>

But now when I’m building, I got this:

C:\Program Files\dotnet\sdk\3.1.300\Sdks\Microsoft.NET.Sdk\targets\Microsoft.PackageDependencyResolution.targets(234,5): error NETSDK1005: Assets file 'C:\…\Analyzer1\Analyzer1\obj\project.assets.json' doesn't have a target for '.NETFramework,Version=v4.5.2'. Ensure that restore has run and that you have included 'net452' in the TargetFrameworks for your project.

There is only one reference to .NET 4.5.2, so I changed that to match the target framework of the analyzer project:

 <ItemGroup>
   <!-- https://github.com/dotnet/sdk/issues/433 -->
-   <ProjectReference Update="@(ProjectReference)" AdditionalProperties="TargetFramework=net452" />
+   <ProjectReference Update="@(ProjectReference)" AdditionalProperties="TargetFramework=netstandard2.0" />

   <!-- https://github.com/Microsoft/extendvs/issues/57 -->
   <ProjectReference Update="@(ProjectReference)" Name="%(Filename)" />
 </ItemGroup>

With these changes, I was able to properly compile and debug the analyzer. Hope this helps anyone else struggling with this!

@sharwell
Copy link
Member

@dotnet/project-system do you know of any way to test project templates in CI?

@KeithLRobertson
Copy link

Thank you for the fix steps, @poke !

I lost hours on this before finding this issue. I second @sharwell 's idea to make testing the templates part of the CI pipeline.

@sharwell
Copy link
Member

sharwell commented May 23, 2020

@KeithLRobertson So far I'm still not aware of a great way to test the templates. However, I found a possible way to test the templates in #532 (the tests exist and can be run manually but currently don't run in CI).

@AignerGames
Copy link

I think I have a similar problem with the "Code Refactoring (.NET Standard)" template using Microsoft Visual Studio Community 2019
Version 16.6.0

The template creates 2 projects:

  • MyProjectName (actual code for the refactoring)
  • MyProjectName.Vsix (additional project to build the vsix?)

And the following error:
NuGet packages: Ambiguous project name

The problem is that both created projects have the same assembly name "MyProjectName".
Project properties -> Application -> Assembly name

As a workaround I changed the assembly name of the .Vsix project from "MyProjectName" to "MyProjectName.Vsix".

I don't know if this has any other side effects but the nuget error is gone and everything else seems to work too. Would be nice if the official template could be adjusted.

@stevenbrix
Copy link

@sharwell which version of VS are the new templates in?

@sharwell
Copy link
Member

They should be in the 16.7 release.

@ShreyasJejurkar
Copy link

@sharwell Is there any workaround yet without doing it manually? Updated template? Anything like that ?

@sharwell
Copy link
Member

@MCCshreyas not yet. The template updates are coming in 16.7.

@paul1956
Copy link

@sharwell The same problem still exists with VB Analyzers

@sharwell
Copy link
Member

@paul1956 I fixed both the C# and the VB templates at the same time, so they'll eventually get updated in the distribution for 16.7.

@nenadvicentic
Copy link

nenadvicentic commented Jul 15, 2020

@sharwell

I have tried using "Analyzer with Code Fix (.NET Standard)" template from latest preview of Visual Studio 16.7.0 Preview 4.0 and the new project dialog fails. Clicking on Create button triggers ModalDialogBox with error:

The path is not of a legal form.

Empty folder gets created on the disk, but without any project files.

Error-AnalyzerWithCodeFix

@kofifus
Copy link

kofifus commented Aug 4, 2020

@nenadvicentic did you find a solution ?

@paul1956
Copy link

paul1956 commented Aug 4, 2020

I reported this via Feedback yesterday, it reappeared with Preview 6. Workaround is don't use preview. But then the Analyzer doesn't build.

@nenadvicentic
Copy link

nenadvicentic commented Aug 4, 2020

@paul1956 @kofifus That was my solution as well, not to use Preview version at all.

Still, there is a problem even with the stable version of VS. It uses outdated/never-officially-published Nuget packages for testing of those components and the template needs fixing of old pre-stable-release syntax.

At the end I've built stand-alone console application to do the analysis/refactoring, which is not ideal. Hopefully MS guys provide stable way forward for developing (and testing) analyzers and code fixes for Visual Studio. Current situation is pretty bad and it's shame, because compiler itself is fully capable.

@sharwell
Copy link
Member

sharwell commented Aug 4, 2020

@paul1956 The workaround is to use the preview version with the following sequence:

  1. Create an Empty Solution from template
  2. Add a project to the solution using the Analyzer and Code Fix template

@paul1956
Copy link

paul1956 commented Aug 4, 2020

Thanks, is there a reason this is closed when in fact it does not work?

@sharwell
Copy link
Member

sharwell commented Aug 4, 2020

The issue is assigned to an internal team. The templates were working fine in earlier 16.7 previews, prior to an unrelated product regression.

@sharwell
Copy link
Member

sharwell commented Aug 4, 2020

Also note that this bug is marked fixed because the original issue #514 (comment) is fixed. The unrelated issue was just a follow-up comment in #514 (comment).

@jmarolf
Copy link
Contributor

jmarolf commented Aug 4, 2020

@paul1956 The feedback ticked you filed here is assigned to the extensibility team and we've pinged them. I will consider that the canonical place to track the "This path is not of a legal form" error. I will see if I can help the extensibility team figure out what was regressed.

@kofifus
Copy link

kofifus commented Aug 6, 2020

@sharwell I tried your suggestion above about creating a blank solution and adding the project from template, while creating the project I get:

image

Any help please ?

I really need this, is there a way for you to provide a clean solution/project to start from ? that would be helpful

@paul1956
Copy link

paul1956 commented Aug 6, 2020

@sharwell workaround worked for VB but the project has a hard reference to System.Collections.Immutable 1.2.5 and doesn't run with 1.7.0 this is probably a separate issue but not sure where to enter it, Roslyn, .Net Core, somewhere else?

@kofifus
Copy link

kofifus commented Aug 6, 2020

update - for VS19 16.8.0 preview 1.0

  1. create empty solution
  2. add project from template C# Analyzer with Code Fix
  3. Delete all Visual Basic items from the Verifiers folder

Solution now builds and run fine

@nenadvicentic
Copy link

nenadvicentic commented Aug 6, 2020

Another update - VS19 16.7 (final) does not fix the error The path is not of a legal form. VS19 16.8-preview1 also has the error.

image

@sharwell
Copy link
Member

sharwell commented Aug 6, 2020

Delete all Visual Basic items from the Verifiers folder

@kofifus For me, it is not necessary to do this step. Can you open a new issue for this?

Another update - VS19 16.7 (final) does not fix the error The path is not of a legal form. VS19 16.8-preview1 also has the error.

@nenadvicentic This is a project system bug being investigated here:
https://developercommunity.visualstudio.com/content/problem/1133261/cant-create-new-vb-vsix-project-with-codefix.html

... has a hard reference to System.Collections.Immutable 1.2.5 and doesn't run with 1.7.0 ...

@paul1956 Can you open a new issue for this? It sounds unrelated to the other issues here.

@kofifus
Copy link

kofifus commented Aug 16, 2020

Hi are there any news about this ? is there any way to get a template that builds with the tests running for latest VS19 preview ?

@sharwell
Copy link
Member

@kofifus The path error is fixed for 16.8 Preview 2. The workaround from #514 (comment) can be used in the meantime. For other problems, see #583 (comment) for the current status.

@kofifus
Copy link

kofifus commented Aug 17, 2020

Thanks @sharwell, until it is all released is there a way for you to provide a download of a solution with a project based on the C# codefix template that builds and runs all tests ? I am no successful in creating that

@sharwell
Copy link
Member

@kofifus Can you create a new issue for the specific problem you are encountering?

@MrZarq
Copy link

MrZarq commented Feb 5, 2021

This also applies to both the release version 16.6.0 and also version 16.7.0 Preview 1.0.

As for the error with the test project, it has references to the following packages:

<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Analyzer.Testing.MSTest" Version="1.0.1-beta1.*" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.CodeFix.Testing.MSTest" Version="1.0.1-beta1.*" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.CodeRefactoring.Testing.MSTest" Version="1.0.1-beta1.*" />

But neither of these packages are published on NuGet. I was able to fix the references by adding the following to restore them from the preview feed:

<PropertyGroup>
  <RestoreAdditionalProjectSources>https://dotnet.myget.org/F/roslyn-analyzers/api/v3/index.json;$(RestoreAdditionalProjectSources)</RestoreAdditionalProjectSources>
</PropertyGroup>

For reference to future Googlers, this feed does not exist anymore. You now need to add the following feeds:

(It's likely only one of them is needed, but I didn't investigate further)

Edit by @sharwell : I crossed out the unnecessary ones. 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.