angular-cli icon indicating copy to clipboard operation
angular-cli copied to clipboard

Data path "" must have required property 'project'.

Open sergey-morenets opened this issue 1 year ago • 13 comments

Command

generate

Is this a regression?

  • [X] Yes, this behavior used to work in the previous version

The previous version in which this bug was not present was

No response

Description

Hi

We have two projects that use Angular and Angular CLI. After we've upgraded both projects to Angular 16.x we can't use schematics for one of the projects (second project is fine). We tried to clear node_modules or cache folders but it didn't help.

Minimal Reproduction

Any time we try to use schematics we always receive an error:

ng generate class Test

Exception or Error

Schematic input does not validate against the Schema: {"name":"Test","path":"src/app/model"}
Errors:

  Data path "" must have required property 'project'.

Your Environment

Angular CLI: 16.1.0
Node: 20.3.0 (Unsupported)
Package Manager: yarn 3.5.1
OS: win32 x64

Angular: 16.1.1
... animations, cdk, common, compiler, compiler-cli, core, forms
... google-maps, localize, material, platform-browser
... platform-browser-dynamic, router

Package                         Version
---------------------------------------------------------
@angular-devkit/architect       0.1601.0
@angular-devkit/build-angular   16.1.0
@angular-devkit/core            16.1.0
@angular-devkit/schematics      16.1.0
@angular/cli                    16.1.0
@schematics/angular             16.1.0
rxjs                            7.8.1
typescript                      5.0.4

Anything else relevant?

No response

sergey-morenets avatar Jun 20 '23 03:06 sergey-morenets

This seems like a bug but we'll need to look at a reproduction to find and fix the problem. Can you setup a minimal repro please?

You can read here why this is needed. A good way to make a minimal repro is to create a new app via ng new repro-app and adding the minimum possible code to show the problem. Then you can push this repository to github and link it here.

This might be related to your directory structure so its really important to get an accurate repro to diagnose this.

alan-agius4 avatar Jun 22 '23 14:06 alan-agius4

Hey guys, I have the same issue here. Don't really understand why you would need to have 'project' in your path. My repo looks like this:

src
    app
        components
        pages
        services
        types
    assets
    environments

Can't do the ng g component languages command for example. Doesn't work in component or in the app folder.

My env:

Angular CLI: 16.1.0
Node: 18.16.0
Package Manager: npm 9.5.1
OS: darwin arm64

Angular: 16.1.1
... animations, common, compiler, compiler-cli, core, forms
... platform-browser, platform-browser-dynamic, router

Package                         Version
---------------------------------------------------------
@angular-devkit/architect       0.1601.0
@angular-devkit/build-angular   16.1.0
@angular-devkit/core            16.1.0
@angular-devkit/schematics      16.1.0
@angular/cli                    16.1.0
@schematics/angular             16.1.1
rxjs                            7.8.1
typescript                      5.1.3

I think it might have something to do with me adding extra projects - and renaming the current project - in the angular.json file and it now expecting I have a projects folder because before I did that it worked fine.

Michael-7 avatar Jun 23 '23 13:06 Michael-7

@Michael-7, a project is required so that the Angular CLI can determine in which directory to generate the files. The project value however is provided automatically. I am not sure why in this case it is not. As such we'd require a reproduction to investigate this further.

alan-agius4 avatar Jun 23 '23 14:06 alan-agius4

@alan-agius4 thanks! I'll try to make a reproduction in another repo.

Michael-7 avatar Jun 23 '23 14:06 Michael-7

@alan-agius4 I created a repro: https://github.com/Michael-7/angular-repro

The problem happens as soon as you add two "projects" in the angular.json file.

In the repro if you try the ng g component language command in the angular-repro-45/src/app folder for example, it gives the error

Michael-7 avatar Jun 23 '23 14:06 Michael-7

What is happening here is that the Angular CLI cannot determine which project you intent to run the schematic in (Due to the removal of defaultProject. We should definitely improve the error message here and explain what actions the users need to take.

You can either provide the project using the --project option or change the current working directory to a project directory.

alan-agius4 avatar Jun 23 '23 14:06 alan-agius4

Yes, it also cannot determine it because both projects have the same root. I need to generate 2 projects from the same root so it would never know what project to pick. So I will set a "defaultProject". It works when I add the --project <project-name>. Thanks for helping out!

Michael-7 avatar Jun 23 '23 14:06 Michael-7

Hi @alan-agius4

Yes, you're right. I found this project in my angular.json:

"project-e2e": {
      "root": "",
      "sourceRoot": "",
      "projectType": "application",
      "architect": {
        "e2e": {
          "builder": "@angular-devkit/build-angular:protractor",
          "options": {
            "protractorConfig": "./protractor.conf.js",
            "devServerTarget": "project:serve"
          }
        }
      }
    }

I didn't add it manually. I guess it was added automatically while creating new application (at least until Angular 11). Once I removed it from angular.json this issue doesn't reproduce any more. So I guess the error message should be more descriptive and states about multiple projects in angular.json.

sergey-morenets avatar Jun 23 '23 17:06 sergey-morenets

Do I get it correct that now it's not recommended to have two projects inside angular.json? Cause I have no idea how I can fix it for example when running from Webstorm

sherlock1982 avatar Aug 28 '23 10:08 sherlock1982

I have 3 projects in my angular.json and IDE has option for "Extract Component" refactoring which is great help, however it fails because the CLI fails, I could really utilize something like defaultProject..

vmachacek avatar Oct 20 '23 05:10 vmachacek

ng build has a nice error message but the default project would be even better for me.

image

nsmithdev avatar Nov 29 '23 14:11 nsmithdev

I had this issue trying to generate a new component, In my case I reviewed my angular.json file and I saw that I had 2 projects, so I just ran the following command using the first one project:

ng generate component {componentName} --project={projectName}

radavel avatar Feb 13 '24 17:02 radavel

We also have two projects defined with the same root directory. We recently updated to Angular 15 (which removed the defaultProject key) and started seeing the following error when attempting to generate some schematic via ng generate component User:

Data path "" must have required property 'project'.

Since the projects have the same root, changing directories does not work. Specifying the --project option to ng generage does work however.

I've also noticed that I can define schematics at the top level of the angular.json file. For each schematic I can specify a project option, for example:

{
    "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
    "version": 1,
    "newProjectRoot": "projects",
    "projects": { 
         "project1": {...},
         "project2": {...}
     },
    "schematics": { 
        "@schematics/angular:component": {
            "prefix": "app",
            "style": "scss",
            "project": "project1" <-- Added the project key
        },
    }
}

Adding the project key to the schematic also seems to work, i.e., ng generate component User now works as expected. In our case this makes sense since both projects share the same root directory. The downside with this is I have to specify a schematic for each schematic: component, directive, pipe, module, etc.

wbuck avatar Apr 18 '24 13:04 wbuck