Node + tsx "__name is not a function" 崩溃

摘要

通过 tsx 运行 Mayros 在启动时失败,出现:

[mayros] Failed to start CLI: TypeError: __name is not a function
    at createSubsystemLogger (.../src/logging/subsystem.ts:203:25)
    at .../src/agents/auth-profiles/constants.ts:25:20

这是在将开发脚本从 Bun 切换到 tsx 后开始的(提交 2871657e,2026-01-06)。相同的运行时路径在 Bun 下工作。

环境

  • Node:v25.x(在 v25.3.0 上观察到)
  • tsx:4.21.0
  • OS:macOS(在运行 Node 25 的其他平台上也可能重现)

重现(仅 Node)

bash
# 在仓库根目录
node --version
pnpm install
node --import tsx src/entry.ts status

仓库中的最小重现

bash
node --import tsx scripts/repro/tsx-name-repro.ts

Node 版本检查

  • Node 25.3.0:失败
  • Node 22.22.0(Homebrew node@22):失败
  • Node 24:此处尚未安装;需要验证

注意 / 假设

  • tsx 使用 esbuild 转换 TS/ESM。esbuild 的 keepNames 发出 __name 辅助函数并用 __name(...) 包装函数定义。
  • 崩溃表明 __name 存在但在运行时不是函数,这意味着辅助函数在 Node 25 加载器路径中对此模块丢失或被覆盖。
  • 在其他 esbuild 使用者中已报告类似的 __name 辅助函数问题,当辅助函数丢失或重写时。

回归历史

  • 2871657e(2026-01-06):脚本从 Bun 更改为 tsx,使 Bun 可选。
  • 在那之前(Bun 路径),mayros statusgateway:watch 有效。

解决方法

  • 使用 Bun 进行开发脚本(当前临时恢复)。

  • 使用 Node + tsc watch,然后运行编译的输出:

    bash
    pnpm exec tsc --watch --preserveWatchOutput
    node --watch mayros.mjs status
    
  • 本地确认:pnpm exec tsc -p tsconfig.json + node mayros.mjs status 在 Node 25 上有效。

  • 如果可能,在 TS 加载器中禁用 esbuild keepNames(防止 __name 辅助函数插入);tsx 目前不公开此功能。

  • 使用 tsx 测试 Node LTS(22/24),看看问题是否是 Node 25 特定的。

参考

后续步骤

  • 在 Node 22/24 上重现以确认 Node 25 回归。
  • 测试 tsx nightly 或固定到早期版本(如果存在已知回归)。
  • 如果在 Node LTS 上重现,使用 __name 堆栈跟踪提交一个最小重现到上游。