Skip to content

Commit dcee1c3

Browse files
authored
fix(editor): reject lock files with no workspace match for cwd (#24323)
1 parent 00d1a7e commit dcee1c3

1 file changed

Lines changed: 10 additions & 10 deletions

File tree

  • packages/opencode/src/cli/cmd/tui/context

packages/opencode/src/cli/cmd/tui/context/editor.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -278,12 +278,16 @@ function resolveEditorLockFile() {
278278
}
279279

280280
const cwd = process.cwd()
281+
// longest workspace folder that contains cwd; 0 if none match
282+
const bestMatchLength = (lock: EditorLockFile) =>
283+
Math.max(0, ...lock.workspaceFolders.map((folder) => pathContainsLength(folder, cwd)))
281284
const locks = entries
282285
.filter((entry) => entry.endsWith(".lock"))
283286
.map((entry) => readEditorLockFile(path.join(directory, entry)))
284287
.filter((entry): entry is EditorLockFile => Boolean(entry))
285-
.sort((left, right) => scoreEditorLock(right, cwd) - scoreEditorLock(left, cwd))
286-
288+
.filter((entry) => bestMatchLength(entry) > 0)
289+
// prefer locks with longer matching workspace folders, then more recent ones
290+
.sort((left, right) => bestMatchLength(right) - bestMatchLength(left) || right.mtimeMs - left.mtimeMs)
287291
return locks[0]
288292
}
289293

@@ -310,11 +314,6 @@ function readEditorLockFile(filePath: string): EditorLockFile | undefined {
310314
}
311315
}
312316

313-
function scoreEditorLock(lock: EditorLockFile, cwd: string) {
314-
const workspaceMatch = lock.workspaceFolders.some((folder) => pathContains(folder, cwd)) ? 1 : 0
315-
return workspaceMatch * 1_000_000_000_000 + lock.mtimeMs
316-
}
317-
318317
function editorSelectionKey(selection: EditorSelection | undefined) {
319318
if (!selection) return ""
320319
return [
@@ -327,9 +326,10 @@ function editorSelectionKey(selection: EditorSelection | undefined) {
327326
].join("\0")
328327
}
329328

330-
function pathContains(parent: string, child: string) {
331-
const relative = path.relative(path.resolve(parent), path.resolve(child))
332-
return relative === "" || (!relative.startsWith("..") && !path.isAbsolute(relative))
329+
function pathContainsLength(parent: string, child: string) {
330+
const resolved = path.resolve(parent)
331+
const relative = path.relative(resolved, path.resolve(child))
332+
return relative === "" || (!relative.startsWith("..") && !path.isAbsolute(relative)) ? resolved.length : 0
333333
}
334334

335335
function openEditorSocket(connection: EditorConnection) {

0 commit comments

Comments
 (0)