dotnet / dotnet/roslyn

ResxSourceGenerator brokes WinForms application build

Open
#83,504 0 comments 0 reactions 0 assignees View on GitHub
Area-Compilers
Dominant language
C#
Stars
20.7k
Forks
4.3k
PR merge metrics
PR metrics pending

Description

**Version Used**: Microsoft.CodeAnalysis.ResxSourceGenerator Version="5.0.0-1.25277.114

**Steps to Reproduce**:

1. Grab test WinForms app: [WinFormsApp7.zip](https://github.com/user-attachments/files/27245348/WinFormsApp7.zip) or create your own with the following steps:
* `dotnet new winforms`
* open project in VS, open form editor - it will create `Form1.resx` for you.
* install source generator `dotnet add package Microsoft.CodeAnalysis.ResxSourceGenerator --version 5.0.0-1.25277.114`
2. `dotnet build`
3. Observe build errors (see below).
4. (optional) Remove `Microsoft.CodeAnalysis.ResxSourceGenerator` reference from the csproj
5. (optional) Build again - now it builds successfully.

**Diagnostic Id**: Multiple CSXXX build errors during the incorrect source generation

If this is a report about a bug in an analyzer, please include the diagnostic ID and message if possible (e.g. `"IDE0030: Use coalesce expression"`).

**Expected Behavior**: project builds successfully

**Actual Behavior**: build errors.
```Restore complete (0.7s)
WinFormsApp7 net10.0-windows failed with 7 error(s) (0.3s)
D:\delme\WinFormsApp7\Form1.cs(3,22): error CS0262: Partial declarations of 'Form1' have conflicting accessibility modifiers
D:\delme\WinFormsApp7\Form1.cs(3,30): error CS0713: Static class 'Form1' cannot derive from type 'Form'. Static classes must derive from object.
D:\delme\WinFormsApp7\Form1.cs(5,12): error CS0710: Static classes cannot have instance constructors
D:\delme\WinFormsApp7\Form1.Designer.cs(8,46): error CS0708: 'Form1.components': cannot declare instance members in a static class
D:\delme\WinFormsApp7\Form1.Designer.cs(14,29): error CS0708: 'Dispose': cannot declare instance members in a static class
D:\delme\WinFormsApp7\Form1.Designer.cs(29,18): error CS0708: 'InitializeComponent': cannot declare instance members in a static class
D:\delme\WinFormsApp7\Form1.Designer.cs(14,29): error CS1057: 'Form1.Dispose(bool)': static classes cannot contain protected members
```
## Additional details
The project contains `Form1.resx` which is created by the WinForms editor. The source generator catches that file and produce the `internal static partial class Form1` class from it. And this class conflicts with the *normal* non-static public `Form1` class.

## Workaround
Manually exclude all the WinForms-related resources from the generator processing: ``. This removes the build error and at the first sight does prevent WinForms from using their resources.

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.