Skip to content

d2js: fix WASM failing to start in Safari 27 workers - #2960

Closed
nelsonjchen wants to merge 2 commits into
d2lang:masterfrom
nelsonjchen:claude/d2-playground-issue-check-6ba05c
Closed

nelsonjchen wants to merge 2 commits into
d2lang:masterfrom
nelsonjchen:claude/d2-playground-issue-check-6ba05c

Conversation

@nelsonjchen

Copy link
Copy Markdown

Fixes #2956. Tracked on the playground side in d2lang/d2-playground#113.

Problem

In Safari 27.2, play.d2lang.com and anything else built on d2.js fail with undefined is not an object (evaluating 'd2.encode'). This isn't a d2.js 0.1.34 / D2 v0.9.0 regression: 0.1.33 fails the same way.

lib/textmeasure imports github.com/yuin/goldmark/extension for Strikethrough and Table. That package compiles its linkify URL regexps at package init, and their {1,256} repetition makes regexp/syntax.(*compiler).compile recurse deeply. A Safari 27 Web Worker has a smaller JS stack than the main thread, so init throws RangeError: Maximum call stack size exceeded. The Go program dies before it registers self.d2, but d2.js still reports ready.

Changes

  • lib/textmeasure/internal/gmext: copies only the Strikethrough and Table extensions from goldmark v1.8.5 (MIT; added to THIRD_PARTY_NOTICES.txt and the d2js copy). goldmark/extension no longer links into the WASM; goldmark/extension/ast is a separate package and stays.
  • gmext/deps_test.go: fails if the GOOS=js d2js dependency graph picks up goldmark/extension again.
  • d2.js workers (browser and node): ready now rejects with D2 WASM failed to initialize: <reason> when the Go program doesn't register d2, instead of resolving with no API.
  • Changelog entries in next.md and the d2.js changelog.

Latest goldmark (v1.8.7) still compiles these regexps eagerly. An upstream change to make them lazy would let us delete the vendored copy later.

Testing

  • Real Safari 27.2 via safaridriver, with a d2.js bundle built from this branch: across 3 cold starts the worker boots and version/compile/render/encode all work on a markdown diagram with a table and strikethrough.
  • d2.js built with the unfixed WASM in Safari 27.2: ready rejects with D2 WASM failed to initialize: Maximum call stack size exceeded.
  • Native SVG output for the same markdown diagram is byte-identical before and after.
  • go test ./lib/textmeasure/... ./d2renderers/... ./e2etests/... ./d2lib/... ./d2js/... pass; bun test test/unit passes (52/52).

🤖 Generated with Claude Code

nelsonjchen and others added 2 commits November 16, 2026 12:30
lib/textmeasure imported github.com/yuin/goldmark/extension for
Strikethrough and Table. That package compiles its linkify URL regexps
at init; their {1,256} repetition recurses deeply in regexp/syntax and
overflows the smaller JS stack of a Safari 27 Web Worker. The Go program
died before registering self.d2, d2.js still reported ready, and every
call failed with "undefined is not an object (evaluating 'd2.encode')".

Copy only the Strikethrough and Table extensions from goldmark v1.8.5
into lib/textmeasure/internal/gmext, add a test that keeps
goldmark/extension out of the d2js WASM dependency graph, and record the
goldmark MIT notice.

Also make the d2.js workers reject init with "D2 WASM failed to
initialize: <reason>" when the Go program fails to register d2, instead
of reporting ready with no API.

Fixes d2lang#2956

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@nelsonjchen

Copy link
Copy Markdown
Author

Closing in favor of #2965

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.

Playground not working

1 participant