-
Notifications
You must be signed in to change notification settings - Fork 27.1k
refactor(core): guards stringify calls with ngDevMode #66958
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
base: main
Are you sure you want to change the base?
Conversation
The `stringify` function is only needed for debugging purposes and should not be called in production mode.
| } | ||
|
|
||
| return new R3Injector(providers, parent || getNullInjector(), name || null, scopes); | ||
| return new R3Injector(providers, parent || getNullInjector(), source || null, scopes); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This value, as I understand it, is only used in dev
angular/packages/core/src/di/r3_injector.ts
Lines 401 to 410 in 63d828a
| if (ngDevMode) { | |
| prependTokenToDependencyPath(error, token); | |
| if (previousInjector) { | |
| // We still have a parent injector, keep throwing | |
| throw error; | |
| } else { | |
| // Format & throw the final error message when we don't have any previous injector | |
| throw augmentRuntimeError(error, this.source); | |
| } |
| try { | ||
| if (record.value === CIRCULAR) { | ||
| throw cyclicDependencyError(stringify(token)); | ||
| throw cyclicDependencyError(ngDevMode ? stringify(token) : ''); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We have a similar case here; the value is only used for dev mode.
angular/packages/core/src/render3/errors_di.ts
Lines 24 to 27 in 63d828a
| export function cyclicDependencyError(token: string, path?: string[]): Error { | |
| const message = ngDevMode ? `Circular dependency detected for \`${token}\`.` : ''; | |
| return createRuntimeError(message, RuntimeErrorCode.CYCLIC_DI_DEPENDENCY, path); | |
| } |
packages/core/src/di/forward_ref.ts
Outdated
| (<any>forwardRefFn).__forward_ref__ = forwardRef; | ||
| (<any>forwardRefFn).toString = function () { | ||
| return stringify(this()); | ||
| return ngDevMode ? stringify(this()) : ''; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I’d only have a question in the case of toString. From what I reviewed, I couldn’t find any usage other than a possible development-time debug scenario.
Would this be necessary, similar to R3Injector, if we want to remove the stringify function from the final bundle
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not a fan of return an empty string here.
If we were to drop that custom implem, I would rather wrap it entirely with a if(ngDevMode) { ... }.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated both cases. Also, in R3Injector, I adding // @ts-ignore to suppress specific TypeScript warning
| return `R3Injector[${tokens.join(', ')}]`; | ||
| } | ||
| return `R3Injector[${tokens.join(', ')}]`; | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We still have an issue here as toString will return undefined.
I think its's reasonable to return something like R3Injector[...]
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think in this case should be enough return 'R3Injector[...] wdyt ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, that's what I had in mind.
The
stringifyfunction is only needed for debugging purposes and should not be called in production mode.