DotNetAnalyzers / StyleCopAnalyzers

An implementation of StyleCop rules using the .NET Compiler Platform
MIT License
2.65k stars 508 forks source link

VS2022 Syntax errors #3457

Closed nstod closed 2 years ago

nstod commented 2 years ago

StyleCop doesn't seem to like using declarations in VS2022. It works fine in VS2019.

Repro:

  1. Create a new C# project in VS2022.
  2. Install the latest pre-release StyleCop (1.2.0-beta.406)
  3. Inside a method, add the following line: using var cts = new CancellationTokenSource();
  4. Notice that you now have an error: "A syntax error has been discovered in file Program.cs on line 5. [StyleCop Rule: SA0102]"
sharwell commented 2 years ago

It sounds like in step (2), you're installing StyleCop Classic (the old StyleCop from Visual Studio 2013 and earlier) instead of StyleCop Analyzers (this project). StyleCop Analyzers doesn't have any rule SA0102.

nstod commented 2 years ago

Here is my full csproj:

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

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net6.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="StyleCop.Analyzers" Version="1.2.0-beta.406">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
  </ItemGroup>

</Project>
sharwell commented 2 years ago

There must be a Directory.Build.props/Directory.Build.targets file in a parent directory which is injecting additional packages outside what the project file shows. If you build on the command line and pass the /bl flag to the compiler, it will produce a msbuild.binlog file which you can open in the MSBuild Structured Log Viewer to locate the point where StyleCop Classic is running.

sharwell commented 2 years ago

See also #3066

This is a situation where I definitely want to help, but the error in question could not have been caused by any version of this project (it never existed). If I had a project with this error, by first choice for investigating it would be the binlog.

nstod commented 2 years ago

I apologize for all this. I wasn't able to track it down with the binlog, but I had forgotten that ReSharper had a StyleCop extension enabled. Once I disabled that, the problem went away. I will close this issue. I really appreciate the help.

sharwell commented 2 years ago

Glad to hear the binlog helped!