Fix live debugger this capture before super by watson · Pull Request #406 · DataDog/build-plugins · GitHub
Skip to content

Fix live debugger this capture before super#406

Merged
gh-worker-dd-mergequeue-cf854d[bot] merged 3 commits into
masterfrom
watson/fix-capture-before-super
Jun 12, 2026
Merged

Fix live debugger this capture before super#406
gh-worker-dd-mergequeue-cf854d[bot] merged 3 commits into
masterfrom
watson/fix-capture-before-super

Conversation

@watson

@watson watson commented Jun 11, 2026

Copy link
Copy Markdown
Contributor

What and why?

Fix Live Debugger instrumentation for arrows that execute before super() completes in derived class constructors. The transform previously injected probe calls that accessed this, which can crash valid derived-constructor code while super() arguments are still being evaluated.

Before instrumentation, this pattern is valid:

class B extends A {
  constructor(items) {
    super(items.map((x) => x * 2));
  }
}

Before this fix, probing the arrow could inject code that accessed constructor this before super() returned:

super(items.map((x) => {
  if ($dd_p0) $dd_entry($dd_p0, this, $dd_e0());
  const $dd_rv0 = x * 2;
  if ($dd_p0) $dd_return($dd_p0, $dd_rv0, this, $dd_e0());
  return $dd_rv0;
}));

After this fix, affected arrows use a constructor-scoped $dd_t alias that is assigned from super():

constructor(items) {
  let $dd_t;
  ($dd_t = super(items.map((x) => {
    if ($dd_p0) $dd_entry($dd_p0, $dd_t, $dd_e0());
    const $dd_rv0 = x * 2;
    if ($dd_p0) $dd_return($dd_p0, $dd_rv0, $dd_t, $dd_e0());
    return $dd_rv0;
  })));
}

How?

The transform now detects arrows whose lexical this comes from a derived constructor, emits one constructor-scoped $dd_t alias, and assigns it from wrapped super() calls. Affected arrows pass $dd_t to $dd_entry, $dd_return, and $dd_throw; arrows outside derived constructors and non-arrow function expressions continue to use their normal this.

Added regression coverage for the reported super(items.map((x) => x * 2)) pattern, a guard for non-arrow function expressions, and an exact-output smoke test for the generated $dd_t shape.

Verified with:

  • yarn test:unit packages/plugins/live-debugger/src/transform/index.test.ts
  • yarn lint packages/plugins/live-debugger/src/transform/index.ts packages/plugins/live-debugger/src/transform/index.test.ts
  • yarn cli typecheck-workspaces --files packages/plugins/live-debugger/src/transform/index.ts --files packages/plugins/live-debugger/src/transform/index.test.ts

Live Debugger instrumentation always passed `this` to probe hooks. For arrows
that run while evaluating `super()` arguments in derived constructors, that
turns otherwise valid code into a ReferenceError because constructor `this` is
not initialized yet.

Track arrows whose lexical `this` comes from a derived constructor, create a
constructor-scoped `$dd_t` alias, and assign it from wrapped `super()` calls.
Those arrows pass `$dd_t` to entry/return/throw hooks, while other arrows and
non-arrow functions keep their normal `this` behavior.

Add regression and exact-output coverage for the derived constructor case.
@watson watson requested review from a team and yoannmoinet as code owners June 11, 2026 04:33

watson commented Jun 11, 2026

Copy link
Copy Markdown
Contributor Author

@tylfin

tylfin commented Jun 12, 2026

Copy link
Copy Markdown
Member

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

To use Codex here, create a Codex account and connect to github.

@tylfin tylfin self-assigned this Jun 12, 2026
Comment thread packages/plugins/live-debugger/src/transform/index.ts
@watson watson requested a review from tylfin June 12, 2026 19:14
@watson

watson commented Jun 12, 2026

Copy link
Copy Markdown
Contributor Author

/merge

@gh-worker-devflow-routing-ef8351

gh-worker-devflow-routing-ef8351 Bot commented Jun 12, 2026

Copy link
Copy Markdown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants