Skip to content

bundler: remove mutable Rootname global; derive and return the entry name (#95) - #118

Merged
argonui merged 1 commit into
mainfrom
refactor/bundler-rootname
Aug 4, 2026
Merged

bundler: remove mutable Rootname global; derive and return the entry name (#95)#118
argonui merged 1 commit into
mainfrom
refactor/bundler-rootname

Conversation

@argonui

@argonui argonui commented Aug 3, 2026

Copy link
Copy Markdown
Owner

Summary

bundler.Rootname was a mutable package global that UnbundleAll rewrote at runtime, while findNextBundledScript hardcoded "__root" in its terminating regex. The two disagreed, producing the two failures described in #95:

  • Silent module loss — a bundle whose entry module isn't __root lost its alphabetically-last module, with no error, because that module's __bundle_register block is only matched when followed by the literal return __bundle_require("__root").
  • Cross-call contamination — the rewritten global persisted, so a later Bundle() registered the root under the stale name while metasuffix still emitted return __bundle_require("__root"), producing Lua that references an unregistered module.

Fix

Follows the issue's suggested approach:

  • Rootname is now an immutable const — the canonical entry name and the default when a bundle declares none.
  • UnbundleAll detects the actual entry name locally from the return __bundle_require("...") line, threads it into the scan regex (via regexp.QuoteMeta, no longer hardcoded), and returns it: (map[string]string, string, error).
  • UnbundleAllXML returns the canonical Rootname so Lua and XML unbundling stay uniform behind the shared handler func pointer.
  • Callers updated: Unbundle, AnalyzeBundle, handler.WhileWritingToFile (no longer references bundler.Rootname), and the affected tests.

Tests

Adds two bundler tests over a bundle with a foreign entry name (custom_entry):

  • TestUnbundleForeignEntryName — asserts the alphabetically-last module (zzz) survives and the returned root name is correct (failure mode a).
  • TestNoCrossCallContamination — unbundles the foreign bundle, then asserts a subsequent Bundle still emits __root and round-trips (failure mode b).

Verified the first test fails against the pre-fix hardcoded regex.

go build ./..., go vet ./..., and go test ./... all pass.

Fixes #95

UnbundleAll stored the detected entry-module name in a mutable package
global (Rootname) while findNextBundledScript hardcoded "__root" in its
terminating regex. The two disagreed, causing (a) silent loss of a
bundle's alphabetically-last module whenever its entry was not named
"__root", and (b) persistent cross-call contamination that corrupted the
entry line emitted by later Bundle calls.

Rootname is now an immutable const (the canonical entry name and default).
UnbundleAll detects the actual entry name locally, threads it into the
scan regex, and returns it; UnbundleAllXML returns the canonical name so
Lua and XML unbundling stay uniform. Callers (Unbundle, the shared
handler, and tests) use the returned name instead of the global.

Adds bundler tests reproducing both failure modes with a foreign entry
name.

Fixes #95

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@argonui
argonui force-pushed the refactor/bundler-rootname branch from 4a9b1f2 to d490839 Compare August 4, 2026 02:24
@argonui
argonui merged commit e515d38 into main Aug 4, 2026
1 check failed
@argonui
argonui deleted the refactor/bundler-rootname branch August 4, 2026 02:24
argonui added a commit that referenced this pull request Aug 4, 2026
#116 (handler tests) and #118 (bundler Rootname refactor) each merged clean
against a main that lacked the other, but together they don't compile: the
test called h.unbundle with two return values while #118 changed it to three.
Discard the new root-name return in the test.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
argonui added a commit that referenced this pull request Aug 4, 2026
fix: handler_test.go 3-value unbundle signature (unbreak main after #116+#118)
argonui added a commit that referenced this pull request Aug 4, 2026
fix: e2e_test.go 3-value UnbundleAll (unbreak main after #118+#119)
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.

Bundler: mutable Rootname global causes silent module loss and cross-contamination

1 participant