Skip to content

Libraries built with esbuild yield "ReferenceError: Cannot access '_c1' before initialization" #56403

Closed

Description

@bgotink

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

compiler-cli, compiler

Is this a regression?

No

Description

When libraries are built using esbuild (e.g. some rc versions of ng-packagr before the change was reverted) the built packages are unusable in webpack builds (verified with ng serve and ng test)

Causes ng-packagr/ng-packagr#2850 (which is closed because the move to esbuild was reverted)

Please provide a link to a minimal reproduction of the bug

ng-packagr/ng-packagr#2850 (comment)

Please provide the exception or error you saw

ReferenceError: Cannot access '_c1' before initialization
    at 3347 (main.js:163526:23)
    at __webpack_require__ (runtime.js:23:41)
    at 25429 (main.js:6695:71)
    at __webpack_require__ (runtime.js:23:41)
    at 76311 (main.js:6539:92)
    at __webpack_require__ (runtime.js:23:41)
    at __webpack_exec__ (main.js:203398:48)
    at main.js:203399:229
    at __webpack_require__.O (runtime.js:57:23)
    at main.js:203400:56

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

Angular CLI: 18.0.3
Node: 20.11.0
Package Manager: yarn 3.3.1
OS: darwin arm64

Angular: 18.0.2
... cdk, common, compiler, compiler-cli, core, elements, forms
... localize, platform-browser, platform-browser-dynamic, router

Package                         Version
---------------------------------------------------------
@angular-devkit/architect       0.1800.3
@angular-devkit/build-angular   18.0.3
@angular-devkit/core            18.0.3
@angular-devkit/schematics      18.0.3
@angular/cli                    18.0.3
@schematics/angular             18.0.3
rxjs                            7.8.1
typescript                      5.4.5
zone.js                         0.14.7

Anything else?

Note the line numbers in the main.js file:
image
image
(excuse the partial screenshots, the codebase is not public)

The const _cX statements are inserted way below where they're used.
This appears to be caused by esbuild not hoisting imports in its output. If I update the built "FESM" file and move all import statements to the top of the file, everything works and the _cX constants are defined before they're used.

Activity

  1. JoostK commented on Jun 12, 2024

    @JoostK
    Member

    This was the reason for the esbuild usage revert in ng-packagr, so I'd advise to update to not run into this problem. If esbuild is reintroduced into ng-packgr we'll look into why this happens and how it can be resolved.

  2. bgotink commented on Jun 12, 2024

    @bgotink
    Author

    We're not using ng-packagr, we've got our own library builder, where we switched to esbuild earlier and we now have to revert because of this same issue.

  3. JoostK commented on Jun 12, 2024

    @JoostK
    Member

    Could you please share (parts of) the library output that is consumed in the app?

  4. bgotink commented on Jun 12, 2024

    @bgotink
    Author

    Dummy library passed through our pipeline: repro.zip

    Here's the FESM file, where I added a comment showing where the constants are inserted
    // dist/@something/redacted/esm2022/my-test.component.js
    import { ChangeDetectionStrategy, Component } from "@angular/core";
    import * as i0 from "@angular/core";
    var MyTest = class _MyTest {
      static {
        this.\u0275fac = i0.\u0275\u0275ngDeclareFactory({ minVersion: "12.0.0", version: "18.0.2", ngImport: i0, type: _MyTest, deps: [], target: i0.\u0275\u0275FactoryTarget.Component });
      }
      static {
        this.\u0275cmp = i0.\u0275\u0275ngDeclareComponent({ minVersion: "14.0.0", version: "18.0.2", type: _MyTest, isStandalone: true, selector: "my-test", ngImport: i0, template: `
        <h1><ng-content select="[title]"></ng-content></h1>
        <ng-content></ng-content>
      `, isInline: true, changeDetection: i0.ChangeDetectionStrategy.OnPush });
      }
    };
    i0.\u0275\u0275ngDeclareClassMetadata({ minVersion: "12.0.0", version: "18.0.2", ngImport: i0, type: MyTest, decorators: [{
      type: Component,
      args: [{
        standalone: true,
        selector: "my-test",
        template: `
        <h1><ng-content select="[title]"></ng-content></h1>
        <ng-content></ng-content>
      `,
        changeDetection: ChangeDetectionStrategy.OnPush
      }]
    }] });
    
    // dist/@something/redacted/esm2022/test.module.js
    import { NgModule } from "@angular/core";
    import * as i02 from "@angular/core";
    // <<---- This is where the `const _cX` variables are inserted
    var TestModule = class _TestModule {
      static {
        this.\u0275fac = i02.\u0275\u0275ngDeclareFactory({ minVersion: "12.0.0", version: "18.0.2", ngImport: i02, type: _TestModule, deps: [], target: i02.\u0275\u0275FactoryTarget.NgModule });
      }
      static {
        this.\u0275mod = i02.\u0275\u0275ngDeclareNgModule({ minVersion: "14.0.0", version: "18.0.2", ngImport: i02, type: _TestModule, imports: [MyTest], exports: [MyTest] });
      }
      static {
        this.\u0275inj = i02.\u0275\u0275ngDeclareInjector({ minVersion: "12.0.0", version: "18.0.2", ngImport: i02, type: _TestModule });
      }
    };
    i02.\u0275\u0275ngDeclareClassMetadata({ minVersion: "12.0.0", version: "18.0.2", ngImport: i02, type: TestModule, decorators: [{
      type: NgModule,
      args: [{
        imports: [MyTest],
        exports: [MyTest]
      }]
    }] });
    export {
      MyTest,
      TestModule
    };
  5. JoostK commented on Jun 12, 2024

    @JoostK
    Member

    Ah, thanks, I see now. The problem originates here:

    /**
    * Insert the `statements` at the top of the `program`, below any import statements.
    */
    function insertIntoProgram(program: NodePath<t.Program>, statements: t.Statement[]): void {
    const body = program.get('body');
    const importStatements = body.filter((statement) => statement.isImportDeclaration());
    if (importStatements.length === 0) {
    program.unshiftContainer('body', statements);
    } else {
    importStatements[importStatements.length - 1].insertAfter(statements);
    }
    }

  6. added
    area: compiler
    P3
    on Jun 12, 2024
  7. modified the milestone: Backlog on Jun 12, 2024
  8. added a commit that references this issue on Jun 13, 2024
    5188604
  9. JoostK commented on Jun 13, 2024

    @JoostK
    Member

    @bgotink perhaps you could patch in #56431 locally to see if that resolves the issue you're having.

  10. bgotink commented on Jun 13, 2024

    @bgotink
    Author

    @JoostK That patch fixes everything, thanks!

  11. added a commit that references this issue on Jun 13, 2024
    ec0d1bf
  12. added a commit that references this issue on Jun 13, 2024
    0b867e8
  13. angular-automatic-lock-bot commented on Jul 14, 2024

    @angular-automatic-lock-bot

    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.

  14. locked and limited conversation to collaborators on Jul 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    P3area: compilercompiler: linkerstate: has PR

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions