Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect Error About Locating @angular/core in Yarn Workspaces Monorepo [Regression] #990

Closed
michaelfaith opened this issue Aug 19, 2021 · 9 comments · Fixed by #991
Closed

Comments

@michaelfaith
Copy link
Contributor

michaelfaith commented Aug 19, 2021

🐛 Bug Report

We have a monorepo with yarn workspaces, and Angular 12, Jest 27. This all worked with 9.0.4, but updating to 9.0.6 is incorrectly throwing an error about not being able to locate @angular/core (which is at the root node_modules, not the local node_modules). I'm assuming it's related to this change: c35d3f2
Rolling back to 9.0.4 fixes it.

Expected behavior

Should run in monorepo environment.

Error log:

# content of error stacktrace :
Error: Cannot locate the '@angular/core' directory, resolved as C:\Users\<user>\src\fabric\projects\components\node_modules\@angular\core. Please make sure you are running 'ngcc-jest-processor.js' from root level of your project
    at Object.<anonymous> (C:\Users\<user>\src\fabric\node_modules\jest-preset-angular\build\utils\ngcc-jest-processor.js:42:11)
    at Module._compile (internal/modules/cjs/loader.js:1068:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1097:10)
    at Module.load (internal/modules/cjs/loader.js:933:32)
    at Function.Module._load (internal/modules/cjs/loader.js:774:14)
    at Module.require (internal/modules/cjs/loader.js:957:19)
    at require (internal/modules/cjs/helpers.js:88:18)
    at Object.<anonymous> (C:\Users\<user>\src\fabric\node_modules\jest-preset-angular\ngcc-jest-processor.js:1:18)
    at Module._compile (internal/modules/cjs/loader.js:1068:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1097:10)
error Command failed with exit code 1.

envinfo

System:
    OS: Windows 10

Npm packages:
    jest: 27.0.6
    jest-preset-angular: 9.0.6
    typescript: 4.3.5
@michaelfaith michaelfaith added Bug Report Needs Repo Need a minimium repository to reproduce the problem Needs Triage labels Aug 19, 2021
@ahnpnl
Copy link
Collaborator

ahnpnl commented Aug 19, 2021

We introduced this to inform user that the util script needs to be explicitly run at root of the project to be able to run ngcc correctly.

It's either can be a warning or throw error, that I wasn't sure so I made it to throw error.

@michaelfaith
Copy link
Contributor Author

Error or warning isn't really the issue, so much as it's assuming the node_modules is adjacent to the package.json, and in yarn workspace setup it isn't. So it's mis-identifying the location that it's looking for.

@ahnpnl
Copy link
Collaborator

ahnpnl commented Aug 19, 2021

in the case of yarn workspace, the util script invokes ngcc but ngcc doesn't process node_modules correctly (which I tested with your project). How do we inform users about that?

@michaelfaith
Copy link
Contributor Author

michaelfaith commented Aug 19, 2021

It would probably be ok to just change it to warning and relax the language a bit. Something like "Warning: Could not locate @angular/core to run ngcc automatically. Please be sure to run ngcc prior to testing." Something that let's people know in case they do have a problematic setup, but doesn't error out on people who don't? Either that or improve the logic that tries to find the library

@ahnpnl
Copy link
Collaborator

ahnpnl commented Aug 19, 2021

I think changing to warning is the only way. We invoke ngcc directly from @angular/compiler-cli which we can't change the logic how it looks for node_modules :)

@michaelfaith
Copy link
Contributor Author

michaelfaith commented Aug 19, 2021

We invoke ngcc directly from @angular/compiler-cli which we can't change the logic how it looks for node_modules :)

I meant this logic, where it just defaults to cwd and then appends node_modules. It doesn't walk up the tree, or anything more expansive.

const nodeModuleDirPath = findNodeModulesDirectory(process.cwd());
const canRunNgcc =
  !process.argv.find((arg) => IGNORE_ARGS.includes(arg)) && existsSync(join(nodeModuleDirPath, '@angular', 'core'));
function findNodeModulesDirectory(startPoint: string): string {
  let current = startPoint;
  while (dirname(current) !== current) {
    const nodePath = join(current, 'node_modules');
    if (existsSync(nodePath)) {
      return nodePath;
    }

    current = dirname(current);
  }

  throw new Error(
    `Cannot locate the 'node_modules' directory. Please make sure you are running jest from root level of your project`,
  );
}

@ahnpnl
Copy link
Collaborator

ahnpnl commented Aug 19, 2021

Oh ye that one is assuming that this script is run from top of the project but doesn’t take into account of Yarn workspace.

I’m curious if ngcc works with cwd instead of from root? Probably using require.resolve on @angular/compiler-cli to find real path to node_modules and fetch that to the script.

@michaelfaith
Copy link
Contributor Author

michaelfaith commented Aug 19, 2021

Yeah, I don't believe it does. For example, if ngcc is set as a postinstall step on the lower package.json it doesn't find the libraries. I have to add it as a postinstall at the top/root package.json for it to find everything.

@michaelfaith
Copy link
Contributor Author

@ahnpnl thanks for the quick work on this. Really appreciate it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants