Skip to content

Unable to acquire PlatformRef and ModuleRef with aot during bootstrap #8265

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
EricABC opened this issue Oct 31, 2017 · 10 comments
Closed

Unable to acquire PlatformRef and ModuleRef with aot during bootstrap #8265

EricABC opened this issue Oct 31, 2017 · 10 comments
Labels
P3 An issue that is relevant to core functions, but does not impede progress. Important, but not urgent type: bug/fix

Comments

@EricABC
Copy link

EricABC commented Oct 31, 2017

Very similar to #8234

Bug Report or Feature Request (mark with an x)

- [ X ] bug report -> please search issues before submitting
- [ ] feature request

Versions. ng-cli@rc-8, ng@rc-9

Repro steps.

The following bootstrap code worked somewhere around rc-3 and down:

environment.ngPlatformRef = platformBrowserDynamic();
environment.ngPlatformRef.bootstrapModule(AppModule).then(r => environment.ngModuleRef = r);

The log given by the failure.

Same templateUrl 404's as #8234.
http://localhost:4200/material.component.html 404 (Not Found)

Desired functionality.

The desire is to have the ability to tear down an angular application, as our front end separates behavioral services in a separate parallel application from the angular application. This is done by calling the dispose methods on the module and platform (some detail found in issue for known memory leak: angular/angular#13725). I have tried a few forms of capturing these to no avail (settings to constants first, etc...). As soon as I give up the concern for tearing the ng app down, the standard platformBrowserDynamic().bootstrapModule(AppModule) works fine.

@filipesilva filipesilva self-assigned this Oct 31, 2017
@filipesilva
Copy link
Contributor

This happens due to the way we try to replace the bootstrap code for AOT. We look for platformBrowserDynamic().bootstrapModule(AppModule) and that detection is not flexible enough to allow for this sort of indirection.

@filipesilva filipesilva added P3 An issue that is relevant to core functions, but does not impede progress. Important, but not urgent type: bug/fix labels Oct 31, 2017
@EricABC
Copy link
Author

EricABC commented May 26, 2018

This was fixed, thanks.

@EricABC EricABC closed this as completed May 26, 2018
@EricABC
Copy link
Author

EricABC commented May 26, 2018

Oops, stupid mistake on my part. It still is an issue.

@EricABC EricABC reopened this May 26, 2018
@mgechev
Copy link
Member

mgechev commented Dec 22, 2018

Are you still able to reproduce this issue with the latest version?

@EricABC
Copy link
Author

EricABC commented Dec 22, 2018

Yup, you can drop this into an 'ng new' main.ts and then run with --aot. This was 7.2.0-rc.0 and errors out here. Everything runs fine without aot.

const ngPlatformRef = platformBrowserDynamic();
let ngModuleRef: NgModuleRef<AppModule>;
ngPlatformRef.bootstrapModule(AppModule).then(r => ngModuleRef = r);

@alexzuza
Copy link

@EricABC
What about this option?

import { getPlatform, NgModuleRef, PlatformRef } from '@angular/core';
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';

let ngPlatformRef: PlatformRef;
let ngModuleRef: NgModuleRef<AppModule>;

platformBrowserDynamic().bootstrapModule(AppModule)
  .then(r => {
    ngModuleRef = r;
    ngPlatformRef = getPlatform(); // or r.injector.get(PlatformRef);
  });

@EricABC
Copy link
Author

EricABC commented Dec 23, 2018

Thanks for the suggestion. Although this works, it couples the synchronous entry point and resource allocation of angular with the asynchronous application resource allocation. I think there is value in separating the angular context (platform) from the developer context (app bootstrapping). If an angular app fails to bootstrap, there is a way to recover the 'outer' app. This could be useful for any situation where angular works in harmony with other external entry points, frameworks, tooling apps that assist in angular development, etc...

Actually, I was starting to write a proposal to PR this, but Ivy looks like it's being designed to handle more complex use cases, including multiple root nodes and root contexts. I'm thinking that any work here might be short-lived. I'm happy closing this as out of scope, or if desired I could still PR. If you want the PR, I would implement by creating a separate entrypoint discovery context class that explicitly maps SyntaxKind.Identifier entryModuleIdentifiers to imports, discovers variable assignments, and ultimately replaces the expressions. replaceBootstrap and replaceServerBootstrap would both use this class to map one or more entrypoints.

@EricABC
Copy link
Author

EricABC commented Dec 23, 2018

Actually, I tried this and I was able to access and dispose the platform properly at both a synchronous failure (ie. module constructor throws) and an async failure (ie. a component constructor throws).

try {
  platformBrowserDynamic().bootstrapModule(AppModule).catch(e => {
    console.log('async');
    getPlatform().destroy();
  });
} catch (err) {
  console.log('sync');
  getPlatform().destroy();
}

I'd close this, but i'll leave it to you guys since its marked with a higher priority, or if you wanted the PR anyway to support more entrypoint syntax.

@filipesilva
Copy link
Contributor

I think the approach you are suggesting should be ok. We try to avoid adding more bootstrap patterns because they are brittle by nature.

@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 Nov 8, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
P3 An issue that is relevant to core functions, but does not impede progress. Important, but not urgent type: bug/fix
Projects
None yet
Development

No branches or pull requests

4 participants