Skip to content

fix(tokenless): replace __dirname with import.meta.dirname for ESM compatibility#878

Open
shiloong wants to merge 1 commit into
alibaba:mainfrom
shiloong:bugfix
Open

fix(tokenless): replace __dirname with import.meta.dirname for ESM compatibility#878
shiloong wants to merge 1 commit into
alibaba:mainfrom
shiloong:bugfix

Conversation

@shiloong

Copy link
Copy Markdown
Collaborator

Description

OpenClaw 2026.6.5 (5181e4f) fails to load tokenless plugin at
registration with ReferenceError: __dirname is not defined.

The openclaw adapter package.json declares type:module (ESM) and
tsconfig outputs Node16 module format, which produces ESM import
syntax in dist/index.js.  However loadToolCategories() used the
CJS global __dirname to resolve tool_categories.json paths — this
variable does not exist in ESM modules.

Replace the two __dirname references with import.meta.dirname
(Node 20.11+; OpenClaw 2026.6.5 runs on Node 22+).  Other openclaw
plugins (sec-core, ws-ckpt, agent-memory) were checked and have
no bare __dirname usage — no similar bug exists elsewhere.

Related Issue

fixes # openclaw plugins install errors

Type of Change

  • Bug fix (non-breaking change that fixes an issue)
  • New feature (non-breaking change that adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation update
  • Refactoring (no functional change)
  • Performance improvement
  • CI/CD or build changes

Scope

  • cosh (copilot-shell)
  • sec-core (agent-sec-core)
  • skill (os-skills)
  • sight (agentsight)
  • tokenless (tokenless)
  • ckpt (ws-ckpt)
  • memory (agent-memory)
  • anolisa (anolisa-cli)
  • Multiple / Project-wide

Checklist

  • I have read the Contributing Guide
  • My code follows the project's code style
  • I have added tests that prove my fix is effective or that my feature works
  • I have updated the documentation accordingly
  • For cosh: Lint passes, type check passes, and tests pass
  • For sec-core (Rust): cargo clippy -- -D warnings and cargo fmt --check pass
  • For sec-core (Python): Ruff format and pytest pass
  • For skill: Skill directory structure is valid and shell scripts pass syntax check
  • For sight: cargo clippy -- -D warnings and cargo fmt --check pass
  • For tokenless: cargo clippy -- -D warnings and cargo fmt --check pass
  • For memory (Linux only): cargo clippy --all-targets -- -D warnings, cargo fmt --check, and cargo test pass
  • For anolisa: cargo clippy --all-targets --locked -- -D warnings, cargo fmt --all --check, and cargo test --locked pass
  • Lock files are up to date (package-lock.json / Cargo.lock)

Testing

Additional Notes

@shiloong shiloong requested a review from Forrest-ly as a code owner June 12, 2026 09:20
@github-actions github-actions Bot added the component:tokenless src/tokenless/ label Jun 12, 2026
@Forrest-ly

Copy link
Copy Markdown
Collaborator

Code Review: __dirnameimport.meta.dirname ESM 兼容性修复

变更概要

loadToolCategories() 中的两处 __dirname 替换为 import.meta.dirname,修复 ESM 模式下 ReferenceError: __dirname is not defined 的问题。

诊断确认

  • package.json.in 声明了 "type": "module",编译产物为 ESM
  • tsconfig.json 使用 module: "Node16",产出 ESM import 语法
  • __dirname 在 ESM 中不存在,运行时必定 ReferenceError
  • 当前虽有 fallback 硬编码兜底不会整体崩溃,但基于文件的 tool_categories.json 加载路径实际上已失效

评审结论:Approve(附 1 个建议项)

修复方向正确,改动最小化,不影响其他模块。以下是一个非阻塞建议:


建议:确认 import.meta.dirname 的类型安全

当前 tsconfig.json 配置:

"target": "ES2022"

import.meta.dirnameES2023 引入的特性,target: ES2022 的 lib 类型定义中不包含该属性。虽然 @types/node (>=22) 提供了 Node.js 侧的 ambient 类型补全,tsc 可能不会报错,但存在隐患:

  1. 如果未来 @types/node 降级或 skipLibCheck 被关闭,可能出现类型错误
  2. 语义上 target 声明与实际运行时依赖不一致

建议:将 target 提升到 "ES2023" 或添加 "lib": ["ES2023"],使类型声明与实际使用对齐。这是非阻塞项——当前在 Node 22+ 运行时上功能正常,PR 描述也明确声明了 Node 22+ 最低版本。


其他检查项

检查项 结果
是否有其他 __dirname 残留 ✅ 无,仅此两处
其他 adapter 是否受影响 ✅ 无,仅 openclaw 是 TS adapter
运行时崩溃风险 ✅ 无,fallback 兜底 + Node 22+ 支持 import.meta.dirname
向后兼容性 ✅ 无破坏,Node 22+ 为 OpenClaw 最低版本

…law plugin

OpenClaw 2026.6.5 (5181e4f) fails to load tokenless plugin at
registration with ReferenceError: __dirname is not defined.

The openclaw adapter package.json declares type:module (ESM) and
tsconfig outputs Node16 module format, which produces ESM import
syntax in dist/index.js.  However loadToolCategories() used the
CJS global __dirname to resolve tool_categories.json paths — this
variable does not exist in ESM modules.

Replace the two __dirname references with import.meta.dirname
(Node 20.11+; OpenClaw 2026.6.5 runs on Node 22+).  Other openclaw
plugins (sec-core, ws-ckpt, agent-memory) were checked and have
no bare __dirname usage — no similar bug exists elsewhere.

Signed-off-by: Shile Zhang <shile.zhang@linux.alibaba.com>
@shiloong

Copy link
Copy Markdown
Collaborator Author

Code Review: __dirnameimport.meta.dirname ESM 兼容性修复

变更概要

loadToolCategories() 中的两处 __dirname 替换为 import.meta.dirname,修复 ESM 模式下 ReferenceError: __dirname is not defined 的问题。

诊断确认

  • package.json.in 声明了 "type": "module",编译产物为 ESM
  • tsconfig.json 使用 module: "Node16",产出 ESM import 语法
  • __dirname 在 ESM 中不存在,运行时必定 ReferenceError
  • 当前虽有 fallback 硬编码兜底不会整体崩溃,但基于文件的 tool_categories.json 加载路径实际上已失效

评审结论:Approve(附 1 个建议项)

修复方向正确,改动最小化,不影响其他模块。以下是一个非阻塞建议:

建议:确认 import.meta.dirname 的类型安全

当前 tsconfig.json 配置:

"target": "ES2022"

import.meta.dirnameES2023 引入的特性,target: ES2022 的 lib 类型定义中不包含该属性。虽然 @types/node (>=22) 提供了 Node.js 侧的 ambient 类型补全,tsc 可能不会报错,但存在隐患:

  1. 如果未来 @types/node 降级或 skipLibCheck 被关闭,可能出现类型错误
  2. 语义上 target 声明与实际运行时依赖不一致

建议:将 target 提升到 "ES2023" 或添加 "lib": ["ES2023"],使类型声明与实际使用对齐。这是非阻塞项——当前在 Node 22+ 运行时上功能正常,PR 描述也明确声明了 Node 22+ 最低版本。

其他检查项

检查项 结果
是否有其他 __dirname 残留 ✅ 无,仅此两处
其他 adapter 是否受影响 ✅ 无,仅 openclaw 是 TS adapter
运行时崩溃风险 ✅ 无,fallback 兜底 + Node 22+ 支持 import.meta.dirname
向后兼容性 ✅ 无破坏,Node 22+ 为 OpenClaw 最低版本

fixed, thx!

@Forrest-ly

Copy link
Copy Markdown
Collaborator

Code Review — PR #878

结论:✅ Approve

根因分析清晰:ESM 模块中 __dirname 不存在,import.meta.dirname 是 Node 20.11+ 的标准替代。target: ES2023 升级安全,Node 22 完整支持。

建议(非阻塞):

  1. 可补充一个最简冒烟测试(如 node -e "import('./dist/index.js')" 确保加载不报错),防止后续重构回退
  2. import.meta.dirname 的类型支持主要取决于 lib 配置和 @types/node 版本,而非 target。若后续 TS 编译报类型错误,可确认 lib 包含 ES2023

⚠️ 注意:本 PR 的 openclaw/index.ts 变更已包含在 PR #879 中(同一 commit)。如果 #879 先合并,本 PR 会产生冲突。建议先合并 #878 再 rebase #879,或关闭本 PR 由 #879 覆盖。

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

Labels

component:tokenless src/tokenless/

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants