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

[AoT] [ngc] Re-exports causing build error during #482

Closed
babeal opened this issue Jan 11, 2018 · 20 comments
Closed

[AoT] [ngc] Re-exports causing build error during #482

babeal opened this issue Jan 11, 2018 · 20 comments

Comments

@babeal
Copy link

babeal commented Jan 11, 2018

Type of Issue

Getting

[ x] Bug Report
[ ] Feature Request

Description

BUILD ERROR
Cannot read property 'module' of undefined
TypeError: Cannot read property 'module' of undefined
    at MetadataBundler.convertSymbol (<..>/ng-packaged/node_modules/@angular/compiler-cli/src/metadata/bundler.js:264:57)
    at createReference (<..>/ng-packaged/node_modules/@angular/compiler-cli/src/metadata/bundler.js:390:23)
    at MetadataBundler.convertReference (<..>/ng-packaged/node_modules/@angular/compiler-cli/src/metadata/bundler.js:438:24)
    at MetadataBundler.convertExpression (<..>/ng-packaged/node_modules/@angular/compiler-cli/src/metadata/bundler.js:367:33)
    at MetadataBundler.convertValue (<..>/ng-packaged/node_modules/@angular/compiler-cli/src/metadata/bundler.js:348:25)
    at <..>/ng-packaged/node_modules/@angular/compiler-cli/src/metadata/bundler.js:351:58
    at Array.map (<anonymous>)
    at MetadataBundler.convertValue (<..>/ng-packaged/node_modules/@angular/compiler-cli/src/metadata/bundler.js:351:26)
    at MetadataBundler.convertValue (<..>/ng-packaged/node_modules/@angular/compiler-cli/src/metadata/bundler.js:357:32)
    at <..>/ng-packaged/node_modules/@angular/compiler-cli/src/metadata/bundler.js:351:58

The error occurs when I import the shared module from the index.ts file. i.e. import { SharedModule } from "./shared/index". If I import directly from the shared.module the error goes away. I've never had a problem using index files before and would like to know more about why it's failing.

index.ts

export * from "./shared.module";

shared.module.ts

import { NgModule } from "@angular/core";
import { CommonModule } from "@angular/common";

@NgModule({
  imports: [
    CommonModule
  ],
  declarations: [
  ],
  exports: [
  ]
})
export class SharedModule {}

How To Reproduce

Expected Behaviour

I can import reexports properly without getting cryptic error.

Version Information

$ node_modules/.bin/ng-packagr --version
ng-packagr:            2.0.0-rc.10
@angular/compiler:     5.2.0
@angular/compiler-cli: 5.2.0
rollup:                0.53.0
tsickle:               0.26.0
typescript:            2.6.2
@babeal
Copy link
Author

babeal commented Jan 11, 2018

I just added tried this out in the ng-packaged repository and I get the same error. So it looks like reexports aren't supported by this tool. Any reason?

@babeal
Copy link
Author

babeal commented Jan 11, 2018

one last update, it seems to work if you remove the file name import { SharedModule } from './shared'; But this is not valid es6 and only works when running in a node process. If we use system.js in the browser for the demo site, any import of a folder will fail.

@babeal
Copy link
Author

babeal commented Jan 12, 2018

Here is a version of the packager repo that exhibits the problem. https://github.com/babeal/ng-packaged

@babeal
Copy link
Author

babeal commented Jan 22, 2018

When the ngc configuration is set to output a flatModuleOutFile, it runs code in the bundler.ts file which sadly is broken. It tries to resolve the symbol for the symbolic reference to the shared module and it's canonicalSymbol declaration reference is null. Can you link these issues?

angular/angular#20931

@dherges
Copy link
Contributor

dherges commented Jan 22, 2018

Not a v1 issue. issue reproducible in v2 with angular 5.

By applying this change set, the compile error goes away.

diff --git a/my-lib/src/lib.module.ts b/my-lib/src/lib.module.ts
index 0ad912a..ef862f8 100644
--- a/my-lib/src/lib.module.ts
+++ b/my-lib/src/lib.module.ts
@@ -2,7 +2,7 @@ import { NgModule, ModuleWithProviders } from '@angular/core';
 import { CommonModule } from '@angular/common';
 import { FooComponent } from './foo/foo.component';
 import { BarService } from './bar/bar.service';
