dotnet / winforms

Windows Forms is a .NET UI framework for building Windows desktop applications.
MIT License
4.41k stars 984 forks source link

Missing Program.vb in Winforms App targeting 3.1 from Visual Studio #7267

Closed phenning closed 2 years ago

phenning commented 2 years ago

.NET version

7.0.100-preview.4.22252.9

Did it work in .NET Framework?

Yes

Did it work in any of the earlier releases of .NET Core or .NET 5+?

Only with netcoreapp3.1 SDK I believe.

Issue description

When creating a NET Core 3.1 WinForms app in VIsual Studio, an error is encountered:

1>------ Build started: Project: WinFormsApp5, Configuration: Debug Any CPU ------ 1>vbc : error BC30420: 'Sub Main' was not found in 'WinFormsApp5'. 1>Done building project "WinFormsApp5.vbproj" -- FAILED. ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

This is happening because the Program.vb is not being laid down by the template due to an incorrect condition in template.json When the same template is created on the command line via the following, the Program.vb is created, so no error results. dotnet new winforms -f netcoreapp3.1 -lang VB

The following configuration is causing the errors: Since the TFM is netcoreapp3.1, UseWindowsDesktopSdkis set to true;

    "UseWindowsDesktopSdk": {
      "type": "computed",
      "value": "(Framework == \"netcoreapp3.1\")"
    },

As a result of this, the ApplicationEvents, and other files are excluded:


            "condition": "(UseWindowsDesktopSdk)",
            "exclude": [
              "Company.WinFormsApplication1.vbproj",
              "Company.WinFormsApplicationSkipAppModel1.vbproj",
              "My Project/**/*",
              "ApplicationEvents.vb"
            ],
            "rename": {
              "Company.WinFormsApplication3x1.vbproj": "Company.WinFormsApplication1.vbproj"
            }
          },

However, the Program.vb is ALSO excluded:

          {
            "condition": "(!skipAppModel)",
            "exclude": [
              "Program.vb",
              "Company.WinFormsApplicationSkipAppModel1.vbproj"
            ],
            "copyOnly": [ "My Project/**/*" ]
          },

This happens because skipAppModel is evaluated as false, but the condition above is a negation, so Program.vb is always excluded (hostIsCli == false) in this case, so this will always be false when run from Visual Studio "skipAppModel": { "type": "computed", "value": "(hostIsCli && !UseAppFramework)" }

Fix here would be to update the skipAppModel condition to: "(UseWindowsDesktopSdk || (hostIsCli && !UseAppFramework))"

Steps to reproduce

Repro steps:

  1. Create a VisualBasic Winforms project targeting NET Core 3.1 in Visual Studio
  2. Build
  3. Observe the Output Window

Build started... 1>------ Build started: Project: WinFormsApp5, Configuration: Debug Any CPU ------ 1>vbc : error BC30420: 'Sub Main' was not found in 'WinFormsApp5'. 1>Done building project "WinFormsApp5.vbproj" -- FAILED. ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

RussKie commented 2 years ago

VB is not supported in .NET Core 3.1, the support was added in .NET 5, https://devblogs.microsoft.com/dotnet/visual-basic-winforms-apps-in-net-5-and-visual-studio-16-8/.

/cc: @KlausLoeffelmann @KathleenDollard

phenning commented 2 years ago

VB is not supported in .NET Core 3.1, the support was added in .NET 5, https://devblogs.microsoft.com/dotnet/visual-basic-winforms-apps-in-net-5-and-visual-studio-16-8/.

/cc: @KlausLoeffelmann @KathleenDollard

Support in the template was added in this PR, so it's been "supported" for quite some time.

https://github.com/dotnet/winforms/commit/bc0c4af69366d388b880980282209d6c9c0a2372#diff-a748f16615f37a9935eb65fdabe285c79fc66f99c5f62e339f9ed48189ce81f5

It (netcoreapp3.1) got broken when you added support for VB Application Framework in this PR: https://github.com/dotnet/winforms/commit/8bec0f0c8ad2057f24fb1b003a5b3e696b02eccc

My PR fixes the brokenness IRT to 3.1 apparently not supporting VB Application Framework, thus the explicit parameter which is in the template.

    "UseWindowsDesktopSdk": {
      "type": "computed",
      "value": "(Framework == \"netcoreapp3.1\")"
    },

I think the confusion is here:

(From the link above: https://devblogs.microsoft.com/dotnet/visual-basic-winforms-apps-in-net-5-and-visual-studio-16-8/)

While .NET Core has had Visual Basic since the first release, and WinForms since it was released in .NET Core 3.1, it did not include the Application Framework library and WinForms Designer support Visual Basic programmers expect.

I read this as WinForms did support VB in netcoreapp3.1, but did not support the Application Framework library until net5.0

RussKie commented 2 years ago

@phenning thank you for digging in.

I had a quick offline chat with @KlausLoeffelmann, and he straightened me - VB is supported from cli just not in VS.

phenning commented 2 years ago

@RussKie I think its just Application Framework isn't supported when targeting 3.1.

That appears to be the intent in both the blog posts and in all the previous updates to template.json. It's just that there was a slight authoring error in the template.json.

Its pretty apparent there is different expected behavior here since 3.1 uses <Project Sdk="Microsoft.NET.Sdk.WindowsDesktop"> and 5.0+ uses <Project Sdk="Microsoft.NET.Sdk">

RussKie commented 2 years ago

Yes, "Application Framework" is the keyword that I omitted from my earlier reply.

Nora-Zhou01 commented 2 years ago

Verified on the .NET 7.0 Preview6 test pass build: 7.0.100-preview.6.22352.1, this issue is fixed that the .NET Core 3.1 VB WinForms project which created from VS can be built successfully now. 3 1