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

helper-define-polyfill-provider: Cannot read properties of undefined (reading 'includes') #210

Closed
epmatsw opened this issue Mar 25, 2024 · 9 comments · Fixed by #213
Closed
Labels
bug Something isn't working

Comments

@epmatsw
Copy link

epmatsw commented Mar 25, 2024

I'm having a bit of trouble narrowing down a useful reproduction, but I'm seeing this error with the following stack with @babel/helper-define-polyfill-provider@0.6.1 while I did not see the same issue with 0.5.0.

TypeError: /myproject/afile.ts: Cannot read properties of undefined (reading 'includes')
    at isRemoved (/myproject/node_modules/@babel/helper-define-polyfill-provider/lib/visitors/usage.js:11:45)
    at isRemoved (/myproject/node_modules/@babel/helper-define-polyfill-provider/lib/visitors/usage.js:15:10)
    at isRemoved (/myproject/node_modules/@babel/helper-define-polyfill-provider/lib/visitors/usage.js:15:10)
    at isRemoved (/myproject/node_modules/@babel/helper-define-polyfill-provider/lib/visitors/usage.js:15:10)
    at isRemoved (/myproject/node_modules/@babel/helper-define-polyfill-provider/lib/visitors/usage.js:15:10)
    at isRemoved (/myproject/node_modules/@babel/helper-define-polyfill-provider/lib/visitors/usage.js:15:10)
    at isRemoved (/myproject/node_modules/@babel/helper-define-polyfill-provider/lib/visitors/usage.js:15:10)
    at isRemoved (/myproject/node_modules/@babel/helper-define-polyfill-provider/lib/visitors/usage.js:15:10)
    at PluginPass.MemberExpression (/myproject/node_modules/@babel/helper-define-polyfill-provider/lib/visitors/usage.js:76:104)
    at newFn (/myproject/node_modules/@babel/traverse/lib/visitors.js:160:14)
    at NodePath._call (/myproject/node_modules/@babel/traverse/lib/path/context.js:46:20)
    at NodePath.call (/myproject/node_modules/@babel/traverse/lib/path/context.js:36:17)
    at NodePath.visit (/myproject/node_modules/@babel/traverse/lib/path/context.js:82:31)
    at TraversalContext.visitQueue (/myproject/node_modules/@babel/traverse/lib/context.js:89:16)
    at TraversalContext.visitSingle (/myproject/node_modules/@babel/traverse/lib/context.js:65:19)
    at TraversalContext.visit (/myproject/node_modules/@babel/traverse/lib/context.js:112:19)
    at traverseNode (/myproject/node_modules/@babel/traverse/lib/traverse-node.js:22:17)
    at NodePath.visit (/myproject/node_modules/@babel/traverse/lib/path/context.js:88:52)
    at TraversalContext.visitQueue (/myproject/node_modules/@babel/traverse/lib/context.js:89:16)
    at TraversalContext.visitSingle (/myproject/node_modules/@babel/traverse/lib/context.js:65:19)
    at TraversalContext.visit (/myproject/node_modules/@babel/traverse/lib/context.js:112:19)
    at traverseNode (/myproject/node_modules/@babel/traverse/lib/traverse-node.js:22:17)
    at NodePath.visit (/myproject/node_modules/@babel/traverse/lib/path/context.js:88:52)
    at TraversalContext.visitQueue (/myproject/node_modules/@babel/traverse/lib/context.js:89:16)
    at TraversalContext.visitMultiple (/myproject/node_modules/@babel/traverse/lib/context.js:61:17)
    at TraversalContext.visit (/myproject/node_modules/@babel/traverse/lib/context.js:110:19)
    at traverseNode (/myproject/node_modules/@babel/traverse/lib/traverse-node.js:22:17)
    at NodePath.visit (/myproject/node_modules/@babel/traverse/lib/path/context.js:88:52)
    at TraversalContext.visitQueue (/myproject/node_modules/@babel/traverse/lib/context.js:89:16)
    at TraversalContext.visitSingle (/myproject/node_modules/@babel/traverse/lib/context.js:65:19)
    at TraversalContext.visit (/myproject/node_modules/@babel/traverse/lib/context.js:112:19)
    at traverseNode (/myproject/node_modules/@babel/traverse/lib/traverse-node.js:22:17)
    at NodePath.visit (/myproject/node_modules/@babel/traverse/lib/path/context.js:88:52)
    at TraversalContext.visitQueue (/myproject/node_modules/@babel/traverse/lib/context.js:89:16)
    at TraversalContext.visitSingle (/myproject/node_modules/@babel/traverse/lib/context.js:65:19)
    at TraversalContext.visit (/myproject/node_modules/@babel/traverse/lib/context.js:112:19)
    at traverseNode (/myproject/node_modules/@babel/traverse/lib/traverse-node.js:22:17)
    at NodePath.visit (/myproject/node_modules/@babel/traverse/lib/path/context.js:88:52)
    at TraversalContext.visitQueue (/myproject/node_modules/@babel/traverse/lib/context.js:89:16)
    at TraversalContext.visitMultiple (/myproject/node_modules/@babel/traverse/lib/context.js:61:17)
    at TraversalContext.visit (/myproject/node_modules/@babel/traverse/lib/context.js:110:19)
    at traverseNode (/myproject/node_modules/@babel/traverse/lib/traverse-node.js:22:17)
    at NodePath.visit (/myproject/node_modules/@babel/traverse/lib/path/context.js:88:52)
    at TraversalContext.visitQueue (/myproject/node_modules/@babel/traverse/lib/context.js:89:16)
    at TraversalContext.visitMultiple (/myproject/node_modules/@babel/traverse/lib/context.js:61:17)
    at TraversalContext.visit (/myproject/node_modules/@babel/traverse/lib/context.js:110:19)
    at traverseNode (/myproject/node_modules/@babel/traverse/lib/traverse-node.js:22:17)
    at NodePath.visit (/myproject/node_modules/@babel/traverse/lib/path/context.js:88:52)
    at TraversalContext.visitQueue (/myproject/node_modules/@babel/traverse/lib/context.js:89:16)
    at TraversalContext.visitSingle (/myproject/node_modules/@babel/traverse/lib/context.js:65:19)

