fix(paths): ~ 是主目录路径而不是会话相对路径,展开后再解析(Fixes #713,随 Phase 2 路径簇顺带修)

This commit is contained in:
HuanLinOTO committed 2026-10-01 17:20:06 +08:00
1 parent 3345b72699
commit 37fb9204b0
4 files changed
+29 -2

No files matched your search

+4 -1
View File
@@ -21,7 +21,10 @@
* comment — so a produced UNC path is never joined onto the cwd.
*/
export function isAbsolutePath(path: string): boolean {
return path.startsWith('/') || /^[A-Za-z]:[\\/]/.test(path) || /^[\\/]{2}[^\\/]/.test(path)
// A `~`-relative path (`~`, `~/...`, `~\...`) names the user's home, not a
// session-relative path (#713): never join it onto the cwd client-side.
return path === '~' || path.startsWith('~/') || path.startsWith('~\\')
|| path.startsWith('/') || /^[A-Za-z]:[\\/]/.test(path) || /^[\\/]{2}[^\\/]/.test(path)
}
/**
+7 -1
View File
@@ -17,6 +17,7 @@
* way they did with the fence: the change is the SEMANTICS — "resolve" rather
* than "guard".
*/
import { homedir } from 'node:os'
import { isAbsolute, join } from 'node:path'
import { requireAbsolute } from './fs-tree.ts'
import { resolveSessionPath } from './session-path.ts'
@@ -62,10 +63,15 @@ export async function ensureWorkspaceWritePath(cwd: string, target: string, _fen
* (lexical `..` collapse). Throws fs-error for a non-absolute result.
*/
export function resolveTarget(cwd: string, target: string): string {
// #713: `~` names the user's home directory, not a session-relative path —
// expand it before the join so it is not pasted after the cwd (ENOENT).
const homeExpanded = target === '~' || target.startsWith('~/') || target.startsWith('~\\')
? join(homedir(), target.slice(1))
: target
// #646: the ecosystem passes session-relative targets (e.g. `openFile(scope,
// 'pastes/x.txt')` keeps `pastes/x.txt` in the tab). Join them onto the
// session cwd before the absolute check so every entry point (fs.read /
// write / rename / remove / upload, media, html) shares ONE contract.
const joined = isAbsolute(target) ? target : join(cwd, target)
const joined = isAbsolute(homeExpanded) ? homeExpanded : join(cwd, homeExpanded)
return requireAbsolute(resolveSessionPath(cwd, joined))
}
+10
View File
@@ -37,4 +37,14 @@ describe('fs.write resolves session-relative paths (#646)', () => {
// The fence argument is ignored by design now; the path resolves lexically.
await expect(ensureWorkspaceWritePath(dir, target, true)).resolves.toBe(target)
})
it('expands a ~ home path instead of joining it under the cwd (#713)', async () => {
const dir = join(tmpdir(), 'dshm-713')
dirs.push(dir)
mkdirSync(dir, { recursive: true })
const { homedir } = await import('node:os')
const result = await ensureWorkspaceWritePath(dir, '~/notes/x.txt', false)
expect(result.startsWith(homedir())).toBe(true)
expect(result).toContain(join('notes', 'x.txt'))
})
})
+8
View File
@@ -57,6 +57,14 @@ describe('path helpers', () => {
expect(isAbsolutePath('rel/x.ts')).toBe(false)
})
it('treats ~ as home-absolute, never a session-relative path (#713)', () => {
expect(isAbsolutePath('~')).toBe(true)
expect(isAbsolutePath('~/notes/x.md')).toBe(true)
expect(isAbsolutePath('~\\notes\\x.md')).toBe(true)
// `~other-user` is NOT the home shorthand; it stays relative-shaped.
expect(isAbsolutePath('~other/x.ts')).toBe(false)
})
it('htmlUrl always marks UNC paths (platform-neutral marker)', () => {
// The marker is platform-neutral now: the host resolves the decoded
// '//server/share/...' form per-platform, so no cwd/OS signal is needed.