diff options
| author | Jermiah Joseph <[email protected]> | 2026-04-26 14:21:29 -0400 |
|---|---|---|
| committer | GitHub <[email protected]> | 2026-04-26 14:21:29 -0400 |
| commit | dcee1c36426495716b2bacc7f31ccf54d29a3ac0 (patch) | |
| tree | a9585d1110401394cdf638dbe9d28d71322dd713 | |
| parent | 00d1a7e090e3b7cc7d3a39e874a02865439d8472 (diff) | |
| download | opencode-dcee1c36426495716b2bacc7f31ccf54d29a3ac0.tar.gz opencode-dcee1c36426495716b2bacc7f31ccf54d29a3ac0.zip | |
fix(editor): reject lock files with no workspace match for cwd (#24323)
| -rw-r--r-- | packages/opencode/src/cli/cmd/tui/context/editor.ts | 20 |
1 files changed, 10 insertions, 10 deletions
diff --git a/packages/opencode/src/cli/cmd/tui/context/editor.ts b/packages/opencode/src/cli/cmd/tui/context/editor.ts index 75c5440f5..72b0785d6 100644 --- a/packages/opencode/src/cli/cmd/tui/context/editor.ts +++ b/packages/opencode/src/cli/cmd/tui/context/editor.ts @@ -278,12 +278,16 @@ function resolveEditorLockFile() { } const cwd = process.cwd() + // longest workspace folder that contains cwd; 0 if none match + const bestMatchLength = (lock: EditorLockFile) => + Math.max(0, ...lock.workspaceFolders.map((folder) => pathContainsLength(folder, cwd))) const locks = entries .filter((entry) => entry.endsWith(".lock")) .map((entry) => readEditorLockFile(path.join(directory, entry))) .filter((entry): entry is EditorLockFile => Boolean(entry)) - .sort((left, right) => scoreEditorLock(right, cwd) - scoreEditorLock(left, cwd)) - + .filter((entry) => bestMatchLength(entry) > 0) + // prefer locks with longer matching workspace folders, then more recent ones + .sort((left, right) => bestMatchLength(right) - bestMatchLength(left) || right.mtimeMs - left.mtimeMs) return locks[0] } @@ -310,11 +314,6 @@ function readEditorLockFile(filePath: string): EditorLockFile | undefined { } } -function scoreEditorLock(lock: EditorLockFile, cwd: string) { - const workspaceMatch = lock.workspaceFolders.some((folder) => pathContains(folder, cwd)) ? 1 : 0 - return workspaceMatch * 1_000_000_000_000 + lock.mtimeMs -} - function editorSelectionKey(selection: EditorSelection | undefined) { if (!selection) return "" return [ @@ -327,9 +326,10 @@ function editorSelectionKey(selection: EditorSelection | undefined) { ].join("\0") } -function pathContains(parent: string, child: string) { - const relative = path.relative(path.resolve(parent), path.resolve(child)) - return relative === "" || (!relative.startsWith("..") && !path.isAbsolute(relative)) +function pathContainsLength(parent: string, child: string) { + const resolved = path.resolve(parent) + const relative = path.relative(resolved, path.resolve(child)) + return relative === "" || (!relative.startsWith("..") && !path.isAbsolute(relative)) ? resolved.length : 0 } function openEditorSocket(connection: EditorConnection) { |