I suspect the issue was introduced in packages/babel-helper-define-polyfill-provider/src/visitors/usage.ts by #201.

The path.listKey in this case is arguments, the path.parentPath.node.type is an ObjectExpression, the path.node.type is also an ObjectExpression. Happy to snag any other debugging info that'd be helpful, just a bit stuck on what exactly would help debug here.

@liuxingbaoyu liuxingbaoyu added the bug Something isn't working label Mar 25, 2024
@liuxingbaoyu
Copy link
Member

path+"" can print out the code, which may be helpful to you. :)

@jinliming2
Copy link

I also encountered a very similar error, and the error stack trace appears to originate from the same location. After downgrading @babel/helper-define-polyfill-provider to version 0.5.0 using yarn's resolutions, the error disappeared.

When printing path+"" before the error in the code, I did not observe any particular anomalies. Similarly, printing path.parentPath+"" did not reveal any issues. However, path.parentPath.node was null.

TypeError: Cannot read properties of null (reading 'children')
    at isRemoved (/dev/shm/project/node_modules/@babel/helper-define-polyfill-provider/lib/visitors/usage.js:10:30)
    at isRemoved (/dev/shm/project/node_modules/@babel/helper-define-polyfill-provider/lib/visitors/usage.js:14:10)
    at isRemoved (/dev/shm/project/node_modules/@babel/helper-define-polyfill-provider/lib/visitors/usage.js:14:10)
    at PluginPass.MemberExpression (/dev/shm/project/node_modules/@babel/helper-define-polyfill-provider/lib/visitors/usage.js:75:104)
    at newFn (/dev/shm/project/node_modules/@babel/traverse/lib/visitors.js:160:14)
    at NodePath._call (/dev/shm/project/node_modules/@babel/traverse/lib/path/context.js:46:20)
    at NodePath.call (/dev/shm/project/node_modules/@babel/traverse/lib/path/context.js:36:17)
    at NodePath.visit (/dev/shm/project/node_modules/@babel/traverse/lib/path/context.js:82:31)
    at TraversalContext.visitQueue (/dev/shm/project/node_modules/@babel/traverse/lib/context.js:89:16)
    at TraversalContext.visitSingle (/dev/shm/project/node_modules/@babel/traverse/lib/context.js:65:19)
    at TraversalContext.visit (/dev/shm/project/node_modules/@babel/traverse/lib/context.js:112:19)
    at traverseNode (/dev/shm/project/node_modules/@babel/traverse/lib/traverse-node.js:22:17)
    at NodePath.visit (/dev/shm/project/node_modules/@babel/traverse/lib/path/context.js:88:52)
    at TraversalContext.visitQueue (/dev/shm/project/node_modules/@babel/traverse/lib/context.js:89:16)
    at TraversalContext.visitQueue (/dev/shm/project/node_modules/@babel/traverse/lib/context.js:94:21)
    at TraversalContext.visitSingle (/dev/shm/project/node_modules/@babel/traverse/lib/context.js:65:19)
    at TraversalContext.visit (/dev/shm/project/node_modules/@babel/traverse/lib/context.js:112:19)
    at traverseNode (/dev/shm/project/node_modules/@babel/traverse/lib/traverse-node.js:22:17)
    at traverse (/dev/shm/project/node_modules/@babel/traverse/lib/index.js:52:34)
    at transformFile (/dev/shm/project/node_modules/@babel/core/lib/transformation/index.js:82:31)
    at transformFile.next (<anonymous>)
    at run (/dev/shm/project/node_modules/@babel/core/lib/transformation/index.js:24:12)
    at run.next (<anonymous>)
    at /dev/shm/project/node_modules/@babel/core/lib/transform-ast.js:23:33
    at Generator.next (<anonymous>)
    at evaluateSync (/dev/shm/project/node_modules/gensync/index.js:251:28)
    at sync (/dev/shm/project/node_modules/gensync/index.js:89:14)
    at stopHiding - secret - don't use this - v1 (/dev/shm/project/node_modules/@babel/core/lib/errors/rewrite-stack-trace.js:47:12)
    at transformFromAstSync (/dev/shm/project/node_modules/@babel/core/lib/transform-ast.js:43:83)
    at Object.transform (/dev/shm/project/node_modules/metro-react-native-babel-transformer/src/index.js:251:20)
    at /dev/shm/project/node_modules/metro/src/JSTransformer/worker.js:241:31
    at Generator.next (<anonymous>)
    at asyncGeneratorStep (/dev/shm/project/node_modules/metro/src/JSTransformer/worker.js:75:24)
    at _next (/dev/shm/project/node_modules/metro/src/JSTransformer/worker.js:95:9)
    at /dev/shm/project/node_modules/metro/src/JSTransformer/worker.js:100:7
    at new Promise (<anonymous>)
    at /dev/shm/project/node_modules/metro/src/JSTransformer/worker.js:92:12
    at JsTransformer.transform (/dev/shm/project/node_modules/metro/src/JSTransformer/worker.js:394:7)
    at Object.<anonymous> (/dev/shm/project/node_modules/metro/src/DeltaBundler/Worker.js:102:38)
    at Generator.next (<anonymous>)
    at asyncGeneratorStep (/dev/shm/project/node_modules/metro/src/DeltaBundler/Worker.js:14:24)
    at _next (/dev/shm/project/node_modules/metro/src/DeltaBundler/Worker.js:34:9)
    at /dev/shm/project/node_modules/metro/src/DeltaBundler/Worker.js:39:7
    at new Promise (<anonymous>)
    at Object.<anonymous> (/dev/shm/project/node_modules/metro/src/DeltaBundler/Worker.js:31:12)
    at Object.transform (/dev/shm/project/node_modules/metro/src/DeltaBundler/Worker.js:79:21)
    at execFunction (/dev/shm/project/node_modules/jest-worker/build/workers/processChild.js:155:17)
    at execHelper (/dev/shm/project/node_modules/jest-worker/build/workers/processChild.js:139:5)
    at execMethod (/dev/shm/project/node_modules/jest-worker/build/workers/processChild.js:143:5)
    at process.<anonymous> (/dev/shm/project/node_modules/jest-worker/build/workers/processChild.js:64:7)

