fix(editor): reject lock files with no workspace match for cwd (#24323)

This commit is contained in:
Jermiah Joseph
2026-04-26 14:21:29 -04:00
committed by GitHub
parent 00d1a7e090
commit dcee1c3642
@@ -278,12 +278,16 @@ function resolveEditorLockFile() {
} }
const cwd = process.cwd() 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 const locks = entries
.filter((entry) => entry.endsWith(".lock")) .filter((entry) => entry.endsWith(".lock"))
.map((entry) => readEditorLockFile(path.join(directory, entry))) .map((entry) => readEditorLockFile(path.join(directory, entry)))
.filter((entry): entry is EditorLockFile => Boolean(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] 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) { function editorSelectionKey(selection: EditorSelection | undefined) {
if (!selection) return "" if (!selection) return ""
return [ return [
@@ -327,9 +326,10 @@ function editorSelectionKey(selection: EditorSelection | undefined) {
].join("\0") ].join("\0")
} }
function pathContains(parent: string, child: string) { function pathContainsLength(parent: string, child: string) {
const relative = path.relative(path.resolve(parent), path.resolve(child)) const resolved = path.resolve(parent)
return relative === "" || (!relative.startsWith("..") && !path.isAbsolute(relative)) const relative = path.relative(resolved, path.resolve(child))
return relative === "" || (!relative.startsWith("..") && !path.isAbsolute(relative)) ? resolved.length : 0
} }
function openEditorSocket(connection: EditorConnection) { function openEditorSocket(connection: EditorConnection) {