nested injector on provider `multi: true` doesn't continue searching parents when injector returns something

This issue has been created since 2022-09-16.

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

core

Is this a regression?

I don't know, but version 14.0.0, 14.1.0 and 14.2.2 all have the same issue.

Description

version: @angular/[email protected]

Code:

const injector1 = Injector.create([{provide: 'foo', useValue: 'bar 1', multi: true}]);
const injector2 = Injector.create([{provide: 'foo', useValue: 'bar 2', multi: true}], injector1);

// actual:   [ 'bar 2' ]
// expected: [ 'bar 2', 'bar 1' ]
console.log('injector2', injector2.get('foo'));

Reproduction: https://stackblitz.com/edit/angular-ivy-vdqqgr?file=src%2Fapp%2Fapp.component.ts

--

I stumbled on this bug as I couldn't provide an BEFORE_APP_SERIALIZED provider on ssr anymore when upgrading from angular 14.1.0 to angular 14.2.2:

platformServer([{
    provide: BEFORE_APP_SERIALIZED,
    useFactory: () => { /* ... do something ... */ },
    multi: true,
});

Because the ServerModule also provides a BEFORE_APP_SERIALIZED, and the dependency injector stops searching for my custom root level provider after it found the one in the ServerModule. Maybe this behavior of Injector was always there. But this commit a0b2d36 made it a problem for me specifically.

JoostK wrote this answer on 2022-09-16

This is expected behavior and working as designed. Multi providers are not merged across injectors, and there are certainly cases where the current behavior is desired.

There was a feature request in #43304 to support this, but it is already possible to achieve merging behavior if needed using the current DI system.

JoostK wrote this answer on 2022-09-16

About the BEFORE_APP_SERIALIZED scenario: it's unclear to me what the exact setup is in that case, so I can't judge what is going on there (and how it was affected from 14.1 to 14.2.2)

sod wrote this answer on 2022-09-16

@JoostK my uneducated guess is that the refactor in a0b2d36 caused the issue for me.

sod wrote this answer on 2022-09-16

I workaround this issue, so a fix is not urgent. But having this as expected behavior wants me to never use multi: true ever again and purge it from our code to not have this potential footgun.

More Details About Repo
Owner Name angular
Repo Name angular
Full Name angular/angular
Language TypeScript
Created Date 2014-09-18
Updated Date 2022-09-30
Star Count 84080
Watcher Count 3064
Fork Count 22235
Issue Count 1197

YOU MAY BE INTERESTED

Issue Title Created Date Comment Count Updated Date
Forwarding ranges of ports 4 2020-08-14 2022-04-24
Clarification request concerning `choosenim_install` 4 2021-06-29 2022-09-02
Incompatible semver module resolution 0 2022-04-02 2022-07-22
Refactor joins to use `pd-merge` 0 2021-07-12 2022-09-12
Investigate and fix test fails bypassed in PR 347 0 2021-09-15 2022-09-25
point_triangulator problem 4 2021-02-01 2022-09-08
Improve docs 0 2021-02-16 2021-12-30
Get PasswordCredentialsToken with TOTP 0 2021-08-09 2022-09-23
error in edgesEvalImg (69th line) : [matchE1,matchG1] = correspondPixels(E1,G{g},maxDist); 0 2021-02-11 2022-08-22
Fuzz Testing 0 2021-03-13 2022-01-10
Ombi not recognizing request has been filled 22 2022-03-09 2022-09-06
Set a different background-image based on the viewport width 4 2022-04-25 2022-09-08
Predicates do not work if stored in a variable 2 2022-08-04 2022-09-02
Is there a way to monitor vGPU with DCGM? 1 2022-04-26 2022-09-07
can use for 2080Ti/1080Ti ? 2 2021-12-11 2022-09-07
Segmentation fault (core dumped) 0 2022-01-27 2022-09-07
0.41版本没有最小化到托盘了 5 2021-09-28 2022-01-13
[增强] 更加明确关闭APP选项的行为 8 2021-09-28 2021-10-20
Yes play music 0 2021-09-26 2021-10-20
How do I avoid any changes to the message? 4 2021-04-19 2022-09-18
Make generator_stop universal 0 2021-10-28 2022-09-10
Preset options are not typed 3 2022-03-14 2022-06-12
Cannot override lsp mappings 13 2022-05-27 2022-09-10
java.lang.NoSuchMethodError: org.opencv.imgcodecs.Imgcodecs.dicomJpgMatRead 1 2021-11-04 2022-09-21
Bunny client properties 1 2019-10-10 2022-09-10
Documentation issue: Installing Bazel on Fedora 35 Fails per the Instructions 0 2022-01-26 2022-09-14
[JVP] Allow the Joomler to decide what should be shown 2 2020-09-21 2022-02-09
Importing the public cucumber API is slow 1 2022-08-15 2022-09-14
project update hook fail with many jobs no playbook found 2 2022-04-01 2022-09-23
monthCellRender does not get called 1 2019-10-25 2022-09-14
SQLSTATE[22001]: String data, right truncated: 1406 Data too long for column 'in_reply_to' at row 1 5 2022-06-01 2022-09-19
Google Play: ru.gdeposylka.delta 2 2022-02-18 2022-08-15
`.husky/.gitignore` is unnecessary in v7.0.0 but how about the new `.husky/_/.gitignore`? 3 2021-09-22 2022-09-30
How to configure automatic log deletion 2 2021-04-15 2022-08-05
Could not load image due to "Command Output: ctr: image might be filtered out" 6 2022-05-17 2022-09-30
Support tags with dynamic/variable children 6 2021-01-02 2022-09-22
Reduce doc and CLI usage maintenance cost 0 2022-04-10 2022-09-22
Unify Date Format in Historical Records 1 2022-06-09 2022-09-21
toggle defx without close current opened dir 1 2021-10-08 2022-09-08
rename command cursor position 1 2021-09-14 2022-09-29
Advanced Options: Enable SSH? 2 2021-11-20 2022-09-14
x/build/cmd/relui: better retry support 2 2022-08-05 2022-09-18
runtime: openbsd/arm fails when compiled with GOARM=5 1 2022-08-05 2022-08-09
Core: add `randomUUID()` and `randomSecureUuid()` Expression Language functions 0 2022-08-12 2022-09-21
Convert trained models to Onnx 1 2021-08-25 2022-08-07
Select2 Dropdown offset 2 2018-08-29 2022-09-11
[Feature] Enable addon in runtime cluster 1 2021-10-27 2022-08-08
[Feature] Rollout will conflict with HPA, autoscaling trait. 0 2021-10-27 2022-08-20
Focus on a specific node on startup 2 2022-01-31 2022-08-29
Some icons are not working at all 0 2019-07-15 2022-09-22