@jinliming2
Copy link

I suspect the issue is because path.removed is false, while path.parentPath.removed is true. However, I don't know the reason why.

Why would a node that has already been removed still be traversed by traverse?

@nicolo-ribaudo
Copy link
Member

I assume that the ancestor node is being removed while it's descendants are still being traversed. Would you be able to create a repo that shows the issue? You config + the file it's crashing on would be a good starting point, but you can probably reduce it.

@nicolo-ribaudo
Copy link
Member

I have a workaround in #213, but it's be good to have a reproduction to figure out if the problem has other consequences.

@epmatsw
Copy link
Author

epmatsw commented Apr 19, 2024

Sorry, missed this.

in our case, the offending code is an ObjectMethod that gets replaced by a plugin with an ObjectProperty where the value is an object literal with a function as a value. I can check the removed flags today as well.

@epmatsw
Copy link
Author

epmatsw commented Apr 19, 2024

I can test the fix in our codebase today as well

@jinliming2
Copy link

I haven't found a minimal implementation to reproduce this issue yet, but I tested this solution by modifying files under node_modules in my project, and the issue disappeared.

image

In my case, I have a plugin based on babel-plugin-macros, which utilizes the path.replaceWith / path.replaceWithMultiple API to remove certain layers of nodes in the JSX tree, replacing the current node with its children. Sometimes, it also creates a jsxFragment to wrap some of the children.

This error is triggered in certain specific JSX structures, but not in all JSX structures.

@nicolo-ribaudo
Copy link
Member

Workaround published in @babel/helper-define-polyfill-provider@0.6.2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants