dotnet / dotnet/vscode-csharp

Strongly-Typed Resource Generation Difference in Build and Analysis

Open
#5,396 5 comments 0 reactions 0 assignees View on GitHub
Bug Project System Triaged
Dominant language
TypeScript
Stars
3.1k
Forks
737
Avg merge
18h 40m
Merged PRs (30d)
31

Description

The O# design time build calls the build in a way that avoids calling the `CoreResGen` task that runs the resx generator when generating via MSBuild.

This can be worked around by adding `PrepareResources;$(CompileDependsOn)` to ensure that the correct targets run in the O# design time build.

See https://github.com/dotnet/msbuild/issues/8086#issuecomment-1290568321 for a more detailed explanation.

Something likely needs to change here - https://github.com/OmniSharp/omnisharp-roslyn/blob/6068b511ce10fe7100897599954f893efe9643cc/src/OmniSharp.MSBuild/ProjectLoader.cs#L102

Original issue as filed

## Issue Description ##

I'm trying to work out how to [use the strongly-typed resource (resx) generation](https://www.paraesthesia.com/archive/2022/09/30/strongly-typed-resources-with-net-core/) across both VS Code and Visual Studio. I found that if you put this in your .csproj things mostly work:

```xml





ResXFileCodeGenerator
MyResources.Designer.cs
MyResources.Designer.cs
CSharp
MyNamespace
MyResources

```

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 `` is a problem _for OmniSharp code analysis_. It sees that and ignores the `.Designer.cs` files (like you think it should) but then 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`

**Is there a way to indicate in the `Compile` directive for OmniSharp to ignore it during analysis?** For example, is there something like:

```xml

