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

Angular never searches for zone.js in the correct path

Open its-dibo opened this issue 1 year ago • 8 comments

Command

build

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

I have a monorepo, one of its workspaces is an Angular workspace. and I use pnpm to install the dependencies each workspace has its own node_modules folder.

when I start building this app (the Angular workspace), it fails to find zone.js it should find it inside node_modules of the Angular app

this debug info may be useful as you can see it looks in:

  • dibo/node_modules/zone.js (the monorepo's top-level root)
  • dibo/apps/mobarmegeen/mobarmegeen-ui/zone.js

but never looks in the correct path which is dibo/apps/mobarmegeen/mobarmegeen-ui/node_modules/zone.js

debug info
 [DEBUG] Resolving import "zone.js/node" in directory "/home/dibo/dev/@its-dibo/dibo" of type "import-statement"

  Checking for package alias matches
    Failed to find any package alias matches
  Read 25 entries for directory "/"
  Read 1 entry for directory "/home"
  Read 25 entries for directory "/home/dibo"
  Read 4 entries for directory "/home/dibo/dev"
  Read 3 entries for directory "/home/dibo/dev/@its-dibo"
  The file "/home/dibo/dev/@its-dibo/dibo/package.json" exists
  Read 40 entries for directory "/home/dibo/dev/@its-dibo/dibo"
  Read 40 entries for directory "/home/dibo/dev/@its-dibo/dibo"
  Searching for "zone.js/node" in "node_modules" directories starting from "/home/dibo/dev/@its-dibo/dibo"
    Matching "zone.js/node" against "paths" in
  "/home/dibo/dev/@its-dibo/dibo/apps/mobarmegeen/mobarmegeen-ui/tsconfig.app.json"
      Using "/home/dibo/dev/@its-dibo/dibo/apps/mobarmegeen/mobarmegeen-ui" as "baseURL"
    Attempting to load "/home/dibo/dev/@its-dibo/dibo/apps/mobarmegeen/mobarmegeen-ui/zone.js/node" as a file
      Failed to read directory "/home/dibo/dev/@its-dibo/dibo/apps/mobarmegeen/mobarmegeen-ui/zone.js": open
  /home/dibo/dev/@its-dibo/dibo/apps/mobarmegeen/mobarmegeen-ui/zone.js: no such file or directory
    Attempting to load "/home/dibo/dev/@its-dibo/dibo/apps/mobarmegeen/mobarmegeen-ui/zone.js/node" as a directory
      Read 40 entries for directory "/home/dibo/dev/@its-dibo/dibo"
      Read 2 entries for directory "/home/dibo/dev/@its-dibo/dibo/apps"
      Read 2 entries for directory "/home/dibo/dev/@its-dibo/dibo/apps/mobarmegeen"
      The file "/home/dibo/dev/@its-dibo/dibo/apps/mobarmegeen/mobarmegeen-ui/package.json" exists
      Read 16 entries for directory "/home/dibo/dev/@its-dibo/dibo/apps/mobarmegeen/mobarmegeen-ui"
      Failed to read directory "/home/dibo/dev/@its-dibo/dibo/apps/mobarmegeen/mobarmegeen-ui/zone.js": open
  /home/dibo/dev/@its-dibo/dibo/apps/mobarmegeen/mobarmegeen-ui/zone.js: no such file or directory
      Failed to read directory "/home/dibo/dev/@its-dibo/dibo/apps/mobarmegeen/mobarmegeen-ui/zone.js"
      Failed to read directory "/home/dibo/dev/@its-dibo/dibo/apps/mobarmegeen/mobarmegeen-ui/zone.js/node"
    Parsed package name "zone.js" and package subpath "./node"
    Checking for a package in the directory "/home/dibo/dev/@its-dibo/dibo/node_modules/zone.js/node"
    Read 40 entries for directory "/home/dibo/dev/@its-dibo/dibo"
    Read 76 entries for directory "/home/dibo/dev/@its-dibo/dibo/node_modules"
    Failed to read directory "/home/dibo/dev/@its-dibo/dibo/node_modules/zone.js": open
  /home/dibo/dev/@its-dibo/dibo/node_modules/zone.js: no such file or directory
    Failed to read directory "/home/dibo/dev/@its-dibo/dibo/node_modules/zone.js"
    Attempting to load "/home/dibo/dev/@its-dibo/dibo/node_modules/zone.js/node" as a file
      Failed to read directory "/home/dibo/dev/@its-dibo/dibo/node_modules/zone.js": open
  /home/dibo/dev/@its-dibo/dibo/node_modules/zone.js: no such file or directory
    Attempting to load "/home/dibo/dev/@its-dibo/dibo/node_modules/zone.js/node" as a directory
      Failed to read directory "/home/dibo/dev/@its-dibo/dibo/node_modules/zone.js"
      Failed to read directory "/home/dibo/dev/@its-dibo/dibo/node_modules/zone.js/node"

✘ [ERROR] Could not resolve "zone.js/node"

    angular:polyfills-server:angular:polyfills-server:1:7:
      1 │ import 'zone.js/node';
        ╵        ~~~~~~~~~~~~~~

  You can mark the path "zone.js/node" as external to exclude it from the bundle, which will remove
  this error and leave the unresolved path in the bundle.

Minimal Reproduction

https://github.com/its-dibo/issue-zonejs

1- install via pnpm pnpm install if pnpm is not installed, simply, run npm I -g pnpm

2- run the app npm start

note: don't use any other package manager, as they hoist the packages of the app to the root of the monorepo. .

Exception or Error

No response

Your Environment

Angular CLI: 17.0.3
Node: 20.10.0
Package Manager: yarn 1.22.22
OS: linux x64

Angular: undefined
... 

Package                      Version
------------------------------------------------------
@angular-devkit/architect    0.1700.3 (cli-only)
@angular-devkit/core         17.0.3 (cli-only)
@angular-devkit/schematics   17.0.3 (cli-only)
@schematics/angular          17.0.3 (cli-only)
rxjs   


its-dibo avatar Apr 16 '24 03:04 its-dibo

Polyfills are resolved from the workspace root because they're referenced in the angular.json file. Typically, in an Angular CLI workspace, it's expected to have a single node_modules directory at the top level of the workspace.

alan-agius4 avatar Apr 16 '24 14:04 alan-agius4

Polyfills are resolved from the workspace root

that is what I need, but the actual behavior is that it searches in the monorepo's root rather than the workspace's root.

note that the Angular workspace is part of a monorepo.

its-dibo avatar Apr 16 '24 21:04 its-dibo

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 Apr 17 '24 06:04 alan-agius4

Can you setup a minimal repro please?

I updated the description with the minimal repro and steps to reproduction

its-dibo avatar Apr 17 '24 18:04 its-dibo

Hello @its-dibo, thank you for your reproduction. It seems that this is an NX managed monorepo, so its operation differs slightly from an Angular CLI workspace due to the presence of multiple nested projects.json and an nx.json file. In an NX monorepo, however the workspace root is where your nx.json is located, so the polyfills and other node_modules entries are resolved relative to that location.

alan-agius4 avatar Apr 18 '24 08:04 alan-agius4

but, as you know, pnpm creates s node_module folder for each package separately (hard links) that's mean that polyfills can never be found, as they'll never be presents in the same level of nx.json

its-dibo avatar Apr 18 '24 18:04 its-dibo

Had a brief discussion about this within the team and decided it probably makes sense to resolve polyfills from the project root. Projects should be relatively self-contained and have all the information to build themselves. They shouldn't need to rely on workspace-level information here, so it makes sense to be project -relative.

This is arguably a bug fix, but it is an observable change and probably safer to do in a major version. Let's include this in v19.

dgp1130 avatar Jun 12 '24 18:06 dgp1130