-import { SharedModule } from './shared/index';
+import { SharedModule } from './shared/shared.module';
 
 @NgModule({
   imports: [

On first sight, it looks related to TypeScript barrels (#195). But, the import was explicit to the ./shared/index (not the folder name which will be aliased to index).

Here are the bundle indexes:

$ cat my-lib.d.ts

/**
 * Generated bundle index. Do not edit.
 */
export * from './public_api';
export { SharedModule as ɵa } from './shared/shared.module';

$ cat my-lib.metadata.json

{
  "__symbolic": "module",
  "version": 4,
  "metadata": {
    "BarService": {
      "__symbolic": "class",
      "decorators": [
        {
          "__symbolic": "call",
          "expression": {
            "__symbolic": "reference",
            "module": "@angular\/core",
            "name": "Injectable",
            "line": 5,
            "character": 1
          }
        }
      ],
      "members": {
        
      }
    },
    "FooComponent": {
      "__symbolic": "class",
      "decorators": [
        {
          "__symbolic": "call",
          "expression": {
            "__symbolic": "reference",
            "module": "@angular\/core",
            "name": "Component",
            "line": 2,
            "character": 1
          },
          "arguments": [
            {
              "selector": "my-foo",
              "template": "<h1>foo<\/h1>",
              "styles": [
                "h1 {\n  color: #ff00ff; }\n"
              ]
            }
          ]
        }
      ],
      "members": {
        
      }
    },
    "MyLibModule": {
      "__symbolic": "class",
      "decorators": [
        {
          "__symbolic": "call",
          "expression": {
            "__symbolic": "reference",
            "module": "@angular\/core",
            "name": "NgModule",
            "line": 6,
            "character": 1
          },
          "arguments": [
            {
              "imports": [
                {
                  "__symbolic": "reference",
                  "name": "\u0275a"
                }
              ],
              "declarations": [
                {
                  "__symbolic": "reference",
                  "name": "FooComponent"
                }
              ],
              "exports": [
                {
                  "__symbolic": "reference",
                  "name": "FooComponent"
                }
              ]
            }
          ]
        }
      ],
      "members": {
        
      },
      "statics": {
        "forRoot": {
          "__symbolic": "function",
          "parameters": [
            
          ],
          "value": {
            "ngModule": {
              "__symbolic": "reference",
              "name": "MyLibModule"
            },
            "providers": [
              {
                "__symbolic": "reference",
                "name": "BarService"
              }
            ]
          }
        }
      }
    },
    "\u0275a": {
      "__symbolic": "class",
      "decorators": [
        {
          "__symbolic": "call",
          "expression": {
            "__symbolic": "reference",
            "module": "@angular\/core",
            "name": "NgModule",
            "line": 3,
            "character": 1
          },
          "arguments": [
            {
              "imports": [
                {
                  "__symbolic": "reference",
                  "module": "@angular\/common",
                  "name": "CommonModule",
                  "line": 5,
                  "character": 4
                }
              ]
            }
          ]
        }
      ],
      "members": {
        
      }
    }
  },
  "origins": {
    "BarService": ".\/bar\/bar.service",
    "FooComponent": ".\/foo\/foo.component",
    "MyLibModule": ".\/lib.module",
    "\u0275a": ".\/shared\/shared.module"
  },
  "importAs": "@my\/lib"
}

@dherges dherges changed the title Reexports causing build error during ngc [ngc] Re-exports causing build error during Jan 22, 2018
@chillinski
Copy link

I got the same issue in my project. We defined the following rule for our development "Export from barrels, but only explicitly import from single files instead of barrels".

Is there a solution on your side to fix this in an upcoming version? It would make our code more lean if imports from barrels are possible (e.g. in routing-modules defining routes and navigation structure)

@dirkluijk
Copy link

Looking for a workaround as well. Our library uses barrels everywhere (because they are so convenient!).

@dherges
Copy link
Contributor

dherges commented Feb 6, 2018

@chillinski The metadata is generated by ngc. If it's possible to reproduce the metadata by running just ngc and provide the code in a GitHub project, we're able to either reach out to Angular team for a fix or see if it's an issue in ng-packagr and the way it's using the Angular compiler.

@Siderite
Copy link

Siderite commented Mar 12, 2018

Same issue for me. Noob in npm/angular and stuff, though. Any resolution? I will be trying to remove all index.ts files, see what works.

Update: removed all index.ts files, imported directly from the component .ts files and packagr worked just fine.

@babeal
Copy link
Author

babeal commented Mar 13, 2018

@Siderite you have to export all components, modules and pipes out of the index file that ng packagr uses. That means that you can’t have any private modules used only internally. They have to all be exported towards the top level

@matheo
Copy link

matheo commented Mar 15, 2018

@babeal I just import external types when ng-packagr cannot name some var and it complains about it, but I successfully compiled my library having this problem, without export all the stuff, just the entry NgModule I wanted to export.

Just need to take care when working with barrels, they don't work quite well with ngc, sadly.

@twilson7755
Copy link

We have a fairly large project that extensively uses index files. I am trying to run ng-packagr against our code base and have run into this issue. It is unclear to me from reading this thread whether this issue has/will be fixed or whether we can only use ng-packagr if we eliminate our use of index files (a very big effort!)? This seems like it would be a very common issue.

@babeal
Copy link
Author

babeal commented Apr 24, 2018

@twilson7755 index files aren't the issue. It's the import path to the file from an entry point. The problem isn't in ng-packagr but with the @angular/compiler. So if you have any private imports they don't have a path to the entry point and don't get populated with the correct metadata.

@abbazabacto
Copy link

abbazabacto commented May 28, 2018

BUILD ERROR
src/index.ts(8,1): error TS2308: Module '...' has already exported a member named 'ɵa'. Consider explicitly re-exporting to resolve the ambiguity.

I did get a similar issue due to Typescript Barrels.

My secondary entry point uses a barrel but didn't export all the component which were imported elsewhere this lead to the creation of an extra file with this forgotten to export component:

/**
 * Generated bundle index. Do not edit.
 */
export * from './index';
export { MatOverlayOriginComponent as ɵa } from './mat-overlay-origin/mat-overlay-origin.component';

As multiple of my libraries had the same issue it complained about re-exporting the same symbol.

I could resolve the issue by exporting the MatOverlayOriginComponent in the index.ts of its module.

Hope this can help in resolving this issue

@sergiojoker11
Copy link

sergiojoker11 commented Nov 21, 2018

In Angular, the error occurs when importing stuff from barrels in a module or routing file. As many people said in above comments, the error disappears when one imports the component/service from the actual file path and not from the barrel.
Versions:
"ng-packagr": "^4.1.0"
"@angular/cli": "^6.2.7"
"@angular/xxx": "^6.1.10"
"typescript": "^2.9.2".

It would be really wonderful if at least the error message was a bit more descriptive because this minor issue can make people waste a lot of time.

BUILD ERROR
Cannot read property 'module' of undefined
TypeError: Cannot read property 'module' of undefined
    at MetadataBundler.convertSymbol (/home/sergiojoker11/IdeaProjects/collect/node_modules/@angular/compiler-cli/src/metadata/bundler.js:312:61)
    at createReference (/home/sergiojoker11/IdeaProjects/collect/node_modules/@angular/compiler-cli/src/metadata/bundler.js:438:27)
    at MetadataBundler.convertReference (/home/sergiojoker11/IdeaProjects/collect/node_modules/@angular/compiler-cli/src/metadata/bundler.js:486:28)
    at MetadataBundler.convertExpression (/home/sergiojoker11/IdeaProjects/collect/node_modules/@angular/compiler-cli/src/metadata/bundler.js:415:37)
    at MetadataBundler.convertValue (/home/sergiojoker11/IdeaProjects/collect/node_modules/@angular/compiler-cli/src/metadata/bundler.js:396:29)
    at MetadataBundler.convertValue (/home/sergiojoker11/IdeaProjects/collect/node_modules/@angular/compiler-cli/src/metadata/bundler.js:405:36)
    at /home/sergiojoker11/IdeaProjects/collect/node_modules/@angular/compiler-cli/src/metadata/bundler.js:399:62
    at Array.map (<anonymous>)
    at MetadataBundler.convertValue (/home/sergiojoker11/IdeaProjects/collect/node_modules/@angular/compiler-cli/src/metadata/bundler.js:399:30)
    at MetadataBundler.convertValue (/home/sergiojoker11/IdeaProjects/collect/node_modules/@angular/compiler-cli/src/metadata/bundler.js:405:36)


Compilation failed. Watching for file changes...

@babeal
Copy link
Author

babeal commented Nov 21, 2018

The error message improvement has to be done in the angular compiler. I’ve been meaning to do it but haven’t gone through the process of signing the cla. So it would be better to open an issue to improve the error messages on the angular compiler; most likely this already been done but can’t remember

@dherges dherges added the bug label Jan 2, 2019
@dherges dherges changed the title [ngc] Re-exports causing build error during [AoT] [ngc] Re-exports causing build error during Jan 24, 2019
@tonivj5
Copy link

tonivj5 commented Mar 11, 2019

Any news?

@lnbxyz
Copy link

lnbxyz commented Jun 17, 2020

This frequently affects me when working on a large project and I spend way too much time finding out what export is missing. The error message is useless.

@alan-agius4
Copy link
Member

Thanks for reporting this issue. This issue is now obsolete due to changes in the recent releases.

@alan-agius4 alan-agius4 closed this as not planned Won't fix, can't repro, duplicate, stale Jun 12, 2022
@github-actions
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.

This action has been performed automatically by a bot.

@github-actions github-actions bot locked and limited conversation to collaborators Jul 13, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Development

No branches or pull requests