Skip to content

Commit 352058e

Browse files
committed
fix: keep packaged acp core plugin aliases
1 parent c82d654 commit 352058e

2 files changed

Lines changed: 66 additions & 2 deletions

File tree

src/plugins/sdk-alias.test.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1752,6 +1752,28 @@ describe("plugin sdk alias helpers", () => {
17521752
).toBe(fs.realpathSync(modelCatalogCore.distFile));
17531753
});
17541754

1755+
it("derives acp-core aliases from packaged root dist when package metadata is absent", () => {
1756+
const fixture = createPluginSdkAliasFixture();
1757+
const sourcePluginEntry = writePluginEntry(
1758+
fixture.root,
1759+
bundledPluginFile("demo", "src/index.ts"),
1760+
);
1761+
const acpRuntimeErrors = path.join(fixture.root, "dist", "acp-core", "runtime", "errors.js");
1762+
mkdirSafeDir(path.dirname(acpRuntimeErrors));
1763+
fs.writeFileSync(acpRuntimeErrors, "export {};\n", "utf-8");
1764+
const cwdWithoutOpenClawPackage = makeTempDir();
1765+
1766+
const aliases = withCwd(cwdWithoutOpenClawPackage, () =>
1767+
withEnv({ NODE_ENV: undefined }, () =>
1768+
buildPluginLoaderAliasMap(sourcePluginEntry, undefined, undefined, "dist"),
1769+
),
1770+
);
1771+
1772+
expect(fs.realpathSync(aliases["@openclaw/acp-core/runtime/errors"] ?? "")).toBe(
1773+
fs.realpathSync(acpRuntimeErrors),
1774+
);
1775+
});
1776+
17551777
it("aliases bundled plugin package public surfaces for source plugin transforms", () => {
17561778
const { fixture, sourceApiPath, sourceRuntimeApiPath } =
17571779
createBundledPluginPackagePublicSurfaceAliasFixture();

src/plugins/sdk-alias.ts

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1041,6 +1041,46 @@ function resolvePackageExportImportPath(value: unknown): string | null {
10411041
: null;
10421042
}
10431043

1044+
function listRootPackagedWorkspacePackageAliasEntries(params: {
1045+
packageRoot: string;
1046+
packageName: string;
1047+
packageDir: string;
1048+
}): WorkspacePackageAliasEntry[] {
1049+
const distRoot = path.join(params.packageRoot, "dist", params.packageDir);
1050+
if (!fs.existsSync(distRoot)) {
1051+
return [];
1052+
}
1053+
const entries: WorkspacePackageAliasEntry[] = [];
1054+
const visit = (dir: string, prefix = "") => {
1055+
for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
1056+
const relativePath = prefix ? path.join(prefix, entry.name) : entry.name;
1057+
const fullPath = path.join(dir, entry.name);
1058+
if (entry.isDirectory()) {
1059+
visit(fullPath, relativePath);
1060+
continue;
1061+
}
1062+
if (!entry.isFile() || !relativePath.endsWith(".js")) {
1063+
continue;
1064+
}
1065+
const normalizedRelativePath = relativePath.split(path.sep).join("/");
1066+
const subpath =
1067+
normalizedRelativePath === "index.js" ? "" : normalizedRelativePath.slice(0, -".js".length);
1068+
if (subpath.includes("..")) {
1069+
continue;
1070+
}
1071+
entries.push({
1072+
packageName: params.packageName,
1073+
packageDir: params.packageDir,
1074+
subpath,
1075+
srcFile: `${subpath || "index"}.ts`,
1076+
distFile: relativePath,
1077+
});
1078+
}
1079+
};
1080+
visit(distRoot);
1081+
return entries.toSorted((a, b) => a.subpath.localeCompare(b.subpath));
1082+
}
1083+
10441084
export function listWorkspacePackageExportAliasEntries(params: {
10451085
packageRoot: string;
10461086
packageName: string;
@@ -1062,7 +1102,7 @@ export function listWorkspacePackageExportAliasEntries(params: {
10621102
: null);
10631103
const exports = packageJson?.exports;
10641104
if (!exports || typeof exports !== "object" || Array.isArray(exports)) {
1065-
return [];
1105+
return listRootPackagedWorkspacePackageAliasEntries(params);
10661106
}
10671107
const entries: WorkspacePackageAliasEntry[] = [];
10681108
for (const [exportKey, value] of Object.entries(exports)) {
@@ -1081,7 +1121,9 @@ export function listWorkspacePackageExportAliasEntries(params: {
10811121
distFile,
10821122
});
10831123
}
1084-
return entries.toSorted((a, b) => a.subpath.localeCompare(b.subpath));
1124+
return entries.length > 0
1125+
? entries.toSorted((a, b) => a.subpath.localeCompare(b.subpath))
1126+
: listRootPackagedWorkspacePackageAliasEntries(params);
10851127
}
10861128

10871129
function isUsableDistPluginSdkArtifact(candidate: string): boolean {

0 commit comments

Comments
 (0)