Skip to content

Compiler generates ngfactory for file with no angular metadata #22193

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

Closed
filipesilva opened this issue Feb 13, 2018 · 14 comments
Closed

Compiler generates ngfactory for file with no angular metadata #22193

filipesilva opened this issue Feb 13, 2018 · 14 comments
Labels
area: compiler Issues related to `ngc`, Angular's template compiler freq2: medium P5 The team acknowledges the request but does not plan to address it, it remains open for discussion state: confirmed type: bug/fix
Milestone

Comments

@filipesilva
Copy link
Contributor

filipesilva commented Feb 13, 2018

I'm submitting a...


[ ] Regression (a behavior that used to work and stopped working in a new release)
[x] Bug report  
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question

Current behavior

Under some circumstances, the AOT compiler can generate a .ngfactory.ts file for a file with no Angular metadata:

  • must be using Angular CLI
  • must be using AOT compilation
  • must be using "module": "commonjs" in tsconfig (the CLI sets it automatically for ng serve, for performance reasons)
  • must have a namespace import (like import * as numeral from 'numeral';) in a file without angular decorators

When all these conditions are met, rebuilds will fail with the following stack trace:

ERROR in : TypeError: Cannot read property 'text' of undefined
    at getErrorSpanForNode (D:\sandbox\repro-bug-angular-cli\node_modules\typescript\lib\typescript.js:7880:40)
    at createDiagnosticForNodeInSourceFile (D:\sandbox\repro-bug-angular-cli\node_modules\typescript\lib\typescript.js:7804:20)
    at Object.createDiagnosticForNode (D:\sandbox\repro-bug-angular-cli\node_modules\typescript\lib\typescript.js:7800:16)
    at error (D:\sandbox\repro-bug-angular-cli\node_modules\typescript\lib\typescript.js:24149:22)
    at resolveExternalModule (D:\sandbox\repro-bug-angular-cli\node_modules\typescript\lib\typescript.js:25263:17)
    at resolveExternalModuleNameWorker (D:\sandbox\repro-bug-angular-cli\node_modules\typescript\lib\typescript.js:25253:20)
    at Object.getExternalModuleFileFromDeclaration (D:\sandbox\repro-bug-angular-cli\node_modules\typescript\lib\typescript.js:45763:32)
    at tryGetModuleNameFromDeclaration (D:\sandbox\repro-bug-angular-cli\node_modules\typescript\lib\typescript.js:50792:50)
    at Object.getExternalModuleNameLiteral (D:\sandbox\repro-bug-angular-cli\node_modules\typescript\lib\typescript.js:50756:20)
    at createRequireCall (D:\sandbox\repro-bug-angular-cli\node_modules\typescript\lib\typescript.js:63361:33)
    at visitExportDeclaration (D:\sandbox\repro-bug-angular-cli\node_modules\typescript\lib\typescript.js:63423:45)
    at sourceElementVisitor (D:\sandbox\repro-bug-angular-cli\node_modules\typescript\lib\typescript.js:63151:28)
    at Object.visitNodes (D:\sandbox\repro-bug-angular-cli\node_modules\typescript\lib\typescript.js:51105:48)
    at transformCommonJSModule (D:\sandbox\repro-bug-angular-cli\node_modules\typescript\lib\typescript.js:62890:40)
    at transformSourceFile (D:\sandbox\repro-bug-angular-cli\node_modules\typescript\lib\typescript.js:62863:27)
    at D:\sandbox\repro-bug-angular-cli\node_modules\typescript\lib\typescript.js:2601:86
    at reduceLeft (D:\sandbox\repro-bug-angular-cli\node_modules\typescript\lib\typescript.js:2274:30)
    at D:\sandbox\repro-bug-angular-cli\node_modules\typescript\lib\typescript.js:2601:42
    at transformRoot (D:\sandbox\repro-bug-angular-cli\node_modules\typescript\lib\typescript.js:65720:82)
    at Object.map (D:\sandbox\repro-bug-angular-cli\node_modules\typescript\lib\typescript.js:1725:29)
    at Object.transformNodes (D:\sandbox\repro-bug-angular-cli\node_modules\typescript\lib\typescript.js:65708:30)
    at Object.emitFiles (D:\sandbox\repro-bug-angular-cli\node_modules\typescript\lib\typescript.js:68442:28)
    at emitWorker (D:\sandbox\repro-bug-angular-cli\node_modules\typescript\lib\typescript.js:72519:33)
    at D:\sandbox\repro-bug-angular-cli\node_modules\typescript\lib\typescript.js:72481:66
    at runWithCancellationToken (D:\sandbox\repro-bug-angular-cli\node_modules\typescript\lib\typescript.js:72573:24)
    at Object.emit (D:\sandbox\repro-bug-angular-cli\node_modules\typescript\lib\typescript.js:72481:20)
    at defaultEmitCallback (D:\sandbox\repro-bug-angular-cli\node_modules\@angular\compiler-cli\src\transformers\program.js:33:20)
    at D:\sandbox\repro-bug-angular-cli\node_modules\@angular\compiler-cli\src\transformers\program.js:231:109
    at Array.map (<anonymous>)
    at AngularCompilerProgram.emit (D:\sandbox\repro-bug-angular-cli\node_modules\@angular\compiler-cli\src\transformers\program.js:231:63)
    at AngularCompilerPlugin._emit (D:\sandbox\repro-bug-angular-cli\node_modules\@ngtools\webpack\src\angular_compiler_plugin.js:749:49)
    at Promise.resolve.then.then.then (D:\sandbox\repro-bug-angular-cli\node_modules\@ngtools\webpack\src\angular_compiler_plugin.js:582:54)
    at <anonymous>
    at process._tickCallback (internal/process/next_tick.js:188:7)

