-
Notifications
You must be signed in to change notification settings - Fork 40
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
fix: isConstruct
is wrong when symlinking libraries
#955
Conversation
`Construct.isConstruct` was still using (and recommending) `instanceof`, even though `instanceof` can never be made to work reliably. When we thought `instanceof` was safe to use again, it's because we thought that `npm install` combined with `peerDependencies` would make sure only one copy of `constructs` would ever be installed. That's correct, but monorepos and users using `npm link` can still mess up that guarantee, so we cannot rely on it after all.
Signed-off-by: github-actions <[email protected]>
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've been pondering for a bit if there's any ways this could cause a regression, but I can't think of anything...
Approving with do-not-merge so you can look one comment I added. Since this library is used in a lot of places, though, it might not hurt to get a second pair of eyes on this. :)
// Mark all instances of 'Construct' | ||
Object.defineProperty(Construct.prototype, CONSTRUCT_SYM, { | ||
value: true, | ||
enumerable: false, | ||
writable: false, | ||
}); |
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.
Why not put this in the constructor? (is there any difference?)
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.
The difference is where the symbol lives: on the instance or on the class.
This puts it on the class, putting it in the construct (and presumably passing this
instead of Construct.prototype
) would put it on the instance.
Putting it on the class saves an instruction on every construct instantiation, and the memory of saving it in every symbol table (plus it might potentially have some impact on V8s optimizations... though I'm for sure not looking at any of that!)
It's probably a minor thing, but this ought to be slightly more efficient.
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.
Makes sense to me as far as "another pair of eyes" 👀
Fixes aws#17974 Inspired from aws/constructs#955
Fixes aws#17974 Inspired from aws/constructs#955
Fixes aws#17974 Inspired from aws/constructs#955
Fixes aws#17974 Inspired from aws/constructs#955
Fixes aws#17974 Inspired from aws/constructs#955
Fixes aws#17974 Inspired from aws/constructs#955
Fixes #17974 Follow-up of #14468 This follows the implementation of aws/constructs#955 to be more robust. ---- ### All Submissions: * [x] Have you followed the guidelines in our [Contributing guide?](https://github.com/aws/aws-cdk/blob/master/CONTRIBUTING.md) ### Adding new Unconventional Dependencies: * [ ] This PR adds new unconventional dependencies following the process described [here](https://github.com/aws/aws-cdk/blob/master/CONTRIBUTING.md/#adding-new-unconventional-dependencies) ### New Features * [ ] Have you added the new feature to an [integration test](https://github.com/aws/aws-cdk/blob/master/INTEGRATION_TESTS.md)? * [ ] Did you use `cdk-integ` to deploy the infrastructure and generate the snapshot (i.e. `cdk-integ` without `--dry-run`)? *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
Fixes #401 I haven't explicitly tested this with the scenario described in #401 but I believe the root cause is the same issue described [here](aws/constructs#955). In a nutshell, "instanceof" is unreliable since it can cause issues if libraries are referencing different versions of cdk8s library in `node_modules`. Signed-off-by: Christopher Rybicki <[email protected]>
Fixes aws#17974 Follow-up of aws#14468 This follows the implementation of aws/constructs#955 to be more robust. ---- ### All Submissions: * [x] Have you followed the guidelines in our [Contributing guide?](https://github.com/aws/aws-cdk/blob/master/CONTRIBUTING.md) ### Adding new Unconventional Dependencies: * [ ] This PR adds new unconventional dependencies following the process described [here](https://github.com/aws/aws-cdk/blob/master/CONTRIBUTING.md/#adding-new-unconventional-dependencies) ### New Features * [ ] Have you added the new feature to an [integration test](https://github.com/aws/aws-cdk/blob/master/INTEGRATION_TESTS.md)? * [ ] Did you use `cdk-integ` to deploy the infrastructure and generate the snapshot (i.e. `cdk-integ` without `--dry-run`)? *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
Construct.isConstruct
was still using (and recommending)instanceof
,even though
instanceof
can never be made to work reliably.When we thought
instanceof
was safe to use again, it's because wethought that
npm install
combined withpeerDependencies
wouldmake sure only one copy of
constructs
would ever be installed.That's correct, but monorepos and users using
npm link
canstill mess up that guarantee, so we cannot rely on it after all.