Skip to content

CS2002 warning for .resx code generation in .NET Core / VS Code #8086

Closed
@tillig

Description

@tillig

Issue Description

I'm trying to work out how to use the strongly-typed resource (resx) generation across both VS Code and Visual Studio. I found that if you put this in your .csproj things mostly work:

<Project Sdk="Microsoft.NET.Sdk">
  <ItemGroup>
    <!--
      Magic embedded resource incantation based on https://github.com/dotnet/msbuild/issues/4751

      The EmbeddedResource entry seems to add the Designer files to Compile, so
      if you don't first remove them from Compile you get warnings about
      double-including source.
    -->
    <Compile Remove="**/*.Designer.cs" />
    <EmbeddedResource Update="MyResources.resx">
      <Generator>ResXFileCodeGenerator</Generator>
      <LastGenOutput>MyResources.Designer.cs</LastGenOutput>
      <StronglyTypedFileName>MyResources.Designer.cs</StronglyTypedFileName>
      <StronglyTypedLanguage>CSharp</StronglyTypedLanguage>
      <StronglyTypedNamespace>MyNamespace</StronglyTypedNamespace>
      <StronglyTypedClassName>MyResources</StronglyTypedClassName>
    </EmbeddedResource>
  </ItemGroup>
</Project>

When you build this it works great. No warnings, no errors, all the strongly-typed resources are available to your code. If the .Designer.cs file doesn't exist, it will get generated. No problem.

What I've found is that the <Compile Remove="**/*.Designer.cs" /> is a problem for OmniSharp code analysis because it follows MSBuild, but MSBuild somehow is including the .Designer.cs files magically even though they're listed as removed. Whenever you use the class (MyNamespace.MyResources.SomeResource) the OmniSharp code analysis says there's no such class.

However, if you remove that line, then when you build you get a warning:

CSC : warning CS2002: Source file '/path/to/MyNamespace/MyResources.Designer.cs' specified multiple times

I tried filing this as dotnet/vscode-csharp#5396 but they noted it's an MSBuild issue.

Related issues here: #4751, #7609

Steps to Reproduce

  • Create a C# library project.
  • Create a .resx file with a single string in it.
  • In the .csproj file, add the above <Compile> and <EmbeddedResource> directives.
  • Build. You should see the .Designer.cs file get created.
  • Create a class that uses the strongly-typed resource class that was generated.
  • Build. It should build successfully.
  • Look at the analysis in VS Code on the class that uses the strongly-typed resource. You should see a red squiggly and an error saying "The name 'XXXXX' does not exist in the current context" indicating that the class doesn't exist. Note you may need to exit and restart VS Code to see this; sometimes there's a race condition or something that allows the analysis to work and see the class once. On subsequent analysis runs it doesn't work.
  • Edit the .csproj file. Remove the <Compile Remove> directive.
  • Build. The build will issue a warning that the .Designer.cs has been included twice.
  • Look at the analysis in VS Code on the class using the strongly-typed resource. The red squiggly should have disappeared.

I have provided a repro project here with a README that explains how to see the behavior.

Expected Behavior

I expect the analysis to see the class just like the build does - I should have consistent behavior between the build (only include the .Designer.cs file once) and the OmniSharp analysis (.Designer.cs still somehow included even though it's excluded from <Compile>).

OmniSharp is following the MSBuild here, so they insist this is an MSBuild problem, and I tend to agree.

Actual Behavior

There's a difference between the build and the OmniSharp analysis. Either the build works, or I get no-red-squiggly, but I can't have both.

OmniSharp is following the MSBuild here, so they insist this is an MSBuild problem, and I tend to agree. (I apologize for saying this multiple times; in cases like this where I mention "I can see the problem illustrated via XYZ solution" like OmniSharp, there has been a tendency in my experience to want to close the issue and redirect to that solution. It's not an OmniSharp bug. It's an MSBuild bug.)

Analysis

I have not been able to figure out where it's happening, just that it is. Sorry.

Versions & Configurations

  • Mac OS Monterey
  • .NET 6.0.401
  • MSBuild 17.3.1.41501

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugneeds-triageHave yet to determine what bucket this goes in.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions