question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Regression: path.parentPath.get(path.listKey) can clobber a traversal's visitors with those from a separate traversal in the global path cache

See original GitHub issue

Bug Report

  • I would like to work on a fix!

Input Code

const {parse} = require('@babel/core')
const generate = require('@babel/generator').default
const traverse = require('@babel/traverse').default

const code = `
import { Foo } from './Foo'
import { Bar } from './Bar'
`

const presets = [
  ['@babel/preset-env', { targets: { node: 'current' } }],
]

const ast = parse(code, {plugins, presets})

traverse(ast, {
  Program(path) {
    path.traverse({
      ImportDeclaration: {
        enter(path) {
          console.log('ENTER', generate(path.node).code)
          if (path.node.source.value === './Bar')
            path.parentPath.get(path.listKey)
        },
        exit(path) {
          console.log('EXIT ', generate(path.node).code)
        }
      }
    })
  }
})

Expected behavior

I haven’t bisected to find the version that introduced the regression yet, but with @babel/core, @babel/traverse, and @babel/preset-env 7.4.5, the exit visitor is called for both import statements:

ENTER import { Foo } from './Foo';
EXIT  import { Foo } from './Foo';
ENTER import { Bar } from './Bar';
EXIT  import { Bar } from './Bar';

Current behavior

With these versions: ├─ @babel/core@7.12.10 ├─ @babel/preset-env@7.12.11 └─ @babel/traverse@7.12.12

The exit visitor for the second import statement isn’t called because path.parentPath.get(path.listKey) has the side effect of replacing path.opts with those of the parent traversal (which only has a Program visitor)

ENTER import { Foo } from './Foo';
EXIT  import { Foo } from './Foo';
ENTER import { Bar } from './Bar';

(missing EXIT import { Bar } from './Bar')

Additional context

babel-plugin-flow-runtime is no longer working after I upgraded @babel/*, and I uncovered this issue debugging it.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:11 (10 by maintainers)

github_iconTop GitHub Comments

1reaction
jedwards1211commented, Nov 18, 2021

Huh I see. FWIW, I wonder if a different API design would be possible where the context information is passed around instead of being a property on the paths that gets mutated. This issue certainly wasn’t a showstopper for me but I did burn a lot of time in a painful debugging session on it.

0reactions
jedwards1211commented, Nov 18, 2021

@JLHwung oh interesting.

I noticed that visitor is already an argument to path.traverse etc. And this issue involved traversal getting the visitors from path.opts in some cases, and that getting blown away by context changes. So aside from changes in that PR, I wonder if it would be possible for traversal to always use the visitor passed around via function arguments instead of anything related to context?

Read more comments on GitHub >

github_iconTop Results From Across the Web

What is directory traversal, and how to prevent it? - PortSwigger
In this section, we'll explain what directory traversal is, describe how to carry out path traversal attacks and circumvent common obstacles, and spell...
Read more >
Path Exploration in Google Analytics 4 (practical ... - YouTube
Learn how to use Path Exploration reports in Google Analytics 4 and see what your users do and how they behave on a...
Read more >
The Path for Testing Path Traversal Vulnerabilities with Python
I have noticed an odd behavior in requests module in Python, which uses urllib3. I inspected the root cause via regression testing and...
Read more >
Understanding directory symlinks traversals and the parent ...
bash "knows" about symlinks and tracks this info when you use a symlink to enter a directory. You can check this by doing...
Read more >
repository-root.el - EmacsWiki
Download ;;; repository-root.el --- deduce the repository root directory for a given file ;; Copyright (C) 2008-2013 Avi Rozen ;; Author: ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found