Thanks to visit codestin.com
Credit goes to github.com

Skip to content

fix: Regression of new traverse with transform-modules-amd#17001

Closed
liuxingbaoyu wants to merge 6 commits into
babel:mainfrom
liuxingbaoyu:fix-amd-scope
Closed

fix: Regression of new traverse with transform-modules-amd#17001
liuxingbaoyu wants to merge 6 commits into
babel:mainfrom
liuxingbaoyu:fix-amd-scope

Conversation

@liuxingbaoyu
Copy link
Copy Markdown
Member

Q                       A
Fixed Issues? Fixes #16999
Patch: Bug Fix?
Major: Breaking Change?
Minor: New Feature?
Tests Added + Pass? Yes
Documentation PR Link
Any Dependency Changes?
License MIT

#16923 caused the regression, but the source of the problem was not there.
The source of the problem was that transform-modules-amd corrupted the container of the paths in the traversal queue.

const { body, directives } = path.node;
path.node.directives = [];
path.node.body = [];
const amdFactoryCall = path
.pushContainer("body", wrapper)[0]
.get("expression") as NodePath<t.CallExpression>;
const amdFactoryCallArgs = amdFactoryCall.get("arguments");
const amdFactory = (
amdFactoryCallArgs[
amdFactoryCallArgs.length - 1
] as NodePath<t.FunctionExpression>
).get("body");
amdFactory.pushContainer("directives", directives);
amdFactory.pushContainer("body", body);
changed Program.body.
resync.call(path);
causes nodes added to Program.body via pushContainer to have wrong container and key in subsequent traversals.
Then the crash happens when we use the wrong container.

Maybe we also add a temporary fix to @babel/traverse in Babel 7? I'm not sure how many people will accidentally use the new traverse with the old transform-modules-amd.

@liuxingbaoyu liuxingbaoyu added PR: Bug Fix 🐛 A type of pull request used for our changelog categories i: regression labels Dec 4, 2024
@babel-bot
Copy link
Copy Markdown
Collaborator

babel-bot commented Dec 4, 2024

Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/58416

@liuxingbaoyu
Copy link
Copy Markdown
Member Author

liuxingbaoyu commented Dec 4, 2024

Maybe we also add a temporary fix to @babel/traverse in Babel 7? I'm not sure how many people will accidentally use the new traverse with the old transform-modules-amd.

Yes, we should, because it seems that the act of arbitrarily moving nodes to children will cause this problem.
The CI issue at #16965 is also related to this.

@nicolo-ribaudo
Copy link
Copy Markdown
Member

How would you feel about reverting #16923, and re-landing it (together with this PR) for babel 8?

Identifier(path) {
if (!bool) {
bool = true;
path.parentPath.parentPath.pushContainer("body", t.functionDeclaration(t.identifier("fn"), [t.identifier("x")], t.blockStatement([])));
Copy link
Copy Markdown
Contributor

@JLHwung JLHwung Dec 4, 2024

Choose a reason for hiding this comment

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

Can you add some assertions to this test so that it will fail without the changes in the amd transform? Currently this test is already passing given only the babel-traverse changes.

@JLHwung
Copy link
Copy Markdown
Contributor

JLHwung commented Dec 4, 2024

Since #16998 is introducing a currently breaking e2e test, could you rebase this PR onto #16998 so that we can be confident that there is no other breaking changes for ember?

@liuxingbaoyu
Copy link
Copy Markdown
Member Author

How would you feel about reverting #16923, and re-landing it (together with this PR) for babel 8?

I'm slightly inclined to try this before reverting, because I want to accumulate significant performance changes before v7.27.0. :)

@liuxingbaoyu
Copy link
Copy Markdown
Member Author

Or we revert it and only count the performance changes for Babel 8, since #16965 seems a bit riskier than this.🤦‍♂️

@nicolo-ribaudo
Copy link
Copy Markdown
Member

nicolo-ribaudo commented Dec 5, 2024

The reason I prefer reverting is that it's very common for people to be on an older version of some package, and in this case the fix in transform-modules-amd feels very hacky. It's @babel/traverse that should be responsible of keeping the correct AST metadata (maybe in pushContainer?), and not something that plugins should manually patch. I'm thus worried that here we found one breakage, but it could potentially break any other plugin that moves nodes around.

As an alternative, I would prefer a fix that only involves @babel/traverse.

@liuxingbaoyu
Copy link
Copy Markdown
Member Author

As an alternative, I would prefer a fix that only involves @babel/traverse.

Yes, I submitted the fix for only traverse in the second commit. :)

305d3be (#17001)

But now there are two issues that I need to test whether they have been fixed by the two PRs. So revert may also be a good choice.
#17004
istio/istio.io#16053

@JLHwung JLHwung mentioned this pull request Dec 5, 2024
@liuxingbaoyu
Copy link
Copy Markdown
Member Author

I investigated the bug with babel-minify in istio/istio.io#16053 and luckily it was fixed by this patch as well.
The reason was https://github.com/babel/minify/blob/8f9c7c0a32103653bd6793d0ce44730f937e1ddf/packages/babel-plugin-minify-mangle-names/src/index.js#L77-L80 which cleared the cache but still held programPath.
Before this patch we looked up the cache and created a new programPath, now we still use the old programPath.

@github-actions github-actions Bot added the outdated A closed issue/PR that is archived due to age. Recommended to make a new issue label Apr 11, 2025
@github-actions github-actions Bot locked as resolved and limited conversation to collaborators Apr 11, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

i: regression outdated A closed issue/PR that is archived due to age. Recommended to make a new issue PR: Bug Fix 🐛 A type of pull request used for our changelog categories

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: @babel/traverse 7.26.3 breaks ember

5 participants