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

[sdk/nodejs] Fix a bug in closure serialization. #6999

Merged
merged 6 commits into from
Jul 16, 2021
Merged

[sdk/nodejs] Fix a bug in closure serialization. #6999

merged 6 commits into from
Jul 16, 2021

Conversation

swen128
Copy link
Contributor

@swen128 swen128 commented May 8, 2021

Description

When a closure captures an object which has complex properties (e.g. getters / setters), Pulumi serializes the properties using the defineProperty function.

This does not work properly if a function object has complex properties.
For example, when a class has a static getter, the getter is evaluated at deploy time and the returned value is used to serialize the class.

import {serializeFunction} from "@pulumi/pulumi/runtime";

class Class {
  static get foo() {
    return Math.random();
  }
}

serializeFunction(Class).then(f => {
  console.log(f.text);
});

The above script prints out the following serialized code (Notice the random value injected):

exports.handler = __f0;

__f0.foo = 0.7598867621294356;

function __f0() {
  return (function() {
    with({  }) {

return function /*constructor*/() { };

    }
  }).apply(undefined, undefined).apply(this, arguments);
}

Fixes #6908
The reported error occurs when Pulumi tries to serialize a getter BaseEntity.target

Checklist

  • I have added tests that prove my fix is effective or that my feature works

@github-actions
Copy link

github-actions bot commented May 8, 2021

PR is now waiting for a maintainer to run the acceptance tests.

Note for the maintainer: To run the acceptance tests, please comment /run-acceptance-tests on the PR

@github-actions
Copy link

github-actions bot commented May 8, 2021

PR is now waiting for a maintainer to run the acceptance tests.

Note for the maintainer: To run the acceptance tests, please comment /run-acceptance-tests on the PR

@kfirba
Copy link

kfirba commented May 9, 2021

Bravo!
Thanks for this PR! 👏🏻

@lukehoban
Copy link
Member

/run-acceptance-tests

@github-actions
Copy link

github-actions bot commented May 9, 2021

Please view the results of the PR Build + Acceptance Tests Run Here

2 similar comments
@github-actions
Copy link

Please view the results of the PR Build + Acceptance Tests Run Here

@github-actions
Copy link

Please view the results of the PR Build + Acceptance Tests Run Here

@kfirba
Copy link

kfirba commented May 11, 2021

@lukehoban Any plans on merging this? I'm stumbling across many issues with this, unfortunately

@github-actions
Copy link

PR is now waiting for a maintainer to run the acceptance tests.

Note for the maintainer: To run the acceptance tests, please comment /run-acceptance-tests on the PR

@@ -1309,7 +1309,7 @@ return function () { console.log(v); };

var __v = {};
var __v_d1_proto = {};
__f1.prototype = __v_d1_proto;
Object.defineProperty(__f1, "prototype", { value: __v_d1_proto });
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The one thing I'm slightly nervous about is that this change will cause a lot of usage of closure serialization to change - which will lead to e.g. redeploying all Lambdas when this version of Pulumi is adopted. That's not necessarily a blocker - though it is a downside we'll need to message.

I was initially wondering if this particular part of the change could be suppressed - but I do see that it is also technically incorrect to do what we were doing previously.

How would you characterize the cases where serialization would change after this PR? Is it "if you serialize a class definition, the serialization will be different"?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How would you characterize the cases where serialization would change after this PR? Is it "if you serialize a class definition, the serialization will be different"?

That characterization sounds good to me.

This PR could also affect any function whose property is defined via Object.defineProperty, but I don't think there are many use cases other than class definition.

@pgavlin
Copy link
Member

pgavlin commented Jul 13, 2021

@lukehoban @swen128 is this ready to go?

@github-actions
Copy link

PR is now waiting for a maintainer to run the acceptance tests.

Note for the maintainer: To run the acceptance tests, please comment /run-acceptance-tests on the PR

Further commands available:

  • /run-codegen - used to test the Pull Request against downstream codegen
  • /run-docs-gen - used to test the Pull Request against documentation generation

@lukehoban
Copy link
Member

@lukehoban @swen128 is this ready to go?

Yes - I think we should merge this. It will lead to some magic functions being redeployed even with no user changes, but we've generally been okay with that in minor versions in the past.

@lukehoban
Copy link
Member

/run-acceptance-tests

@github-actions
Copy link

Please view the results of the PR Build + Acceptance Tests Run Here

@github-actions
Copy link

PR is now waiting for a maintainer to run the acceptance tests.

Note for the maintainer: To run the acceptance tests, please comment /run-acceptance-tests on the PR

Further commands available:

  • /run-codegen - used to test the Pull Request against downstream codegen
  • /run-docs-gen - used to test the Pull Request against documentation generation

@komalali komalali merged commit c6062ea into pulumi:master Jul 16, 2021
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.

Function serialization seems to execute code unexpectedly
5 participants