angular / angular-cli

CLI tool for Angular
https://cli.angular.io
MIT License
26.76k stars 11.97k forks source link

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

Open sergey-morenets opened 1 year ago

sergey-morenets commented 1 year ago

Command

generate

Is this a regression?

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

alan-agius4 commented 1 year ago

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.

Michael-7 commented 1 year ago

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.

alan-agius4 commented 1 year ago

@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.

Michael-7 commented 1 year ago

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

Michael-7 commented 1 year ago

@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

alan-agius4 commented 1 year ago

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.

Michael-7 commented 1 year ago

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!

sergey-morenets commented 1 year ago

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.

sherlock1982 commented 1 year ago

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

vmachacek commented 1 year ago

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..

nsmithdev commented 11 months ago

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

image

radavel commented 8 months ago

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}

wbuck commented 6 months ago

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.