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

Cannot destructure property 'pos' of 'file.referencedFiles[index]' as it is undefined.

Open vbraun opened this issue 1 year ago • 6 comments

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

compiler

Is this a regression?

No

Description

Incorrect imports can cause hard-to-debug errors like

$ npx ng build b-lib
[...]
Cannot destructure property 'pos' of 'file.referencedFiles[index]' as it is undefined.

when building libraries. Where do you even start to debug this? A bit more hand-holding would be nice.

There is a closed bug report at https://github.com/angular/angular/issues/57850 where a reproducer was requested but never delivered. Well here it is!

Please provide a link to a minimal reproduction of the bug

https://github.com/vbraun/angular-repro-57850

Please provide the exception or error you saw

$ npx ng build b-lib
Building Angular Package

------------------------------------------------------------------------------
Building entry point 'b-lib'
------------------------------------------------------------------------------
✖ Compiling with Angular sources in partial compilation mode.
Cannot destructure property 'pos' of 'file.referencedFiles[index]' as it is undefined.

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

Angular CLI: 20.3.8
Node: 22.20.0
Package Manager: npm 2.14.21
OS: linux x64
    

Angular: 20.3.9
... common, compiler, compiler-cli, core, forms
... platform-browser, router

Package                      Version
------------------------------------
@angular-devkit/architect    0.2003.8
@angular-devkit/core         20.3.8
@angular-devkit/schematics   20.3.8
@angular/build               20.3.8
@angular/cli                 20.3.8
@schematics/angular          20.3.8
ng-packagr                   20.3.0
rxjs                         7.8.2
typescript                   5.9.3
zone.js                      0.15.1

Anything else?

No response

vbraun avatar Oct 30 '25 15:10 vbraun

A similar issue was reported by #29450

JeanMeche avatar Oct 30 '25 15:10 JeanMeche

Note that #29450 was closed as "completed" but no actual fix delivered, only the issue reporter was helped along.

IMHO this is quite a usability wart as the import statements are generally auto-generated by the editor and getting it wrong then sends developers on day-long wild goose chases that could be avoided by a more useful error messages.

vbraun avatar Oct 31 '25 14:10 vbraun

The error is from within the TypeScript compiler and not caused Angular. Hence, it’s not actionable by use.

Previously, the TypeScript compiler did provide a clearer error message but for some reason it no longer does.

alan-agius4 avatar Oct 31 '25 14:10 alan-agius4

In tsconfig.js I changed my "paths" from:

    "paths": {
      "my-lib-1": ["./projects/my-lib-1/src/public-api.ts", "./dist/my-lib-1"],
      "my-lib-2": ["./projects/my-lib-2/src/public-api.ts", "./dist/my-lib-2"],
      "my-lib-3": ["./projects/my-lib-3/src/public-api.ts", "./dist/my-lib-3"],
      "shared": ["./projects/shared/src/public-api.ts", "./dist/shared"]
    },

Into:

    "paths": {
      "my-lib-1": ["./dist/my-lib-1", "./projects/my-lib-1/src/public-api.ts"],
      "my-lib-2": ["./dist/my-lib-2", "./projects/my-lib-2/src/public-api.ts"],
      "my-lib-3": ["./dist/my-lib-3", "./projects/my-lib-3/src/public-api.ts"],
      "shared": ["./dist/shared", "./projects/shared/src/public-api.ts"]
    },

For some reason, this worked... Hope it helps!

Sefibrah avatar Nov 27 '25 10:11 Sefibrah

For some reason, this worked... Hope it helps!

That fixes it for us too, thanks @Sefibrah ! (got hit by this during some major upgrades in our NX Angular component-library monorepo: Angular 14 -> 18.2.9, NX and everything else to latest)

We have loading-indicator component (no dependencies on other components) which built fine, but our button-component (which has loading-indicator as a peerDep) did not build, with the same error as OP: Cannot destructure property 'pos' of 'file.referencedFiles[index]' as it is undefined.

Added both the dist/build-folder and the src/public-api.ts to tsconfig.paths, it seems that it needs to have both (since button is importing what is expected to be the built output of loading-indicator, not the source). I think... 😅

nilsel avatar Dec 10 '25 14:12 nilsel

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.