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

Confusing error when preserveSymlinks is missing. #55709

Closed
axos88 opened this issue May 7, 2024 · 2 comments
Closed

Confusing error when preserveSymlinks is missing. #55709

axos88 opened this issue May 7, 2024 · 2 comments

Comments

@axos88
Copy link

axos88 commented May 7, 2024

Which @angular/* package(s) are the source of the bug?

router

Is this a regression?

No

Description

Repro:

  1. git clone a primeng library
  2. npm run build:lib
  3. cd myproject
  4. npm i ../../primeng/dist
  5. import MenubarModule
  6. ng build

Observe: modules cannot be resolved

  1. Try workaround by adding:
    "paths": {
      "primeng/*": ["../node_modules/primeng/*/public_api"]
    },
  1. ng build succeeds
  2. Open application, observe NG0203
main.ts:27 ERROR Error: NG0203: inject() must be called from an injection context such as a constructor, a factory function, a field initializer, or a function used with `runInInjectionContext`. Find more at https://angular.io/errors/NG0203
  1. spend days debugging the issue
  2. solved by adding preserveSymlinks: true.

Please provide a link to a minimal reproduction of the bug

No response

Please provide the exception or error you saw

main.ts:27 ERROR Error: NG0203: inject() must be called from an injection context such as a constructor, a factory function, a field initializer, or a function used with `runInInjectionContext`. Find more at https://angular.io/errors/NG0203

Please provide the environment you discovered this bug in (run ng version)

Angular CLI: 17.3.6
Node: 20.12.2
Package Manager: npm 10.5.0
OS: linux x64

Angular: 
... 

Package                      Version
------------------------------------------------------
@angular-devkit/architect    0.1703.6 (cli-only)
@angular-devkit/core         17.3.6 (cli-only)
@angular-devkit/schematics   17.3.6 (cli-only)
@schematics/angular          17.3.6 (cli-only)

Anything else?

preserveSymlinks is not well documented (even after the solution I have no idea what it actually preserves), and the error message anything but points to the missing configuration. A more helpful error message would be great!

@JoostK
Copy link
Member

JoostK commented May 7, 2024

Improving this failure mode is being tracked in angular/angular-cli#26774.

Without preserveSymlinks, the real path of a symlink is being used instead of retaining the original path. For example, consider the following structure:

/lib/node_modules/@angular/core/...
/lib/package.json
/app/node_modules/@angular/core/...
/app/lib -> /lib
/app/package.json

Then, a path like /app/lib/entrypoint.ts either remains as is (preserveSymlinks=true) , or is replaced with /lib/entrypoint.ts (preserveSymlinks=false). This then affects module resolution, as it either finds /app/node_modules or /lib/node_modules. in the latter case introducing a secondary copy of @angular/core. That results in a runtime that gets confused, as the two runtimes are not aware of each other and don't have access to the proper state.

Closing as tracked in angular/angular-cli#26774

@JoostK JoostK closed this as not planned Won't fix, can't repro, duplicate, stale May 7, 2024
@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Jun 7, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants