platform
platform copied to clipboard
Schematics: Schematic fails with cryptic error when a project is not specified
Copied from: https://github.com/nrwl/nx/issues/1545
Please make sure you have read the submission guidelines before posting an issue
Expected Behavior
Please describe the behavior you are expecting
When running the below command without a --project
flag, I expect the error message to reflect the missing flag, instead of the message Cannot read property 'root' of undefined
, as it is non-descriptive. The command I ran:
$ ng generate @ngrx/schematics:store State --root --module app.module.ts
The message could be improved by something like:
Could not determine the Angular project. Please provide a '--project' flag.
Current Behavior
What is the current behavior?
The current behavior is that when running the above command, it does not generate the expected files. Instead, it presents the non-descriptive error message Cannot read property 'root' of undefined
. When running the below command, it does generate the expected files.
$ ng generate @ngrx/schematics:store State --root --module app.module.ts --project my-angular-project
Note that my-angular-project
should obviously be defined in you angular.json
file.
Failure Information (for bugs)
Please help provide information about the failure if this is a bug. If it is not a bug, please remove the rest of this template.
Steps to Reproduce
Please provide detailed steps for reproducing the issue.
- Create a new Nx Workspace, say
my-workspace
and be sure to include an Angular project - Step 1 generates the Angular files. Now, delete the full Angular app and create one with a different name, say
my-different-angular-project
. - Now, generate the NgRx root store using the command:
$ ng generate @ngrx/schematics:store State --root --module app.module.ts
- Now, you should see the error.
Context
Please provide any relevant information about your setup:
- version of Nx used "@nrwl/workspace": "8.2.0"
- version of Angular CLI used @angular/[email protected]
-
angular.json
configuration Can't share - version of Angular DevKit used "@angular-devkit/build-angular": "^0.800.0",
- 3rd-party libraries and their versions @ngrx/schematics": "^8.0.1"
- and most importantly - a use-case that fails See the above reproduction steps.
A minimal reproduce scenario using allows us to quickly confirm a bug (or point out coding problem) as well as confirm that we are fixing the right problem.
Failure Logs
Please include any relevant log snippets or files here.
Other
Any other relevant information that will help us help you.
According to this issue, it could be caused by a mismatch between the Angular Project name in angular.json
and the name used in package.json
, but I'm not sure about that.
Hi @brandonroberts,
I've done some testing and I'm getting some different results than in the description. I think this is due to the fact that this is with the older version then I'm testing right now.
I've done the setup removed the first generated app, and created a second one. This is still similar as you explained.
The first thing I've experienced is that I needed to remove the defaultproject
, otherwise I will have the following error:
npx ng generate @ngrx/schematics:store State --root --module app.module.ts
Project 'first-app' could not be found in the workspace.
The error message is a valid message but I believe this is not the situation you would like to be resolved. After seeing this error message I've removed the defaultProject
from the angular.json
. This resulted in the following error:
npx ng generate @ngrx/schematics:store State --root --module app.module.ts
Specified module path /apps/first-app-e2e/src/app/app.module.ts does not exist
The error above is also valid as we have a function in the utility.ts
that gets the first project there is from the angular.json
file when no default project is provided (or the --project
flag).
As I cannot reproduce this issue I just looked at the code and thought of making the following change.
export function getProject(
host: Tree,
options: { project?: string | undefined; path?: string | undefined }
): WorkspaceProject {
const workspace = getWorkspace(host);
if (!options.project) {
options.project =
workspace.defaultProject !== undefined
? workspace.defaultProject
: Object.keys(workspace.projects)[0];
}
return workspace.projects[options.project];
}
to:
export function getProject(
host: Tree,
options: { project?: string | undefined; path?: string | undefined }
): WorkspaceProject {
const workspace = getWorkspace(host);
if (!options.project) {
if(workspace.defaultProject === undefined){
throw new SchematicsException(`Could not determine the Angular project. Please provide a '--project' flag.`);
}
options.project =
workspace.defaultProject !== undefined
? workspace.defaultProject
: Object.keys(workspace.projects)[0];
}
return workspace.projects[options.project];
}
However, I'm not quite happy with this, because Object.keys(workspace.projects)[0]
still could be the one you would like to select by default and with this change, you are forcing people to specifically use the --project
file. Could anybody assist me in resolving this issue in the correct way or maybe describe a different approach? I'm also willing to take this offline via twitter or any other medium so that this thread is not filled with many comments.
@Jefiozie In my understanding, you should use firstly the project provided through the CLI, if not the default one, and lastly the first one as fallback. Taking that into consideration, I'd propose something like:
export function getProject(
host: Tree,
options: { project?: string | undefined; path?: string | undefined }
): WorkspaceProject {
const workspace = getWorkspace(host);
if (options.project) {
const project = workspace.projects[options.project];
if (project) {
return project;
} else {
throw new SchematicsException(`Selected Angular project ${options.project} doesn't exist. Please provide a valid '--project' flag.`);
}
}
if (workspace.defaultProject) {
const project = workspace.projects[workspace.defaultProject];
if (project) {
options.project = workspace.defaultProject;
return project;
} else {
throw new SchematicsException(`Default Angular project ${workspace.defaultProject} doesn't exist. Fix your configuration.`);
}
}
const firstProject: string = Object.keys(workspace.projects)[0];
options.project = firstProject;
return workspace.projects[options.project];
}
However, I'd have to say that this is such a basic a common problem that it'd be worth to be brought into NX framework
@Jefiozie if you can't make it I'm happy to submit a PR
@vsavkin do you feel this would be worth to be shipped as a part of the workspace API in NX?
@Jefiozie @samuelfernandez this issue is still opened. I will be more than happy to submit a PR as you have suggested above.
@Jefiozie @samuelfernandez this issue is still opened. I will be more than happy to submit a PR as you have suggested above.
@patelvimal I won't have much time for this soon
@Jefiozie @samuelfernandez this issue is still opened. I will be more than happy to submit a PR as you have suggested above.
@patelvimal I won't have much time for this soon
No issues, I will be happy to raise a PR for this.
@patelvimal be my guest to pick this one up :)
Closing as resolved
I have the same issue in v16 ... what is resolved exactly ?