```

During build it would remove the designer files, but during analysis it'd keep them.

## 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 `` and `` 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 `` 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.

## 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 analysis (.Designer.cs still somehow included even though it's excluded from ``).

## Actual Behavior ##

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

## Logs ##

I don't see any relevant logs about the class being/not being there.

## Environment information ##

**VSCode version**: 1.72.0
**C# Extension**: 1.25.0

Mono Information
OmniSharp using mono :6.12.0
Dotnet Information
.NET SDK (reflecting any global.json):
Version: 6.0.401
Commit: 0906eae6f8

Runtime Environment:
OS Name: Mac OS X
OS Version: 12.6
OS Platform: Darwin
RID: osx.12-x64
Base Path: /usr/local/share/dotnet/sdk/6.0.401/

global.json file:
Not found

Host:
Version: 6.0.9
Architecture: x64
Commit: 163a63591c

.NET SDKs installed:
3.1.423 [/usr/local/share/dotnet/sdk]
5.0.408 [/usr/local/share/dotnet/sdk]
6.0.401 [/usr/local/share/dotnet/sdk]

.NET runtimes installed:
Microsoft.AspNetCore.App 3.1.29 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 5.0.17 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 6.0.9 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
Microsoft.NETCore.App 3.1.29 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
Microsoft.NETCore.App 5.0.17 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
Microsoft.NETCore.App 6.0.9 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]

Download .NET:
https://aka.ms/dotnet-download

Learn about .NET Runtimes and SDKs:
https://aka.ms/dotnet/runtimes-sdk-info

Visual Studio Code Extensions

|Extension|Author|Version|
|---|---|---|
|auto-close-tag|formulahendry|0.5.14|
|auto-rename-tag|formulahendry|0.1.10|
|azure-account|ms-vscode|0.11.1|
|azure-pipelines|ms-azure-devops|1.208.0|
|better-comments|aaron-bond|3.0.2|
|better-toml|bungcip|0.3.2|
|code-spell-checker|streetsidesoftware|2.10.1|
|color-info|bierner|0.7.0|
|cosmosdbsqlapi|SavranWeb|0.0.39|
|cpptools|ms-vscode|1.13.2|
|csharp|ms-dotnettools|1.25.0|
|cucumberautocomplete|alexkrechik|2.15.2|
|docomment|k--kato|0.1.31|
|dotenv|mikestead|1.0.1|
|dotnet-test-explorer|formulahendry|0.7.8|
|ecdc|mitchdenny|1.8.0|
|EditorConfig|EditorConfig|0.16.4|
|fabric8-analytics|redhat|0.3.6|
|git-extension-pack|donjayamanne|0.1.3|
|git-graph|mhutchie|1.30.0|
|githistory|donjayamanne|0.6.19|
|gitignore|codezombiech|0.9.0|
|gitlens|eamodio|12.2.2|
|go|golang|0.35.2|
|html-css-class-completion|Zignd|1.20.0|
|intellicode-api-usage-examples|VisualStudioExptTeam|0.2.4|
|java|redhat|1.11.0|
|jupyter|ms-toolsai|2022.9.1002791758|
|jupyter-keymap|ms-toolsai|1.0.0|
|jupyter-renderers|ms-toolsai|1.0.10|
|jwt-debugger|yokawasa|0.4.2|
|live-server|ms-vscode|0.4.2|
|markdown-all-in-one|yzhang|3.4.3|
|markdown-emoji|bierner|0.3.0|
|marp-vscode|marp-team|1.5.2|
|material-theme|zhuangtongfa|3.15.5|
|metals|scalameta|1.20.0|
|msbuild-project-tools|tintoy|0.4.9|
|nc-gcode|ML|0.13.0|
|npm-intellisense|christian-kohler|1.4.2|
|openvpn|idleberg|0.2.3|
|output-colorizer|IBM|0.1.2|
|path-intellisense|christian-kohler|2.8.1|
|plantuml|jebbs|2.17.4|
|powershell|ms-vscode|2022.8.5|
|prettier-toml|bodil|0.1.0|
|prettier-vscode|esbenp|9.9.0|
|project-manager|alefragnani|12.7.0|
|python|ms-python|2022.16.0|
|rainbow-csv|mechatroner|3.2.0|
|remote-containers|ms-vscode-remote|0.255.2|
|remote-repositories|ms-vscode|0.22.0|
|remote-ssh|ms-vscode-remote|0.84.0|
|remote-ssh-edit|ms-vscode-remote|0.84.0|
|remote-wsl|ms-vscode-remote|0.72.0|
|remotehub|GitHub|0.44.0|
|restructuredtext|lextudio|189.2.0|
|rewrap|stkb|1.16.3|
|scala|scala-lang|0.5.6|
|simple-rst|trond-snekvik|1.5.2|
|sort-lines|Tyriar|1.9.1|
|sort-lines-by-selection|earshinov|1.0.0|
|spectral|stoplight|1.1.0|
|terraform|hashicorp|2.24.2|
|todo-tree|Gruntfuggly|0.0.215|
|vscode-arduino|vsciot-vscode|0.4.12|
|vscode-azurefunctions|ms-azuretools|1.8.1|
|vscode-azureresourcegroups|ms-azuretools|0.5.6|
|vscode-azurestorage|ms-azuretools|0.15.0|
|vscode-code-review|d-koppenhagen|1.32.1|
|vscode-commons|redhat|0.0.6|
|vscode-cosmosdb|ms-azuretools|0.19.2|
|vscode-coverage-gutters|ryanluker|2.10.1|
|vscode-docker|ms-azuretools|1.22.1|
|vscode-eslint|dbaeumer|2.2.6|
|vscode-graphviz|joaompinto|0.0.6|
|vscode-icons|vscode-icons-team|11.19.0|
|vscode-java-debug|vscjava|0.44.0|
|vscode-java-dependency|vscjava|0.21.0|
|vscode-java-pack|vscjava|0.25.3|
|vscode-java-test|vscjava|0.37.1|
|vscode-json-stable-stringify|TravisIllig|1.2.1|
|vscode-jupyter-cell-tags|ms-toolsai|0.1.6|
|vscode-jupyter-slideshow|ms-toolsai|0.1.5|
|vscode-kubernetes-tools|ms-kubernetes-tools|1.3.10|
|vscode-markdownlint|DavidAnson|0.48.1|
|vscode-maven|vscjava|0.39.0|
|vscode-open-in-github|ziyasal|1.3.6|
|vscode-open-multiple-files|fabiospampinato|1.4.0|
|vscode-openapi|42Crunch|4.13.0|
|vscode-pylance|ms-python|2022.10.10|
|vscode-remote-extensionpack|ms-vscode-remote|0.21.0|
|vscode-xml|redhat|0.21.0|
|vscode-yaml|redhat|1.10.1|
|vscodeintellicode|VisualStudioExptTeam|1.2.28|
|xml|DotJoshJohnson|2.5.1|
|yaml-plus-json|hilleer|1.11.0|;

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.