fix(instrumentation-aws-lambda): handle non-configurable exports from esbuild CJS bundles#3422
Open
RaphaelManke wants to merge 3 commits intoopen-telemetry:mainfrom
Open
Conversation
… esbuild CJS bundles When esbuild bundles ESM to CJS format, it defines exports using non-configurable accessor descriptors. OpenTelemetry's shimmer then fails with "Cannot redefine property" when trying to wrap the handler. Add a makeExportsConfigurable() helper that replaces such exports with a new object having configurable data descriptors before shimmer attempts patching. Fixes: open-telemetry/opentelemetry-lambda#1781
Member
raphael-theriault-swi
left a comment
There was a problem hiding this comment.
Solution looks good, but I'm wondering whether we might want to tackle this from the instrumentation package given it's not technically unique to Lambda
Contributor
Author
|
Can you point me in a direction? I am not sure which instrumentation you are referring to? Like in the core repo? |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Cannot redefine propertywhen trying to wrap the handler.makeExportsConfigurable()helper in the Lambda instrumentation's patch callback that replaces such exports with a new object having configurable data descriptors before shimmer attempts patching.Fixes: open-telemetry/opentelemetry-lambda#1781
See: evanw/esbuild#2199
Steps to reproduce
Create a handler that uses the same non-configurable export pattern that esbuild emits when bundling ESM to CJS:
Deploy as a Lambda with the OTel layer and
AWS_LAMBDA_EXEC_WRAPPER=/opt/otel-handler:Error (before fix):
{ "errorType": "TypeError", "errorMessage": "Cannot redefine property: handler", "trace": [ "TypeError: Cannot redefine property: handler", " at Function.defineProperty (<anonymous>)", " at T._wrap (...)", " at Z.patch (...)", " at T._onRequire (...)" ] }Root cause: esbuild emits exports as non-configurable accessor descriptors (see esbuild#2199), so
Object.definePropertyin shimmer fails when trying to wrap the handler.Test plan
test/lambda-test/esbuild-cjs.jssimulating esbuild's non-configurable export patternTypeError: Cannot redefine property: handler)