Skip to content

fix: Unicode escapes and dynamic import attributes in instrumented code - #266

Open
dividedmind wants to merge 2 commits into
mainfrom
fix/meriyah-unicode-escapes
Open

dividedmind wants to merge 2 commits into
mainfrom
fix/meriyah-unicode-escapes

Conversation

@dividedmind

@dividedmind dividedmind commented Dec 3, 2026 •

Copy link
Copy Markdown
Collaborator

While looking into #162 (CyberChef tests hanging under appmap-node), I found that four of CyberChef's operation tests also failed under appmap-node, independently of the hang. The cause was in how we regenerate instrumented code: string literals came out with the wrong characters. Fixing that turned up a second code generation bug, which drops the import attributes from dynamic imports. Each fix is in its own commit.

Astral Unicode escapes in string literals

String literals with code point escapes above U+FFFF were changed by instrumentation, e.g. "\u{1F600}" became "}Ȁ". meriyah 5.0.0 parses these escapes to the wrong value, and since we generate the output from the AST, the wrong value replaced the original literal. Template literals were not affected.

This upgrades meriyah to 7.4.4, which parses them correctly (6.x would also work). meriyah 7's ESTree types are stricter, so the AST nodes we build by hand needed some small changes: optional on calls and member expressions, generator on arrow functions, narrower assignment target and operator types, and literal() now only accepts the value types a plain { type, value } node can represent.

meriyah 7 declares engines: node >=20, while we still claim Node 18. I checked that parsing and code generation work on Node 18.20.5, so Node 18 support continues on a best-effort basis rather than breaking outright.

Import attributes on dynamic imports

astring ignores the options argument of ImportExpression, so import("./data.json", { with: { type: "json" } }) was regenerated as import("./data.json"). Node rejects a JSON import without its type attribute, so code that worked on its own crashed under appmap-node. Static imports were fine. This overrides astring's ImportExpression handler in our extended generator to write out the options, and also the import.source() / import.defer() phase, which astring drops the same way.

Testing

  • New tests in src/__tests__/transform.test.ts for both bugs; each fails without its fix.
  • Checked end to end: a script with "\u{1F600}", and a dynamic JSON import with attributes on Node 22, both behave the same with and without appmap-node.
  • Full test suite passes, including Postgres.

This doesn't address the slowdown in #162 itself; that is a separate problem and is still open.

Fixes #172

Assisted-by: Claude:claude-opus-5-5

String literals using code point escapes above U+FFFF, such as
"\u{1F600}", came out of instrumentation as different characters
("}Ȁ"), silently changing program behaviour. meriyah 5.0.0 computes the
wrong value for such escapes, and since we regenerate code from the
AST with astring, the wrong value replaced the original literal. This
broke four of CyberChef's operation tests when run under appmap-node.

Upgrade meriyah to 7, which parses these escapes correctly. Its
stricter ESTree types now require optional on call and member
expressions and generator on arrow functions, narrow assignment
targets and operators, and make Literal a union which a plain
{ type, value } node only satisfies for strings, numbers and booleans,
so the AST builders are adjusted accordingly. No callers construct null,
bigint or regex literals.

meriyah 7 declares Node.js >= 20 in its engines field. It still parses
and round-trips fine on Node 18, which we continue to support.

Assisted-by: Claude:claude-opus-5-5
astring, which we use to regenerate instrumented code, ignores the
options argument of ImportExpression, so
  import("./data.json", { with: { type: "json" } })
became a bare import("./data.json"). Node then rejects the JSON import
for missing its type attribute (ERR_IMPORT_ATTRIBUTE_MISSING), so code
which worked without appmap-node failed under it. Static imports were
not affected.

Override astring's ImportExpression handler in our extended generator
to emit the options, and also the source/defer phase
(import.source(...)), which astring drops the same way.

Assisted-by: Claude:claude-opus-5-5
@dividedmind
dividedmind requested review from kgilpin and a balanced review from Copilot December 3, 2026 12:05
@dividedmind dividedmind self-assigned this Dec 3, 2026

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟢 Approval recommended

No active review issues are reported for the code-regeneration fixes.

No open issues 🎉

What changed in this PR

Fixes code regeneration errors discovered while investigating CyberChef failures under appmap-node. This addresses incorrect output, not the slowdown tracked in #162.

Changes:

  • Upgrades meriyah to preserve astral Unicode escapes.
  • Preserves dynamic import options and phases.
  • Updates AST construction for stricter types and adds regression tests.
File Description
yarn.lock Locks meriyah to 7.4.4.
src/​jsxGenerate.ts Preserves dynamic import options and phases.
src/​hooks/​next.ts Adds the required arrow-function generator flag.
src/​hooks/​instrument.ts Updates arrow nodes and assignment-target typing.
src/​generate.ts Aligns AST helpers with stricter parser types.
src/​__tests__/​transform.test.ts Tests Unicode escapes and dynamic imports.
package.json Upgrades the meriyah dependency.

Review effort: Balanced

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Failing tests in gchq/CyberChef

2 participants