CI 的 Windows lane 抓到:地址语法在**所有平台**都是 `/` 分隔,而 `resolveSidebarPath` 把相对路径原样拼到 Windows 基上,于是同一个文件得到 `C:\repo\src/a.ts` 这种混合拼写——文件树 / git 面板 / 一切 join 来源拼的是 `C:\repo\src\a.ts`,而编辑器的 tab id 与 dedupeKey 都是路径派生的,两种拼写 就是两个页签身份(CI 上 worktree-address.spec.ts 3/5 挂:正是三条路径相等断言)。 新增 `src/client/address-base.ts` 的 `resolveAddressPath(base, path)`:在地址边界 把结果折叠成 base 的分隔符风格(POSIX 原样保留 —— 那里 `\` 是合法文件名字符, 也是 `resolveSidebarPath` 从不在 POSIX 折叠的原因),`fileParamsOf` 与 `openSidebarFile` 都改走它。`resolveSidebarPath` 本身与其既有断言 (tests/paths.spec.ts 钉住的混合拼写)逐字未动。 用例:worktree-address.spec.ts 新增 1 条平台无关断言(`C:\repo` + `src/a.ts` → `C:\repo\src\a.ts`,POSIX 原样),并让既有 3 条断言在 Windows 上成立。
68 lines
3.4 KiB
TypeScript
68 lines
3.4 KiB
TypeScript
/**
|
|
* The workspace root a session-scoped file ADDRESS is built and read with.
|
|
*
|
|
* DSH's address contract fixes that base to the session's STORED header cwd:
|
|
* the host chat builds every file link with
|
|
* `fileAddressFor(sessionId, ctx.sessions.list.getSnapshot().byId[sessionId]?.cwd, path)`
|
|
* (DSH `ui-chat`), and the host's own file resolver reads the relative path
|
|
* back against `sessions.get(sessionId).header.cwd` (DSH `api-workspace-files`)
|
|
* — the same value, so the host round-trips perfectly.
|
|
*
|
|
* The plugin does not: since #802 its filesystem routes follow the ACTIVE git
|
|
* worktree, so an address's relative path re-resolved by a worktree-rooted
|
|
* reader lands somewhere else entirely — doubled when the worktree sits inside
|
|
* the stored cwd (`<repo>/frontend/.worktrees/wt/a.md` arrives as
|
|
* `frontend/.worktrees/wt/a.md` and becomes
|
|
* `<wt>/frontend/.worktrees/wt/a.md`, ENOENT — #947), or silently on the
|
|
* same-named file of another checkout (#950).
|
|
*
|
|
* The rule that removes the whole family: an address's base is THIS value,
|
|
* never the active worktree root, and an address's path is resolved to an
|
|
* absolute one at the boundary where the address is read, so no
|
|
* worktree-rooted code downstream ever sees a relative path it could re-base.
|
|
* Live relative paths the user or the plugin intends (file tree, typed
|
|
* paths, links inside a document) keep using the active root, which is what
|
|
* `useSessionRoot` is for.
|
|
*
|
|
* @param ctx - client context carrying the session list.
|
|
* @param sessionId - the session the address belongs to; `undefined` answers
|
|
* `undefined` so callers can fall back to their own cwd.
|
|
* @returns the stored header cwd, or `undefined` when it is unknown (a
|
|
* stripped-down host or a bare test context).
|
|
*/
|
|
import type { Context } from '../context-types.ts'
|
|
import { resolveSidebarPath } from './paths.ts'
|
|
|
|
export function addressBaseOf(ctx: Context, sessionId: string | undefined): string | undefined {
|
|
if (sessionId === undefined) return undefined
|
|
try {
|
|
return ctx.sessions?.list?.getSnapshot?.()?.byId?.[sessionId]?.cwd
|
|
} catch {
|
|
// A context without the session list (or with a broken one) keeps the
|
|
// caller's own cwd; the address is no worse than before this helper.
|
|
return undefined
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Resolve an address's path against its base, spelled the way the base is.
|
|
*
|
|
* The address grammar is `/`-separated on every platform, so joining one onto
|
|
* a Windows base with `resolveSidebarPath` alone leaves a MIXED spelling:
|
|
* `C:\repo` + `src/a.ts` → `C:\repo\src/a.ts`, while the file tree, the git
|
|
* panel and every `join`-based producer spell the same file
|
|
* `C:\repo\src\a.ts`. Two spellings of one file is two tab identities (the
|
|
* editor's `dedupeKey`/id is path-derived) and two answers to "is it the same
|
|
* path", so the address boundary folds the separators to the base's style.
|
|
* POSIX keeps the path verbatim: there `\` is a legal file-name character, not
|
|
* a separator (the same reason `resolveSidebarPath` never folds on POSIX).
|
|
*
|
|
* @param base - the workspace root whose separator style decides the answer.
|
|
* @param path - an address path (relative to `base`) or an absolute path.
|
|
* @returns the resolved path in `base`'s separator style.
|
|
*/
|
|
export function resolveAddressPath(base: string | undefined, path: string): string {
|
|
const resolved = resolveSidebarPath(base, path)
|
|
return base !== undefined && base.includes('\\') ? resolved.replace(/[/\\]+/g, '\\') : resolved
|
|
}
|