Skip to content

fix(windows): normalize plugin path when resolving better-sqlite3 to avoid false “outside plugin dir” #1192

@TelDragon

Description

@TelDragon

Pre-submission checklist | 提交前检查

  • I have searched existing issues and this hasn't been mentioned before | 我已搜索现有问题,确认此问题尚未被提及
  • I have read the project documentation and confirmed this issue doesn't already exist | 我已阅读项目文档并确认此问题尚未存在
  • This issue is specific to MemOS and not a general software issue | 该问题是针对 MemOS 的,而不是一般软件问题

Bug Description | 问题描述

背景 / 问题

在 Windows 环境下,memos-local-openclaw-plugin 启动时会误报:

better-sqlite3 resolved outside plugin dir
better-sqlite3 native module not found

但实际上模块已安装成功。
根因分析

插件原逻辑用字符串前缀判断模块路径是否在插件目录内:

const resolved = require.resolve("better-sqlite3", { paths: [pluginDir] });
if (!resolved.startsWith(pluginDir)) { ... }

Windows 上常出现路径格式不一致:

pluginDir: C:\Users\...
resolved: C:/Users/...

两者指向同一路径,但字符串比较失败,导致误判“目录外”。

此外,pluginDir 由 new URL(import.meta.url).pathname 推导,也容易引入平台路径格式差异。
修复内容

  1. 统一使用 fileURLToPath(import.meta.url) 获取本地文件路径

改前

const pluginDir = path.dirname(new URL(import.meta.url).pathname);

改后

import { fileURLToPath } from "url";
const pluginDir = path.dirname(fileURLToPath(import.meta.url));

  1. 引入路径标准化函数后再比较

新增:

function normalizeFsPath(p: string): string {
return path.resolve(p).replace(/\/g, "/").toLowerCase();
}

判断逻辑改为:

const resolvedNorm = normalizeFsPath(resolved);
const pluginNorm = normalizeFsPath(pluginDir);
if (!resolvedNorm.startsWith(pluginNorm + "/") && resolvedNorm !== pluginNorm) {
// outside plugin dir
}

这样可以消除:

\ vs /
大小写
相对/绝对路径差异
带来的误判。

影响范围

仅影响 better-sqlite3 路径归属判断
不改变插件业务逻辑(记忆写入/检索/工具接口)
对 Linux/macOS 无破坏性影响(标准化后兼容)

验证结果

在 Windows 上验证通过:

插件不再报 resolved outside plugin dir
Plugins: Errors: 0
memos-local-openclaw-plugin 成功加载

How to Reproduce | 如何重现

1

Environment | 环境信息

1

Additional Context | 其他信息

Willingness to Implement | 实现意愿

  • I'm willing to implement this myself | 我愿意自己解决
  • I would like someone else to implement this | 我希望其他人来解决

Metadata

Metadata

Assignees

Labels

bugSomething isn't workingpendingPending items to be addressed | 待解决事项。

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions