dotnet / maui

.NET MAUI is the .NET Multi-platform App UI, a framework for building native device applications spanning mobile, tablet, and desktop.
https://dot.net/maui
MIT License
21.96k stars 1.7k forks source link

Creating a new MAUI App project and naming it "MauiApp" fails to compile #8474

Closed jstedfast closed 2 years ago

jstedfast commented 2 years ago

Description

I just used Visual Studio for Mac 17.3 Preview 3 (in case that matters for the template being used) and I just so happened to name the app "MauiApp" which coincidentally conflicts with a type that is used in the generated MauiProgram.cs (and other files) which causes the following build errors:

dotnet --version
6.0.301
dotnet --list-sdks
6.0.201 [/usr/local/share/dotnet/sdk]
6.0.203 [/usr/local/share/dotnet/sdk]
6.0.300 [/usr/local/share/dotnet/sdk]
6.0.301 [/usr/local/share/dotnet/sdk]
dotnet --list-runtimes
Microsoft.AspNetCore.App 6.0.3 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 6.0.5 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 6.0.6 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
Microsoft.NETCore.App 6.0.3 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
Microsoft.NETCore.App 6.0.5 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
Microsoft.NETCore.App 6.0.6 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]

Steps to Reproduce

  1. Open Visual Studio for Mac 17.3 build 1680
  2. Click + New to create a new project
  3. Select Multiplatform / App / .NET MAUI App
  4. Click Continue
  5. Leave the Target framework at .NET 6.0 and click Continue
  6. Type "MauiApp" as the Project name
  7. Click Create
  8. Build

Version with bug

Unknown/Other (please specify)

Last version that worked well

Unknown/Other

Affected platforms

iOS, macOS

Affected platform versions

macOS 12.4

Did you find any workaround?

I can change the namespace to MauiApp1 to fix it.

Relevant log output

* MauiApp/MauiApp/MauiProgram.cs(16,16): Error CS0118: 'MauiApp' is a namespace but is used like a type (CS0118) (MauiApp)
* MauiApp/MauiApp/Platforms/Android/MainApplication.cs(21,21): Error CS0118: 'MauiApp' is a namespace but is used like a type (CS0118) (MauiApp)
* MauiApp/MauiApp/MauiProgram.cs(16,16): Error CS0118: 'MauiApp' is a namespace but is used like a type (CS0118) (MauiApp)
* MauiApp/MauiApp/Platforms/iOS/AppDelegate.cs(21,21): Error CS0118: 'MauiApp' is a namespace but is used like a type (CS0118) (MauiApp)
* MauiApp/MauiApp/MauiProgram.cs(16,16): Error CS0118: 'MauiApp' is a namespace but is used like a type (CS0118) (MauiApp)
* MauiApp/MauiApp/Platforms/MacCatalyst/AppDelegate.cs(21,21): Error CS0118: 'MauiApp' is a namespace but is used like a type (CS0118) (MauiApp)
Eilon commented 2 years ago

Indeed, this is an edge case that doesn't work. There are undoubtedly thousands of names that wouldn't work in practice, such as int, or System, and this is also one of them.

Even if we wanted to fix this, I'm not sure it's possible to have some list of known "disallowed" names, so I don't think we even could fix this.

jstedfast commented 2 years ago

I think it's fair to close WONTFIX, I just wanted to present this as something that may (or may not?) be a "common" issue for newbies.

Obviously, for anyone that understands C# reasonably well, they'd figure out the problem and find a work-around pretty quickly.

Eilon commented 2 years ago

@jstedfast understood, it's a perfectly reasonable bug, and hopefully not too many people run into it 😄 I think that the tourism board of Hawaii might be the main user to legitimately run into this with very little recourse 😁

Symbai commented 2 years ago

Ideally such forbidden names should get blocked on creation. Like if you type the name of the app in VS, VS should refuse to continue and tell user that this name is not allowed. If you create it using dotnet it should print out that this name is not allowed.

Then we don't have to fix it and no longer have to worry about. And new users don't spend minutes/hours of work into something that then suddenly fails at the very last step (compiling). Not sure which team/repo is responsible for VS/dotnet and where we could suggest this change.

Eilon commented 2 years ago

@Symbai the problem is that the list of problematic names is enormous. Various C# reserved words, various names that conflict with built-in .NET types, etc. are all problematic. It's a list of literally thousands (if not even 10,000's or 100,000's) of names. And whether they cause a problematic conflict depends on the exact code that's in the template. Some names might work fine at first, but once you add one more thing to the project, the name could start to conflict.

I'm fine keeping this issue open to see if it is a sufficiently common problem, but so far I don't recall seeing it much across any project type, let alone .NET MAUI. We have various telemetry channels in Visual Studio and I don't think that using problematic names has ever shown up as a key failure point.

Xyncgas commented 2 years ago

Ideally such forbidden names should get blocked on creation. Like if you type the name of the app in VS, VS should refuse to continue and tell user that this name is not allowed. If you create it using dotnet it should print out that this name is not allowed.

It's ok, I am a strong independent woman that is going to figure out renaming the class name to @MauiApp, since I don't want my web browser blocking me for searching a word that's banned by the government anyways.

jfversluis commented 2 years ago

As pointed out, this is not specific to .NET MAUI, naming your project like any type in code isn't going to work great. If this will ever be fixed, it needs to be done in tooling to prevent the project from even being created. There is nothing for us to do here.