dotnet / templating

This repo contains the Template Engine which is used by dotnet new
https://docs.microsoft.com/dotnet/
MIT License
1.63k stars 369 forks source link

"dotnet new" fails to install a new template with "node_modules" with "Error: Value cannot be null" message #1498

Closed livarcocc closed 6 years ago

livarcocc commented 6 years ago

From @dance2die on April 4, 2018 1:36

Steps to reproduce

  1. Create a new dotnet new SPA project, react.
    
    dance2die@LELOUCH c:\misc\sources\github
    > dotnet new react -n ThisFails
    The template "ASP.NET Core with React.js" was created successfully.

Processing post-creation actions... ... Manual instructions: Run "npm install"

2. Go to the newly created project.
```bash
dance2die@LELOUCH c:\misc\sources\github
> cd ThisFails
  1. Install NPM packages.
    dance2die@LELOUCH c:\misc\sources\github\ThisFails
    > npm install
    npm WARN read-shrinkwrap This version of npm is compatible with lockfileVersion@1, but npm-shrinkwrap.json was generated for lockfileVersion@0. I'll try to do my best with it!
    ...
    added 546 packages in 136.886s
  2. Add a new template folder, .template.config and a new configuration file, template.json.
  3. Copy the following configuration to template.json file.
    {
    "$schema": "http://json.schemastore.org/template",
    "author": "Sung Kim",
    "classifications": ["Web"],
    "name": "BadTemplate",
    "identity": "BadTemplate",
    "shortName": "badtemplate",
    "tags": {
    "language": "C#",
    "type": "project"
    },
    "sourceName": "BadTemplate",
    "preferNameDirectory": true
    }
  4. Go up one directory.
    dance2die@LELOUCH c:\misc\sources\github\ThisFails
    > cd ..
  5. Install the template, ThisFails.
    dance2die@LELOUCH c:\misc\sources\github
    > dotnet new -i .\ThisFails
    Error: Could not install ".\ThisFails".
    Error: Value cannot be null.
    Parameter name: key
    ...
    Examples:
    dotnet new mvc --auth Individual
    dotnet new mstest
    dotnet new --help

    Expected behavior

    Go to the ThisFails folder and delete node_modules folder.

    dance2die@LELOUCH c:\misc\sources\github
    > cd ThisFails
    dance2die@LELOUCH c:\misc\sources\github\ThisFails
    > rmdir /S /Q node_modules

    Go up one directory

    dance2die@LELOUCH c:\misc\sources\github\ThisFails
    > cd ..

    Installing works without an error.

    dance2die@LELOUCH c:\misc\sources\github
    > dotnet new -i .\ThisFails
    Templates                                         Short Name       Language          Tags
    --------------------------------------------------------------------------------------------------------
    ...
    BadTemplate                                       badtemplate      [C#]              Web
    ...
    Examples:
    dotnet new mvc --auth Individual
    dotnet new console
    dotnet new --help

Actual behavior

Fails with following error message.

dance2die@LELOUCH c:\misc\sources\github                                                                
> dotnet new -i .\ThisFails                                                                             
Error: Could not install ".\ThisFails".                                                                 
Error: Value cannot be null.                                                                            
Parameter name: key                                                                                     
Templates                                         Short Name       Language          Tags               
--------------------------------------------------------------------------------------------------------
Console Application                               console          [C#], F#, VB      Common/Console     
Class library                                     classlib         [C#], F#, VB      Common/Library     
Unit Test Project                                 mstest           [C#], F#, VB      Test/MSTest        
xUnit Test Project                                xunit            [C#], F#, VB      Test/xUnit         
BadTemplate                                       badtemplate      [C#]              Web                
ASP.NET Core Empty                                web              [C#], F#          Web/Empty          
ASP.NET Core Web App (Model-View-Controller)      mvc              [C#], F#          Web/MVC            
ASP.NET Core Web App                              razor            [C#]              Web/MVC/Razor Pages
ASP.NET Core with Angular                         angular          [C#]              Web/MVC/SPA        
ASP.NET Core with React.js                        react            [C#]              Web/MVC/SPA        
ASP.NET Core with React.js and Redux              reactredux       [C#]              Web/MVC/SPA        
ASP.NET Core Web API                              webapi           [C#], F#          Web/WebAPI         
global.json file                                  globaljson                         Config             
Nuget Config                                      nugetconfig                        Config             
Web Config                                        webconfig                          Config             
Solution File                                     sln                                Solution           
Razor Page                                        page                               Web/ASP.NET        
MVC ViewImports                                   viewimports                        Web/ASP.NET        
MVC ViewStart                                     viewstart                          Web/ASP.NET        

Examples:                                                                                               
    dotnet new mvc --auth Individual                                                                    
    dotnet new mstest                                                                                   
    dotnet new --help                                                                                   

Environment data

dance2die@LELOUCH c:\misc\sources\github\ThisFails   
> dotnet --info                                      
.NET Command Line Tools (2.0.2)                      

Product Information:                                 
 Version:            2.0.2                           
 Commit SHA-1 hash:  a04b4bf512                      

Runtime Environment:                                 
 OS Name:     Windows                                
 OS Version:  10.0.16299                             
 OS Platform: Windows                                
 RID:         win10-x64                              
 Base Path:   C:\Program Files\dotnet\sdk\2.0.2\     

Microsoft .NET Core Shared Framework Host            

  Version  : 2.0.0                                   
  Build    : e8b8861ac7faf042c87a5c2f9f2d04c98b69f28d

Copied from original issue: dotnet/cli#8989

dance2die commented 6 years ago

Thank you, @livarcocc for migrating the issue 🙏 And I am sorry to cause you a trouble 😥 for opening an issue on a wrong project.

mlorbetske commented 6 years ago

Thanks for reporting this, we'll take a look

mlorbetske commented 6 years ago

TL;DR - the particular node modules that have been added to the project contain a template.json file

In newer CLIs, the crash issue has been fixed & some diagnostics have been added (you can get a better feel for what's going wrong by specifying the --trace:authoring flag when installing the package). Unfortunately, it doesn't make this particular case much clearer, we end up inferring enough of the settings to make it through the basic validation that if the template could be installed it could run (though it probably wouldn't do anything of interest). Things fall down though when it finally tries to register it - the "identity" property is missing from the file, causing it to null reference out when adding it to the in-memory cache before writing it to disk.

There's another item (#1492) that also touches on this topic. I'm preparing a PR now to:

dance2die commented 6 years ago

Thank you @mlorbetske. And I will also try --trace:authoring with the new CLI should I come up with an issue next time.

dance2die commented 6 years ago

Thank you @mlorbetske . Would the next release of dotnet CLI reflect the update?

mlorbetske commented 6 years ago

The CLI after 2.1.300-preview2 should contain the update (2.1.3xx)

Get Outlook for iOShttps://aka.ms/o0ukef


From: Sung Kim notifications@github.com Sent: Tuesday, April 10, 2018 7:39:06 PM To: dotnet/templating Cc: Mike Lorbetske; Mention Subject: Re: [dotnet/templating] "dotnet new" fails to install a new template with "node_modules" with "Error: Value cannot be null" message (#1498)

Thank you @mlorbetskehttps://github.com/mlorbetske . Would the next release of dotnet CLI reflect the update?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHubhttps://github.com/dotnet/templating/issues/1498#issuecomment-380308102, or mute the threadhttps://github.com/notifications/unsubscribe-auth/ABWXUeamL6BMbNOHtvBjs1BWAkadlaQyks5tnWzKgaJpZM4TGDa4.

DeluxZ commented 5 years ago

Bumping into this issue with CLI 2.1.500-preview-009335 and installing a template with a node_modules directory. I'm also trying to exclude the node_modules directory in the exclude property of the template.json. Doesn't help ☹.

Is this already in the CLI version I'm using?

C:\Projects> dotnet -h
.NET Command Line Tools (2.1.500-preview-009335)
Usage: dotnet [runtime-options] [path-to-application] [arguments]
dominikmontada commented 5 years ago

Encountering this issue with version 2.1.502. Any suggestions?

> dotnet -h
.NET Command Line Tools (2.1.502)
mlorbetske commented 5 years ago

@DeluxZ excluding it in template.json won't help as the directory will be present in the nupkg. If this is the same issue where a template.json exists in the nupkg but isn't a valid template, the only solution would be to remove it from the nupkg (possibly by excluding it from the project file used to create the package).

@dominikmontada could you elaborate on what you're seeing?

dominikmontada commented 5 years ago

@mlorbetske Sure thing. I set up a minimal example with vue-cli version 2 here, but you can also create this youself by running vue init webpack and then creating the template config yourself.

After cloning, run npm install, followed by dotnet new -i . from inside the directory. It will fail with

Error: Could not install ".".                                                                 
Error: Value cannot be null.                                                                            
Parameter name: key  

Deleting the node_modules folder remedies the problem.

I included a global.json with the SDK version set to 2.1.502. With the newest version, 2.2.101, I am unable to reproduce the problem.