From 04c07dbdec9ce205dcb51dea271581e1bdb2ee50 Mon Sep 17 00:00:00 2001 From: _Kerman Date: Mon, 13 Jul 2026 15:53:06 +0800 Subject: [PATCH 1/2] chore: drop #/ import array fallbacks and custom resolution plugins Replace the "#/*": ["./src/*.ts", "./src/*/index.ts"] array fallback in every package's imports field with a single "./src/*.ts" target plus explicit entries for directory-style imports (agent-core, kimi-code). Directory-style #/foo imports now resolve through Node's standard single-target subpath imports, so the custom resolution workarounds are no longer needed: - delete build/hash-imports-{loader,plugin,register-loader}.mjs - remove the inlined hashImportsPlugin from the agent-core-v2, kap-server, server-e2e and klient vitest configs - remove hashImportsPlugin from both kimi-code tsdown configs - drop register-hash-imports-loader.mjs from the dev:kap-server script --- apps/kimi-code/package.json | 9 +-- apps/kimi-code/tsdown.config.ts | 3 +- apps/kimi-code/tsdown.native.config.ts | 3 +- apps/vis/server/package.json | 5 +- build/hash-imports-loader.mjs | 79 ----------------------- build/hash-imports-plugin.mjs | 84 ------------------------- build/register-hash-imports-loader.mjs | 8 --- packages/acp-adapter/package.json | 5 +- packages/agent-core-v2/package.json | 5 +- packages/agent-core-v2/vitest.config.ts | 44 +------------ packages/agent-core/package.json | 21 +++++-- packages/kaos/package.json | 5 +- packages/kap-server/vitest.config.ts | 48 ++------------ packages/klient/package.json | 5 +- packages/klient/vitest.config.ts | 42 +------------ packages/kosong/package.json | 5 +- packages/migration-legacy/package.json | 5 +- packages/minidb/package.json | 5 +- packages/node-sdk/package.json | 5 +- packages/oauth/package.json | 5 +- packages/pi-tui/package.json | 5 +- packages/protocol/package.json | 5 +- packages/server-e2e/package.json | 5 +- packages/server-e2e/vitest.config.ts | 45 +------------ packages/telemetry/package.json | 5 +- 25 files changed, 45 insertions(+), 411 deletions(-) delete mode 100644 build/hash-imports-loader.mjs delete mode 100644 build/hash-imports-plugin.mjs delete mode 100644 build/register-hash-imports-loader.mjs diff --git a/apps/kimi-code/package.json b/apps/kimi-code/package.json index 4cc9611253..2346d90ccb 100644 --- a/apps/kimi-code/package.json +++ b/apps/kimi-code/package.json @@ -36,13 +36,10 @@ "type": "module", "imports": { "#/tui/theme": "./src/tui/theme/index.ts", + "#/tui/commands": "./src/tui/commands/index.ts", "#/cli/sub/server": "./src/cli/sub/server/index.ts", "#/cli/sub/server/*": "./src/cli/sub/server/*.ts", - "#/*": [ - "./src/*.ts", - "./src/*/index.ts", - "./src/*.d.ts" - ] + "#/*": "./src/*.ts" }, "publishConfig": { "access": "public", @@ -63,7 +60,7 @@ "dev": "node scripts/dev.mjs", "dev:cli-only": "tsx --import ../../build/register-raw-text-loader.mjs ./src/main.ts", "dev:server": "tsx --tsconfig ./tsconfig.dev.json --import ../../build/register-raw-text-loader.mjs ./src/main.ts server run --foreground", - "dev:kap-server": "KIMI_CODE_EXPERIMENTAL_FLAG=1 tsx --tsconfig ./tsconfig.dev.json --import ../../build/register-raw-text-loader.mjs --import ../../build/register-hash-imports-loader.mjs ./src/main.ts server run --foreground", + "dev:kap-server": "KIMI_CODE_EXPERIMENTAL_FLAG=1 tsx --tsconfig ./tsconfig.dev.json --import ../../build/register-raw-text-loader.mjs ./src/main.ts server run --foreground", "dev:server:restart": "node scripts/dev-server-restart.mjs", "dev:plugin-marketplace": "node scripts/dev-plugin-marketplace-server.mjs", "build:plugin-marketplace": "node scripts/build-plugin-marketplace-cdn.mjs", diff --git a/apps/kimi-code/tsdown.config.ts b/apps/kimi-code/tsdown.config.ts index af6a58927e..858aeeb48c 100644 --- a/apps/kimi-code/tsdown.config.ts +++ b/apps/kimi-code/tsdown.config.ts @@ -2,7 +2,6 @@ import { resolve } from 'node:path'; import { defineConfig } from 'tsdown'; -import { hashImportsPlugin } from '../../build/hash-imports-plugin.mjs'; import { rawTextPlugin } from '../../build/raw-text-plugin.mjs'; import { BUILT_IN_CATALOG_DEFINE, builtInCatalogDefine } from './scripts/built-in-catalog.mjs'; @@ -24,7 +23,7 @@ export default defineConfig({ 'const __dirname = __cjsShimDirname(__filename);', ].join('\n'), }, - plugins: [hashImportsPlugin(), rawTextPlugin()], + plugins: [rawTextPlugin()], alias: { '@': resolve(appRoot, 'src'), }, diff --git a/apps/kimi-code/tsdown.native.config.ts b/apps/kimi-code/tsdown.native.config.ts index 622dd5fd97..c1008cb616 100644 --- a/apps/kimi-code/tsdown.native.config.ts +++ b/apps/kimi-code/tsdown.native.config.ts @@ -4,7 +4,6 @@ import { resolve } from 'node:path'; import { defineConfig } from 'tsdown'; -import { hashImportsPlugin } from '../../build/hash-imports-plugin.mjs'; import { rawTextPlugin } from '../../build/raw-text-plugin.mjs'; import { BUILT_IN_CATALOG_DEFINE, builtInCatalogDefine } from './scripts/built-in-catalog.mjs'; @@ -43,7 +42,7 @@ export default defineConfig({ platform: 'node', target: 'node24', banner: { js: '#!/usr/bin/env node' }, - plugins: [hashImportsPlugin(), rawTextPlugin()], + plugins: [rawTextPlugin()], alias: { '@': resolve(appRoot, 'src'), }, diff --git a/apps/vis/server/package.json b/apps/vis/server/package.json index 1d792bb9ba..db91bc63c0 100644 --- a/apps/vis/server/package.json +++ b/apps/vis/server/package.json @@ -5,10 +5,7 @@ "license": "MIT", "type": "module", "imports": { - "#/*": [ - "./src/*.ts", - "./src/*/index.ts" - ] + "#/*": "./src/*.ts" }, "exports": { ".": { diff --git a/build/hash-imports-loader.mjs b/build/hash-imports-loader.mjs deleted file mode 100644 index 2e50caefdb..0000000000 --- a/build/hash-imports-loader.mjs +++ /dev/null @@ -1,79 +0,0 @@ -import { existsSync, readFileSync } from 'node:fs'; -import { dirname, join } from 'node:path'; -import { fileURLToPath, pathToFileURL } from 'node:url'; - -/** - * Node ESM `resolve` hook: correctly resolve `#/` subpath imports against the - * importing package's own `package.json` `imports` field — including array - * fallbacks such as `"#/*": ["./src/*.ts", "./src//index.ts"]`. - * - * `tsx` (its resolver) only honors the first array element and therefore breaks - * on directory-style `#/` imports (for example `#/_base/errors` → - * `_base/errors/index.ts`). This loader short-circuits `#/` resolution before - * tsx sees it, mirroring the Vite `hashImportsPlugin` used by the v2 tests. - */ - -const pkgCache = new Map(); - -function findPackageJson(fromFileUrl) { - let dir = dirname(fileURLToPath(fromFileUrl)); - for (;;) { - const candidate = join(dir, 'package.json'); - if (existsSync(candidate)) return candidate; - const parent = dirname(dir); - if (parent === dir) return undefined; - dir = parent; - } -} - -function readPackage(pkgPath) { - let pkg = pkgCache.get(pkgPath); - if (pkg === undefined) { - pkg = JSON.parse(readFileSync(pkgPath, 'utf-8')); - pkgCache.set(pkgPath, pkg); - } - return pkg; -} - -function resolveTarget(pkgDir, target, rest) { - const resolved = rest === undefined ? target : target.replace('*', rest); - const full = join(pkgDir, resolved); - return existsSync(full) ? pathToFileURL(full).href : undefined; -} - -function resolveHashImport(specifier, parentURL) { - if (parentURL === undefined) return undefined; - const pkgPath = findPackageJson(parentURL); - if (pkgPath === undefined) return undefined; - const imports = readPackage(pkgPath).imports; - if (imports === undefined) return undefined; - const pkgDir = dirname(pkgPath); - - for (const [key, raw] of Object.entries(imports)) { - if (!key.startsWith('#')) continue; - const targets = Array.isArray(raw) ? raw : [raw]; - if (key.endsWith('*')) { - const prefix = key.slice(0, -1); - if (!specifier.startsWith(prefix)) continue; - const rest = specifier.slice(prefix.length); - for (const target of targets) { - const url = resolveTarget(pkgDir, target, rest); - if (url !== undefined) return url; - } - } else if (specifier === key) { - for (const target of targets) { - const url = resolveTarget(pkgDir, target, undefined); - if (url !== undefined) return url; - } - } - } - return undefined; -} - -export async function resolve(specifier, context, nextResolve) { - if (specifier.startsWith('#/')) { - const url = resolveHashImport(specifier, context.parentURL); - if (url !== undefined) return { url, shortCircuit: true }; - } - return nextResolve(specifier, context); -} diff --git a/build/hash-imports-plugin.mjs b/build/hash-imports-plugin.mjs deleted file mode 100644 index b1aeae1b61..0000000000 --- a/build/hash-imports-plugin.mjs +++ /dev/null @@ -1,84 +0,0 @@ -import { existsSync, readFileSync } from 'node:fs'; -import { dirname, join } from 'node:path'; - -/** - * Rolldown/tsdown plugin: resolve `#/` subpath imports the way Node's - * package.json `imports` field does — scoped to the IMPORTER's owning package, - * honoring array fallbacks such as `"#/*": ["./src/*.ts", "./src//index.ts"]`. - * - * Why this is needed: when the CLI bundles `@moonshot-ai/kap-server`, rolldown - * inlines `@moonshot-ai/agent-core-v2` source, whose internal `#/foo` imports - * must resolve against each package's own `src/`. Rolldown (like tsx) only - * honors the first array element of an `imports` target and therefore breaks - * on directory-style `#/` imports (e.g. `#/_base/errors` → `_base/errors/index.ts`), - * leaving them as bare `require("#/...")` in the bundle. This plugin resolves - * them first. Mirrors `build/hash-imports-loader.mjs` (the Node/tsx loader) and - * the vite `hashImportsPlugin` used by the v2 test configs. - */ - -const pkgCache = new Map(); - -function findPackageJson(importer) { - if (!importer) return undefined; - let dir = dirname(importer.split('?')[0] ?? importer); - for (;;) { - const candidate = join(dir, 'package.json'); - if (existsSync(candidate)) return candidate; - const parent = dirname(dir); - if (parent === dir) return undefined; - dir = parent; - } -} - -function readPackage(pkgPath) { - let pkg = pkgCache.get(pkgPath); - if (pkg === undefined) { - pkg = JSON.parse(readFileSync(pkgPath, 'utf-8')); - pkgCache.set(pkgPath, pkg); - } - return pkg; -} - -function resolveTarget(pkgDir, target, rest) { - const resolved = rest === undefined ? target : target.replace('*', rest); - const full = join(pkgDir, resolved); - return existsSync(full) ? full : undefined; -} - -function resolveHashImport(specifier, importer) { - const pkgPath = findPackageJson(importer); - if (pkgPath === undefined) return undefined; - const imports = readPackage(pkgPath).imports; - if (imports === undefined) return undefined; - const pkgDir = dirname(pkgPath); - - for (const [key, raw] of Object.entries(imports)) { - if (!key.startsWith('#')) continue; - const targets = Array.isArray(raw) ? raw : [raw]; - if (key.endsWith('*')) { - const prefix = key.slice(0, -1); - if (!specifier.startsWith(prefix)) continue; - const rest = specifier.slice(prefix.length); - for (const target of targets) { - const full = resolveTarget(pkgDir, target, rest); - if (full !== undefined) return full; - } - } else if (specifier === key) { - for (const target of targets) { - const full = resolveTarget(pkgDir, target, undefined); - if (full !== undefined) return full; - } - } - } - return undefined; -} - -export function hashImportsPlugin() { - return { - name: 'resolve-hash-imports', - resolveId(id, importer) { - if (!id.startsWith('#/')) return null; - return resolveHashImport(id, importer) ?? null; - }, - }; -} diff --git a/build/register-hash-imports-loader.mjs b/build/register-hash-imports-loader.mjs deleted file mode 100644 index f06a777191..0000000000 --- a/build/register-hash-imports-loader.mjs +++ /dev/null @@ -1,8 +0,0 @@ -import { register } from 'node:module'; - -/** - * Registers the `#/` subpath-import resolver. Pass to Node via `--import` - * (alongside tsx) so source-executed v2 code resolves directory-style `#/` - * imports (array fallback) that tsx's resolver mishandles. - */ -register('./hash-imports-loader.mjs', import.meta.url); diff --git a/packages/acp-adapter/package.json b/packages/acp-adapter/package.json index d0baf317c4..792abf3cbb 100644 --- a/packages/acp-adapter/package.json +++ b/packages/acp-adapter/package.json @@ -7,10 +7,7 @@ "author": "Moonshot AI", "type": "module", "imports": { - "#/*": [ - "./src/*.ts", - "./src/*/index.ts" - ] + "#/*": "./src/*.ts" }, "exports": { ".": { diff --git a/packages/agent-core-v2/package.json b/packages/agent-core-v2/package.json index 4822fe6a1a..a546a948cd 100644 --- a/packages/agent-core-v2/package.json +++ b/packages/agent-core-v2/package.json @@ -27,10 +27,7 @@ ], "type": "module", "imports": { - "#/*": [ - "./src/*.ts", - "./src/*/index.ts" - ] + "#/*": "./src/*.ts" }, "exports": { ".": { diff --git a/packages/agent-core-v2/vitest.config.ts b/packages/agent-core-v2/vitest.config.ts index 2480d0929b..9e1a498784 100644 --- a/packages/agent-core-v2/vitest.config.ts +++ b/packages/agent-core-v2/vitest.config.ts @@ -1,48 +1,6 @@ -import { existsSync } from 'node:fs'; -import { dirname, join } from 'node:path'; -import { defineConfig, type Plugin } from 'vitest/config'; - -function findPackageRoot(importer: string | undefined): string | undefined { - if (!importer) return undefined; - let dir = dirname(importer.split('?')[0] ?? importer); - for (;;) { - if (existsSync(join(dir, 'package.json'))) return dir; - const parent = dirname(dir); - if (parent === dir) return undefined; - dir = parent; - } -} - -/** - * Resolve `#/` subpath imports the way Node's package.json `imports` field does, - * scoped to the importer's owning package. agent-core-v2 sources and tests - * import each other through the `#/*` alias (e.g. `#/agent/loop`); resolving - * against the importer's package root keeps those aliases pointing at - * agent-core-v2 even when a test inlines a dependency's src. - * - * Tries `src/.ts` then `src//index.ts`, mirroring the - * `"#/*"` → `["./src/*.ts", "./src//index.ts"]` fallback used by the package. - */ -function hashImportsPlugin(): Plugin { - return { - name: 'resolve-hash-imports', - enforce: 'pre', - resolveId(id, importer) { - if (!id.startsWith('#/')) return null; - const pkgRoot = findPackageRoot(importer); - if (!pkgRoot) return null; - const sub = id.slice(2); - for (const candidate of [`src/${sub}.ts`, `src/${sub}/index.ts`]) { - const full = join(pkgRoot, candidate); - if (existsSync(full)) return full; - } - return null; - }, - }; -} +import { defineConfig } from 'vitest/config'; export default defineConfig({ - plugins: [hashImportsPlugin()], test: { name: 'agent-core-v2', include: ['test/**/*.{test,e2e,integration}.ts'], diff --git a/packages/agent-core/package.json b/packages/agent-core/package.json index d38a0a473e..dd8028abf6 100644 --- a/packages/agent-core/package.json +++ b/packages/agent-core/package.json @@ -27,10 +27,23 @@ ], "type": "module", "imports": { - "#/*": [ - "./src/*.ts", - "./src/*/index.ts" - ] + "#/agent": "./src/agent/index.ts", + "#/agent/background": "./src/agent/background/index.ts", + "#/agent/compaction": "./src/agent/compaction/index.ts", + "#/agent/config": "./src/agent/config/index.ts", + "#/agent/context": "./src/agent/context/index.ts", + "#/agent/cron": "./src/agent/cron/index.ts", + "#/agent/goal": "./src/agent/goal/index.ts", + "#/agent/permission": "./src/agent/permission/index.ts", + "#/agent/plan": "./src/agent/plan/index.ts", + "#/agent/swarm": "./src/agent/swarm/index.ts", + "#/agent/tool": "./src/agent/tool/index.ts", + "#/config": "./src/config/index.ts", + "#/flags": "./src/flags/index.ts", + "#/loop": "./src/loop/index.ts", + "#/rpc": "./src/rpc/index.ts", + "#/session": "./src/session/index.ts", + "#/*": "./src/*.ts" }, "exports": { ".": { diff --git a/packages/kaos/package.json b/packages/kaos/package.json index b8f65140dd..6175b2a4a9 100644 --- a/packages/kaos/package.json +++ b/packages/kaos/package.json @@ -27,10 +27,7 @@ ], "type": "module", "imports": { - "#/*": [ - "./src/*.ts", - "./src/*/index.ts" - ] + "#/*": "./src/*.ts" }, "exports": { ".": { diff --git a/packages/kap-server/vitest.config.ts b/packages/kap-server/vitest.config.ts index e9fa7ada1f..db9e8c73a4 100644 --- a/packages/kap-server/vitest.config.ts +++ b/packages/kap-server/vitest.config.ts @@ -1,51 +1,11 @@ -import { existsSync } from 'node:fs'; -import { dirname, join } from 'node:path'; - -import { defineConfig, type Plugin } from 'vitest/config'; +import { defineConfig } from 'vitest/config'; import { rawTextPlugin } from '../../build/raw-text-plugin.mjs'; -function findPackageRoot(importer: string | undefined): string | undefined { - if (!importer) return undefined; - let dir = dirname(importer.split('?')[0] ?? importer); - for (;;) { - if (existsSync(join(dir, 'package.json'))) return dir; - const parent = dirname(dir); - if (parent === dir) return undefined; - dir = parent; - } -} - -/** - * Resolve `#/` subpath imports the way Node's package.json `imports` field does, - * scoped to the importer's owning package. server-v2 inlines - * `@moonshot-ai/agent-core-v2` source (and transitively kosong/kaos), whose - * internal `#/foo` imports must resolve against each package's own `src/`. - * - * Mirrors `packages/agent-core-v2/vitest.config.ts`. - */ -function hashImportsPlugin(): Plugin { - return { - name: 'resolve-hash-imports', - enforce: 'pre', - resolveId(id, importer) { - if (!id.startsWith('#/')) return null; - const pkgRoot = findPackageRoot(importer); - if (!pkgRoot) return null; - const sub = id.slice(2); - for (const candidate of [`src/${sub}.ts`, `src/${sub}/index.ts`]) { - const full = join(pkgRoot, candidate); - if (existsSync(full)) return full; - } - return null; - }, - }; -} - +// `rawTextPlugin` is required because server-v2 pulls in agent-core-v2's full +// barrel, which imports `*.md?raw` prompt templates. export default defineConfig({ - // `rawTextPlugin` is required because server-v2 pulls in agent-core-v2's full - // barrel, which imports `*.md?raw` prompt templates. - plugins: [rawTextPlugin(), hashImportsPlugin()], + plugins: [rawTextPlugin()], test: { name: 'kap-server', include: ['test/**/*.{test,e2e}.ts'], diff --git a/packages/klient/package.json b/packages/klient/package.json index 66c6a130b9..249536c8de 100644 --- a/packages/klient/package.json +++ b/packages/klient/package.json @@ -6,10 +6,7 @@ "license": "MIT", "type": "module", "imports": { - "#/*": [ - "./src/*.ts", - "./src/*/index.ts" - ] + "#/*": "./src/*.ts" }, "exports": { ".": { diff --git a/packages/klient/vitest.config.ts b/packages/klient/vitest.config.ts index 959cf09eb4..02890dfb7c 100644 --- a/packages/klient/vitest.config.ts +++ b/packages/klient/vitest.config.ts @@ -1,46 +1,6 @@ -import { existsSync } from 'node:fs'; -import { dirname, join } from 'node:path'; - -import { defineConfig, type Plugin } from 'vitest/config'; - -function findPackageRoot(importer: string | undefined): string | undefined { - if (!importer) return undefined; - let dir = dirname(importer.split('?')[0] ?? importer); - for (;;) { - if (existsSync(join(dir, 'package.json'))) return dir; - const parent = dirname(dir); - if (parent === dir) return undefined; - dir = parent; - } -} - -/** - * Resolve `#/` subpath imports the way Node's package.json `imports` field does, - * scoped to the importer's owning package. The client stays type-only against - * `agent-core-v2`, but tests import service tokens as values (e.g. - * `ISessionIndex`), whose internal `#/foo` imports must resolve against - * `agent-core-v2`'s own `src/`. Mirrors `packages/kap-server/vitest.config.ts`. - */ -function hashImportsPlugin(): Plugin { - return { - name: 'resolve-hash-imports', - enforce: 'pre', - resolveId(id, importer) { - if (!id.startsWith('#/')) return null; - const pkgRoot = findPackageRoot(importer); - if (!pkgRoot) return null; - const sub = id.slice(2); - for (const candidate of [`src/${sub}.ts`, `src/${sub}/index.ts`]) { - const full = join(pkgRoot, candidate); - if (existsSync(full)) return full; - } - return null; - }, - }; -} +import { defineConfig } from 'vitest/config'; export default defineConfig({ - plugins: [hashImportsPlugin()], test: { name: 'klient', include: ['test/**/*.test.ts'], diff --git a/packages/kosong/package.json b/packages/kosong/package.json index c2b630c5e6..fc86f1493d 100644 --- a/packages/kosong/package.json +++ b/packages/kosong/package.json @@ -25,10 +25,7 @@ ], "type": "module", "imports": { - "#/*": [ - "./src/*.ts", - "./src/*/index.ts" - ] + "#/*": "./src/*.ts" }, "exports": { ".": { diff --git a/packages/migration-legacy/package.json b/packages/migration-legacy/package.json index 299122275c..d68b1d9936 100644 --- a/packages/migration-legacy/package.json +++ b/packages/migration-legacy/package.json @@ -9,10 +9,7 @@ "dist" ], "imports": { - "#/*": [ - "./src/*.ts", - "./src/*/index.ts" - ] + "#/*": "./src/*.ts" }, "exports": { ".": { diff --git a/packages/minidb/package.json b/packages/minidb/package.json index b98bc8913f..0113961e54 100644 --- a/packages/minidb/package.json +++ b/packages/minidb/package.json @@ -27,10 +27,7 @@ ], "type": "module", "imports": { - "#/*": [ - "./src/*.ts", - "./src/*/index.ts" - ] + "#/*": "./src/*.ts" }, "exports": { ".": { diff --git a/packages/node-sdk/package.json b/packages/node-sdk/package.json index 31b453b19b..7d075720ae 100644 --- a/packages/node-sdk/package.json +++ b/packages/node-sdk/package.json @@ -27,10 +27,7 @@ ], "type": "module", "imports": { - "#/*": [ - "./src/*.ts", - "./src/*/index.ts" - ] + "#/*": "./src/*.ts" }, "exports": { ".": { diff --git a/packages/oauth/package.json b/packages/oauth/package.json index 9ed7b04879..79dbe04cd2 100644 --- a/packages/oauth/package.json +++ b/packages/oauth/package.json @@ -26,10 +26,7 @@ ], "type": "module", "imports": { - "#/*": [ - "./src/*.ts", - "./src/*/index.ts" - ] + "#/*": "./src/*.ts" }, "exports": { ".": { diff --git a/packages/pi-tui/package.json b/packages/pi-tui/package.json index 85981d50db..d4dd8812d7 100644 --- a/packages/pi-tui/package.json +++ b/packages/pi-tui/package.json @@ -30,10 +30,7 @@ ], "type": "module", "imports": { - "#/*": [ - "./src/*.ts", - "./src/*/index.ts" - ] + "#/*": "./src/*.ts" }, "exports": { ".": { diff --git a/packages/protocol/package.json b/packages/protocol/package.json index 3b0850e2bc..4ed238fd32 100644 --- a/packages/protocol/package.json +++ b/packages/protocol/package.json @@ -15,10 +15,7 @@ }, "type": "module", "imports": { - "#/*": [ - "./src/*.ts", - "./src/*/index.ts" - ] + "#/*": "./src/*.ts" }, "exports": { ".": { diff --git a/packages/server-e2e/package.json b/packages/server-e2e/package.json index ad3753b8d4..a551cfa179 100644 --- a/packages/server-e2e/package.json +++ b/packages/server-e2e/package.json @@ -15,10 +15,7 @@ }, "type": "module", "imports": { - "#/*": [ - "./src/*.ts", - "./src/*/index.ts" - ] + "#/*": "./src/*.ts" }, "exports": { ".": { diff --git a/packages/server-e2e/vitest.config.ts b/packages/server-e2e/vitest.config.ts index 6a00a81d80..8f393810df 100644 --- a/packages/server-e2e/vitest.config.ts +++ b/packages/server-e2e/vitest.config.ts @@ -1,53 +1,14 @@ -import { existsSync } from 'node:fs'; -import { dirname, join } from 'node:path'; import { fileURLToPath } from 'node:url'; -import { defineConfig, type Plugin } from 'vitest/config'; +import { defineConfig } from 'vitest/config'; import { rawTextPlugin } from '../../build/raw-text-plugin.mjs'; -function findPackageRoot(importer: string | undefined): string | undefined { - if (!importer) return undefined; - let dir = dirname(importer.split('?')[0] ?? importer); - for (;;) { - if (existsSync(join(dir, 'package.json'))) return dir; - const parent = dirname(dir); - if (parent === dir) return undefined; - dir = parent; - } -} - -/** - * Resolve `#/` subpath imports scoped to the importer's owning package. Required - * because the v2 SDK tests import `@moonshot-ai/kap-server`, which inlines - * `@moonshot-ai/agent-core-v2` source whose internal `#/foo` imports must - * resolve against that package's own `src/`. Mirrors - * `packages/kap-server/vitest.config.ts`. - */ -function hashImportsPlugin(): Plugin { - return { - name: 'resolve-hash-imports', - enforce: 'pre', - resolveId(id, importer) { - if (!id.startsWith('#/')) return null; - const pkgRoot = findPackageRoot(importer); - if (!pkgRoot) return null; - const sub = id.slice(2); - for (const candidate of [`src/${sub}.ts`, `src/${sub}/index.ts`]) { - const full = join(pkgRoot, candidate); - if (existsSync(full)) return full; - } - return null; - }, - }; -} - // `rawTextPlugin` is required because importing `@moonshot-ai/kap-server` (for // the v2 SDK tests) pulls in agent-core-v2's barrel, which imports `*.md?raw` -// prompt templates. Both plugins are no-ops for the legacy v1 tests, which do -// not import server-v2. +// prompt templates. export default defineConfig({ - plugins: [rawTextPlugin(), hashImportsPlugin()], + plugins: [rawTextPlugin()], resolve: { alias: { '@moonshot-ai/protocol': fileURLToPath( diff --git a/packages/telemetry/package.json b/packages/telemetry/package.json index 26fbc1a98b..36d0f60a39 100644 --- a/packages/telemetry/package.json +++ b/packages/telemetry/package.json @@ -26,10 +26,7 @@ ], "type": "module", "imports": { - "#/*": [ - "./src/*.ts", - "./src/*/index.ts" - ] + "#/*": "./src/*.ts" }, "exports": { ".": { From 733ec5dfa00dd2e563de6ccde6b5a7ed9f433cc8 Mon Sep 17 00:00:00 2001 From: _Kerman Date: Mon, 13 Jul 2026 16:18:16 +0800 Subject: [PATCH 2/2] fix(kimi-code): keep .d.ts fallback for generated vis-web-asset import src/generated/vis-web-asset.ts is gitignored and only produced by the prebuild; on a fresh checkout (where CI typechecks before any build) only the committed vis-web-asset.d.ts exists. Map the exact specifier to ["./src/generated/vis-web-asset.ts", "./src/generated/vis-web-asset.d.ts"] so runtime/bundlers take the first entry (.ts) while TypeScript falls back to the declaration stub when the generated file is absent. --- apps/kimi-code/package.json | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/apps/kimi-code/package.json b/apps/kimi-code/package.json index 708e9c581d..b0775cf216 100644 --- a/apps/kimi-code/package.json +++ b/apps/kimi-code/package.json @@ -39,6 +39,10 @@ "#/tui/commands": "./src/tui/commands/index.ts", "#/cli/sub/server": "./src/cli/sub/server/index.ts", "#/cli/sub/server/*": "./src/cli/sub/server/*.ts", + "#/generated/vis-web-asset": [ + "./src/generated/vis-web-asset.ts", + "./src/generated/vis-web-asset.d.ts" + ], "#/*": "./src/*.ts" }, "publishConfig": {