Expected behavior

Compilation succeed.

Minimal reproduction of the problem with instructions

This issue was originally reported and reproduced in angular/angular-cli#9036:

git clone https://github.com/victornoel/repro-bug-angular-cli
cd repro-bug-angular-cli
npm i
npm start -- --aot
# edit a file, e.g. add `console.log(1);` to  `main.ts`

Environment


Angular version: 5.2.2

 
For Tooling issues:
- Node version: 8.9.1
- Platform:  Windows 10
- Angular CLI: 1.6.6

Others:

/cc @chuckjaz

@filipesilva
Copy link
Contributor Author

@chuckjaz looked at this problem yesterday and had this to say:

I believe this line is wrong https://github.com/angular/angular/blob/master/packages/compiler-cli/src/transformers/compiler_host.ts#L456 and is the genesis of the problem. We try to produce re-exports symbols for metadata that refer to files outside this compilation. The line cause the second file to treat the reference to "localData" in the locale.format.ts file as a reference to an external library.

The problem is only with commonjs in that it avoiding it avoids TypeScript trying to report an invalid import. The problem is still there it is just hiding. It will probably show up elsewhere until we fix the way new programs are created and how isSourceFile is reported in incremental builds.

He also suggested a workaround: type the problematic namespace import as any:

export function getCurrentNumeralLocale(): NumeralJSLocale {
  const result = (numeral as any).localeData() as NumeralJSLocale;
  return result;
}

The next major version of Angular CLI will do away with CommonJS for ng serve so this should also ameliorate the problem. This is not a real fix though, it will only reduce the symptoms.

@kondi
Copy link
Contributor

kondi commented Feb 16, 2018

Are you going to include chuckjaz's workaround in a release? Would be cool if we don't have to wait the webpack 4 integration and @angular/cli v6 for the real solution.

@filipesilva
Copy link
Contributor Author

@kondi the workaround is in usercode, not in tooling.

@kondi
Copy link
Contributor

kondi commented Feb 16, 2018 via email

@victornoel
Copy link
Contributor

@filipesilva @chuckjaz the workaround does not seem to be to type as any the problematic import (because it was already the case in the repro code) but it seems to be to assign the problematic line to a variable before returning it (that's really strange ^^).

@ngbot ngbot bot modified the milestones: needsTriage, Backlog Feb 26, 2018
@matmeylan
Copy link

@chuckjaz What's the workaround ? This error happens to me 1/2 times on hot reloads, making my life miserable !

@Dongata
Copy link

Dongata commented May 28, 2018

Is there any workaround available?

@victornoel
Copy link
Contributor

@Dongata @Starscream27 read the comments : #22193 (comment)

@ifdouglas-zz
Copy link

For me the problem was solved just putting
declare function require(string): string;
to the top of my .ts file that has this:
const LoaderImg = require('assets/img/image.gif');

PS: TypeScript and Webpack have conflict when import an image.

@kimamula
Copy link

In my case, injecting REQUEST from @nguniversal/express-engine caused this problem.

import { REQUEST } from '@nguniversal/express-engine/tokens';
import { Request } from 'express';

// ...

export class AppComponent {
  constructor(@Optional() @Inject(REQUEST) req: Request) {
    // ...
  }
}

Changing the type of req to any worked for me.

export class AppComponent {
  constructor(@Optional() @Inject(REQUEST) req: any) {
    // ...
  }
}

@chaoyangnz
Copy link

how is this issue?

@pkozlowski-opensource pkozlowski-opensource added area: compiler Issues related to `ngc`, Angular's template compiler and removed area: core Issues related to the framework runtime labels Mar 14, 2020
@alxhub
Copy link
Member

alxhub commented May 28, 2020

This definitely was/is an issue, which is less of a problem now that commonjs isn't actually that common. The good news is that it's also fixed completely with Angular v9+ and Ivy.

@jelbourn jelbourn added P3 An issue that is relevant to core functions, but does not impede progress. Important, but not urgent and removed P3 An issue that is relevant to core functions, but does not impede progress. Important, but not urgent labels Oct 1, 2020
@ngbot ngbot bot modified the milestones: Backlog, needsTriage Oct 1, 2020
@jelbourn jelbourn added the P3 An issue that is relevant to core functions, but does not impede progress. Important, but not urgent label Oct 1, 2020
@ngbot ngbot bot modified the milestones: needsTriage, Backlog Oct 1, 2020
@jelbourn jelbourn added P5 The team acknowledges the request but does not plan to address it, it remains open for discussion and removed P3 An issue that is relevant to core functions, but does not impede progress. Important, but not urgent severity3: broken labels Oct 1, 2020
@JoostK
Copy link
Member

JoostK commented May 14, 2021

Closing as resolved in Ivy.

@JoostK JoostK closed this as completed May 14, 2021
@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 14, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area: compiler Issues related to `ngc`, Angular's template compiler freq2: medium P5 The team acknowledges the request but does not plan to address it, it remains open for discussion state: confirmed type: bug/fix
Projects
None yet
Development

No branches or pull requests