dotnet / Scaffolding

Code generators to speed up development.
MIT License
632 stars 226 forks source link

Cannot scaffold Identity due to ambiguous calls between extension methods. #2621

Open Grizzlly opened 7 months ago

Grizzlly commented 7 months ago

The following is taken from https://github.com/dotnet/aspnetcore/issues/53290 as I did not know there is a separate repo for scaffolding.

Is there an existing issue for this?

Describe the bug

If I run dotnet aspnet-codegenerator identity, I get this:

Building project ...
Finding the generator 'identity'...
Running the generator 'identity'...
No database provider found. Using 'SqlServer' by default for new DbContext creation!
RunTime 00:00:25.44

This is weird because I do have a context, but this works and adds all Areas/Identity pages to the project. It also modifies Program.cs by adding the new DbContext. However, if I specify the DbContext, I get this:

Building project ...
Finding the generator 'identity'...
Running the generator 'identity'...
Failed to compile the project in memory
< a lot of errors >
S:\Microsoft Visual Studio\workspaceC#\SSO.NET\src\Identity.API\Program.cs(18,9): error CS0121: The call is ambiguous between the following methods or properties: 'LSAC.Services.Identity.API.Extensions.StartupExtensions.ConfigureCors(Microsoft.AspNetCore.Builder.WebApplicationBuilder)' and 'LSAC.Services.Identity.API.Extensions.StartupExtensions.ConfigureCors(Microsoft.AspNetCore.Builder.WebApplicationBuilder)'
S:\Microsoft Visual Studio\workspaceC#\SSO.NET\src\Identity.API\Program.cs(41,45): error CS0121: The call is ambiguous between the following methods or properties: 'LSAC.Services.Identity.API.Extensions.ConfigurationExtensions.GetRequiredConnectionString(Microsoft.Extensions.Configuration.IConfiguration, string)' and 'LSAC.Services.Identity.API.Extensions.ConfigurationExtensions.GetRequiredConnectionString(Microsoft.Extensions.Configuration.IConfiguration, string)'
< a lot more errors like this >

Due to privacy, I am only showing this, but there are around 30 errors. All errors are related to extension methods.

Expected Behavior

The scaffolder generates code or better errors.

Steps To Reproduce

https://github.com/Grizzlly/IdentityScaffoldBugRepro

Open in VS, add new identity scaffold, select Override all files (for example), select the DbContext and click add. You will get this in the logs:

Finding the generator 'identity'...
Running the generator 'identity'...
Failed to compile the project in memory
S:\Microsoft Visual Studio\workspaceC#\WebApplication1\Program.cs(9,46): error CS0121: The call is ambiguous between the following methods or properties: 'Extensions.ConfigurationExtensions.GetRequiredConnectionString(Microsoft.Extensions.Configuration.IConfiguration, string)' and 'Extensions.ConfigurationExtensions.GetRequiredConnectionString(Microsoft.Extensions.Configuration.IConfiguration, string)'
   at Microsoft.VisualStudio.Web.CodeGeneration.ActionInvoker.<BuildCommandLine>b__6_0()
   at Microsoft.Extensions.CommandLineUtils.CommandLineApplication.Execute(String[] args)
   at Microsoft.VisualStudio.Web.CodeGeneration.ActionInvoker.Execute(String[] args)
   at Microsoft.VisualStudio.Web.CodeGeneration.CodeGenCommand.Execute(String[] args)

Exceptions (if any)

Shown above.

.NET Version

8.0.100

Anything else?

.NET SDK: Version: 8.0.100 Commit: 57efcf1350 Workload version: 8.0.100-manifests.6a1e483a

Runtime Environment: OS Name: Windows OS Version: 10.0.22621 OS Platform: Windows RID: win-x64 Base Path: C:\Program Files\dotnet\sdk\8.0.100\

.NET workloads installed: Workload version: 8.0.100-manifests.6a1e483a [maccatalyst] Installation Source: VS 17.8.34316.72 Manifest Version: 17.0.8478/8.0.100 Manifest Path: C:\Program Files\dotnet\sdk-manifests\8.0.100\microsoft.net.sdk.maccatalyst\17.0.8478\WorkloadManifest.json Install Type: Msi

[android] Installation Source: VS 17.8.34316.72 Manifest Version: 34.0.43/8.0.100 Manifest Path: C:\Program Files\dotnet\sdk-manifests\8.0.100\microsoft.net.sdk.android\34.0.43\WorkloadManifest.json Install Type: Msi

[ios] Installation Source: VS 17.8.34316.72 Manifest Version: 17.0.8478/8.0.100 Manifest Path: C:\Program Files\dotnet\sdk-manifests\8.0.100\microsoft.net.sdk.ios\17.0.8478\WorkloadManifest.json Install Type: Msi

[maui-windows] Installation Source: VS 17.8.34316.72 Manifest Version: 8.0.3/8.0.100 Manifest Path: C:\Program Files\dotnet\sdk-manifests\8.0.100\microsoft.net.sdk.maui\8.0.3\WorkloadManifest.json Install Type: Msi

[wasm-tools] Installation Source: VS 17.8.34316.72 Manifest Version: 8.0.0/8.0.100 Manifest Path: C:\Program Files\dotnet\sdk-manifests\8.0.100\microsoft.net.workload.mono.toolchain.current\8.0.0\WorkloadManifest.json Install Type: Msi

Host: Version: 8.0.0 Architecture: x64 Commit: 5535e31a71

.NET SDKs installed: 6.0.202 [C:\Program Files\dotnet\sdk] 8.0.100 [C:\Program Files\dotnet\sdk]

.NET runtimes installed: Microsoft.AspNetCore.App 5.0.4 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 6.0.5 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 6.0.25 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 7.0.14 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 8.0.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.NETCore.App 5.0.4 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 6.0.0-rc.1.21451.13 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 6.0.0 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 6.0.4 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 6.0.5 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 6.0.15 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 6.0.25 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 7.0.14 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 8.0.0 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.WindowsDesktop.App 5.0.4 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 6.0.0-rc.1.21451.3 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 6.0.0 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 6.0.4 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 6.0.5 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 6.0.15 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 6.0.25 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 7.0.14 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 8.0.0 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

Other architectures found: x86 [C:\Program Files (x86)\dotnet] registered at [HKLM\SOFTWARE\dotnet\Setup\InstalledVersions\x86\InstallLocation]

Environment variables: Not set

global.json file: Not found

Learn more: https://aka.ms/dotnet/info

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

Grizzlly commented 7 months ago

I think this exception is thrown here: https://github.com/dotnet/Scaffolding/blob/f32d610bc72416fd6b5719829db280484848c7a2/src/Scaffolding/VS.Web.CG.Mvc/Identity/IdentityGeneratorTemplateModelBuilder.cs#L670

It makes sense why the generator works if I do not specify the -dc option. It does not try to find it's type so FindExistingType is not called.

https://github.com/dotnet/Scaffolding/blob/f32d610bc72416fd6b5719829db280484848c7a2/src/Scaffolding/VS.Web.CG.Mvc/Identity/IdentityGeneratorTemplateModelBuilder.cs#L93

This function is the culprit.

Grizzlly commented 7 months ago

This has been reported by others too in the original issue thread. Any updates?

gt4Rhys commented 7 months ago

I encountered a similar issue when scaffolding and using extension classes.

My workaround was to change the access modifier of my extension classes to internal instead of public.

zacuke commented 5 months ago

I ran into this problem trying to scaffold some CRUD pages. Changing extension classes to internal instead of public worked. Thanks for the tip.