Skip to content
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

Merged
merged 2 commits into from
Mar 22, 2022

Conversation

rix0rrr
Copy link
Contributor

@rix0rrr rix0rrr commented Mar 21, 2022

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.

`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.
@rix0rrr rix0rrr requested a review from a team March 21, 2022 10:50
@rix0rrr rix0rrr self-assigned this Mar 21, 2022
Signed-off-by: github-actions <[email protected]>
Copy link
Contributor

@Chriscbr Chriscbr left a 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. :)

Comment on lines +555 to +560
// Mark all instances of 'Construct'
Object.defineProperty(Construct.prototype, CONSTRUCT_SYM, {
value: true,
enumerable: false,
writable: false,
});
Copy link
Contributor

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?)

Copy link
Contributor Author

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.

Copy link
Contributor

@MrArnoldPalmer MrArnoldPalmer left a 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" 👀

@rix0rrr rix0rrr merged commit bef8e4d into 10.x Mar 22, 2022
@rix0rrr rix0rrr deleted the huijbers/fix-instanceof branch March 22, 2022 18:03
mnapoli added a commit to mnapoli/aws-cdk that referenced this pull request Mar 22, 2022
mnapoli added a commit to mnapoli/aws-cdk that referenced this pull request Mar 23, 2022
mnapoli added a commit to mnapoli/aws-cdk that referenced this pull request Mar 23, 2022
mnapoli added a commit to mnapoli/aws-cdk that referenced this pull request Mar 23, 2022
mnapoli added a commit to mnapoli/aws-cdk that referenced this pull request Mar 23, 2022
mnapoli added a commit to mnapoli/aws-cdk that referenced this pull request Mar 23, 2022
mergify bot pushed a commit to aws/aws-cdk that referenced this pull request Mar 29, 2022
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*
mergify bot pushed a commit to cdk8s-team/cdk8s-core that referenced this pull request Mar 29, 2022
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]>
StevePotter pushed a commit to StevePotter/aws-cdk that referenced this pull request Apr 27, 2022
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*
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants