Bug: private class fields transformed incorrectly in ESM #2230
Labels
bug
Something isn't working
completed
This item is complete and has been merged/shipped
idempotency
This item relates to the Idempotency Utility
Expected Behaviour
When using the Idempotency utility with a function formatted as ESM the utility and the function should work as intended.
Current Behaviour
As reported by @AllyMurray in this comment, using the Idempotency utility with ESM bundling causes the following runtime error:
__classPrivateFieldSet is not defined
at import time.Looking at the stack trace, the error can be traced back to a private field in the
IdempotencyConfig
class. This is not the only occurrence of private fields (v8.dev), however it's the first occurrence in the import stack.Code snippet
Steps to Reproduce
@aws-lambda-powertools/idempotency
Possible Solution
After a few tests it seems that the root cause for the issue is the
noEmitHelpers: true
setting that we have enabled for the ESM builds here and its interaction with theES2021
(Node.js 16) target that we currently use.When enabled, this setting makes it so that certain language features are polyfilled during transform depending on the targeted version. In the case of private class fields, depending on the target, the following code:
becomes this:
instead of remaining as it is.
When implementing v2 I disabled the setting for ESM builds thinking that we wouldn't need them since according to the v8.dev docs the feature is supported in Node.js 14+ and we have a target of
ES2021
which roughly aligns to Node.js 16.Apparently I was mistaken, and the combination of
noEmitHelpers: true
andtarget: 'ES2021'
caused the esm bundle to be emitted like this, aka__classPrivateFieldSet
is not included even though the code has been downleveled and expects it:The short term solution is to disable the
noEmitHelpers
setting and make a release, this will fix the issue at the cost of a few extra lines (~20) being present in the bundle.In the medium term, and specifically once we drop support for Node.js 16 (#2223) in a few months, we'll change the target to
ES2022
and when that happens we will be able to disablenoEmitHelpers
again since TypeScript emits the private fields as they are (aka without needing an helper fn).Additionally, and this is just as critical, we'll have to prioritize adding new tests that check ESM builds and catch these types of issues.
Powertools for AWS Lambda (TypeScript) version
latest
AWS Lambda function runtime
20.x
Packaging format used
npm
Execution logs
No response
The text was updated successfully, but these errors were encountered: