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 <github-actions@github.com>
Chriscbr
left a comment
There was a problem hiding this comment.
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, | ||
| }); No newline at end of file |
There was a problem hiding this comment.
Why not put this in the constructor? (is there any difference?)
There was a problem hiding this comment.
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.
MrArnoldPalmer
left a comment
There was a problem hiding this comment.
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 <rybickic@amazon.com>
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.isConstructwas still using (and recommending)instanceof,even though
instanceofcan never be made to work reliably.When we thought
instanceofwas safe to use again, it's because wethought that
npm installcombined withpeerDependencieswouldmake sure only one copy of
constructswould ever be installed.That's correct, but monorepos and users using
npm linkcanstill mess up that guarantee, so we cannot rely on it after all.