Skip to content

refactor(core): make deps tracker globally available - #51495

Closed
pmvald wants to merge 1 commit into
angular:mainfrom
pmvald:rdt-make-global
Closed

pmvald wants to merge 1 commit into
angular:mainfrom
pmvald:rdt-make-global

Conversation

@pmvald

@pmvald pmvald commented Aug 24, 2023 •

Copy link
Copy Markdown
Member

In some cases (e.g., aio tests) the file dept_tracker.ts might be loaded into browser multiple times (possibly due to presence in different chunks) and this leads to multiple copies of the deps tracker. But the dept tracker should be singleton. So we use global to achieve this.

This will effectively fix the local aio breakage in #51415

PR Checklist

Please check if your PR fulfills the following requirements:

PR Type

What kind of change does this PR introduce?

  • Bugfix
  • Feature
  • Code style update (formatting, local variables)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • CI related changes
  • Documentation content changes
  • angular.io application / infrastructure changes
  • Other... Please describe:

What is the current behavior?

Issue Number: N/A

What is the new behavior?

Does this PR introduce a breaking change?

  • Yes
  • No

Other information

In some cases (e.g., aio tests) the file `dept_tracker.ts` might be loaded into browser multiple times (possibly due to presence in different chunks) and this leads to multiple copies of the deps tracker. But the dept tracker should be singleton. So we use `global` to achieve this.
@pmvald
pmvald marked this pull request as ready for review August 24, 2023 18:33
@pmvald
pmvald requested a review from devversion August 24, 2023 18:34
@pullapprove
pullapprove Bot requested a review from AndrewKushnir August 24, 2023 18:34
@pmvald pmvald added action: review The PR is still awaiting reviews from at least one requested reviewer area: core Issues related to the framework runtime PullApprove: disable labels Aug 24, 2023
@ngbot ngbot Bot modified the milestone: Backlog Aug 24, 2023
@pmvald
pmvald requested review from crisbeto and removed request for AndrewKushnir August 24, 2023 18:34
@Yogu

Yogu commented Aug 24, 2023

Copy link
Copy Markdown
Contributor

Sorry for the noise if I'm wrong, but in #50063, some usages of global were replaced by globalThis because "global is now available on every runtime supported by Angular". Maybe globalThis could be used here, too?

@JeanMeche

JeanMeche commented Aug 24, 2023 •

Copy link
Copy Markdown
Member

Sorry for the noise if I'm wrong, but in #50063, some usages of global were replaced by globalThis because "global is now available on every runtime supported by Angular". Maybe globalThis could be used here, too?

@Yogu This is fine here, the global constant is typed as any. globalThis only has the cross-env global object/methods.

@JoostK

JoostK commented Aug 24, 2023

Copy link
Copy Markdown
Member

This has the potential of introducing issues for environments with multiple copies of Angular on a page, e.g. self-contained Angular elements.

@pmvald

pmvald commented Aug 24, 2023

Copy link
Copy Markdown
Member Author

@JoostK Good point! Do we have a test for this type of environments? I didn't see any, but it is nice to have as there are some usages of global in the codebase which might produce the same issue.

For this particular case I'm optimistic it may not cause issues since deps tracker uses the class as key to store Types and their info, and since classes from different environments are different there might not be a conflict ... in other words one depsTracker for all environments might work. But we can try it out and even add some tests for that if possible.

@JoostK

JoostK commented Aug 24, 2023

Copy link
Copy Markdown
Member

I don't know of any such scenarios being covered by tests. My main concern is how the code of the deps tracker itself might be different across versions, possibly resulting in failures.

I doubt that multiple copies/versions of @angular/core are widely used intentionally; we typically see the opposite problem where two copies have independent runtime state, resulting in e.g. inject throwing that no injection context is available, if the injection context is only active in one copy of the runtime, where inject of the other runtime is used.

I would like to understand how this module may end up being loaded twice unintentionally to better judge the changes in this PR, i.e. is this a "root cause" fix, or a workaround for an unknown cause.

@devversion devversion self-assigned this Aug 25, 2023
@devversion

Copy link
Copy Markdown
Member

The actual bug is that we are importing using relative imports from other entry-points. The bundler will then duplicate the code, even though it should actually be imported from e.g. @angular/core's primary entry-point. This is a problem we were also facing in Angular Material a while ago- and we built a tslint rule to prevent this.

We never ended up needing it here, but I've started work to build this into ng_package as that Bazel rule has all the necessary information and we can improve correctness here + fix singleton bugs like this. Those are always hard to debug and quite annoying.

@pmvald

pmvald commented Aug 25, 2023

Copy link
Copy Markdown
Member Author

Nice! Close in favour of #51500

@pmvald pmvald closed this Aug 25, 2023
@angular-automatic-lock-bot

Copy link
Copy Markdown

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 Sep 25, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

action: review The PR is still awaiting reviews from at least one requested reviewer area: core Issues related to the framework runtime PullApprove: disable

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants