Scaffolding: dotnet aspnet-codegenerator not able to generate any templates in WSL dotnet CLI

Created on 18 Nov 2020  路  6Comments  路  Source: dotnet/Scaffolding


Issue moved from dotnet/sdk#14546

  • Please respond to @DJBrunelle.

_From @DJBrunelle on Saturday, November 14, 2020 9:00:11 PM_

I'm currently learning how to use the dotnet CLI in WSL 2 (Ubuntu 20) for building web API's. I'm attempting to use the scaffolding tool aspnet-codegenerator, but it can't find any templates to generate from. I've gone through many forums and have made sure to add the Microsoft.VisualStudio.Web.CodeGeneration.Design and other packages, made sure the aspnet-codegenerator tool was installed globally, and provided the csproj file path to the tool. Nothing seems to have changed the output of the basic dotnet aspnet-codegenerator -h command.

I'm using ASP.NET core version 5, and I'm trying to generate scaffolds inside a new project generated from the command dotnet new webapi

apitest.csproj:

<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>net5.0</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="5.0.0" NoWarn="NU1605" />
    <PackageReference Include="Microsoft.AspNetCore.Authentication.OpenIdConnect" Version="5.0.0" NoWarn="NU1605" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="5.0.0">
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
      <PrivateAssets>all</PrivateAssets>
    </PackageReference>
    <PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="5.0.0" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="5.0.0" />
    <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="5.0.0" />
    <PackageReference Include="Swashbuckle.AspNetCore" Version="5.6.3" />
  </ItemGroup>

</Project>

Here is the output when I try to run the tool:

~/code/testing/apitest -> dotnet aspnet-codegenerator -h


Usage: aspnet-codegenerator [arguments] [options]

Arguments:
  generator  Name of the generator. Check available generators below.

Options:
  -p|--project             Path to .csproj file in the project.
  -n|--nuget-package-dir   
  -c|--configuration       Configuration for the project (Possible values: Debug/ Release)
  -tfm|--target-framework  Target Framework to use. (Short folder name of the tfm. eg. net46)
  -b|--build-base-path     
  --no-build               

No code generators are available in this project.Add Microsoft.VisualStudio.Web.CodeGeneration.Design package to the project as a NuGet package reference.

RunTime 00:00:01.02

Using this tutorial, I get the following output:

~/code/testing/apitest -> dotnet aspnet-codegenerator controller -name TodoItemsController -async -api -m TodoItem -dc TodoContext -outDir Controllers
Building project ...
Finding the generator 'controller'...
Running the generator 'controller'...
Attempting to compile the application in memory.
Attempting to figure out the EntityFramework metadata for the model and DbContext: 'TodoItem'
A file matching the name ApiControllerWithContext.cshtml was not found within any of the folders: 
   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)
RunTime 00:00:04.68

Most helpful comment

@jungho I rebuilt Scaffolding tool with fix.

In file: src/VS.Web.CG.Msbuild/ProjectContextWriter.cs, DeserializePackages() returns dependencies paths.

The paths are eg. ~/.nuget/packages/Microsoft.AspNetCore.Identity.UI, but (probably only on linux) directories are named lower case (microsoft.aspnetcore.identity.ui instead Microsoft.AspNetCore.Identity.UI).

Tool checks does directory exist and returns... (wtf?) empty string if doesn't. So scaffonding cannot find dependencies.

All 6 comments

The problem occurs because probably command used by aspnet-codegenerator:

msbuild {projectPath} "/t:EvaluateProjectInfoForCodeGeneration" "/p:OutputFile={tmpFile};CodeGenerationTargetLocation={targetLocation};Configuration={configuration}"

return invalid data.

PackageDependencies have empty Path value, so it can't find {path}/Generators/ParameterDefinitions/*.json files.
Path is eg. Generators/ParametesDefinitions/controller.json instead /home/{user}/.nuget/packages/microsoft.visualstudio.web.codegenerators.mvc/5.0.0/Generators/ParametesDefinitions/controller.json

OS Platform: Arch Linux x64
.NET SDK: 5.0.100

OK I found real problem. But it's so late. I gonna write about it tomorow. It work finally... :D

@dkja How did you solve this issue? :-)

@jungho I rebuilt Scaffolding tool with fix.

In file: src/VS.Web.CG.Msbuild/ProjectContextWriter.cs, DeserializePackages() returns dependencies paths.

The paths are eg. ~/.nuget/packages/Microsoft.AspNetCore.Identity.UI, but (probably only on linux) directories are named lower case (microsoft.aspnetcore.identity.ui instead Microsoft.AspNetCore.Identity.UI).

Tool checks does directory exist and returns... (wtf?) empty string if doesn't. So scaffonding cannot find dependencies.

@dkja @jungho @DJBrunelle @sfoslund
I have a fix for this issue out with a new 3.1.5 and 5.0.2 package out.
Use dotnet tool update dotnet-aspnet-codegenerator -g --version 3.1.5/5.0.2 depending on if you are targeting .netcoreapp3.1 or net5.0. I would like it if someone could confirm the fix before I close the issue.

Apologize for the lengthy delay on this fix and appreciate all the feedback.

Closing. Thanks all.

Was this page helpful?
0 / 5 - 0 ratings