fix(runtime): close removal watchers without reauthorizing a revoked worktree path (#43371)

Worktree removal on POSIX re-ran resolveAuthorizedPath before closing a
leftover watcher child. Once Git had dropped an external worktree's
registration, that check threw PATH_OUTSIDE_ALLOWED_DIRECTORIES and the
whole removal aborted. Fall back to the resolved spelled path for the
in-memory watcher lookup on that error; other errors still propagate.

Fixes #43368

Co-authored-by: Neil <4138956+nwparker@users.noreply.github.com>
This commit is contained in:
katon
2026-11-16 19:47:06 -08:00
committed by GitHub
co-authored by Neil
parent 225baa2477
commit e9060c2039
2 changed files with 29 additions and 1 deletions
+12 -1
View File
@@ -10,6 +10,8 @@ import {
} from '../file-target'
import type { RuntimeFileCommandHost } from '../file-command-host'
import { stat } from 'node:fs/promises'
import { resolve } from 'node:path'
import { isPathOutsideAllowedDirectoriesError } from '../../../../shared/file-access/local-file-access'
import type { DirEntry } from '../../../../shared/file-listing/directory-entry'
import type { FsChangeEvent } from '../../../../shared/file-watch/filesystem-change-event'
import { sortDirEntries } from '../../../../shared/file-listing/file-name-sort'
@@ -100,7 +102,16 @@ export class RuntimeFileWatches {
// Windows watches in-process, so only the leases above exist; Git may already have revoked the path.
if (!connectionId && process.platform !== 'win32') {
// Failed setup may still own a child without having published a lease-release callback.
const resolvedRootPath = await resolveAuthorizedPath(rootPath, this.host.requireStore())
// Closing only looks up existing watches, so a revoked path still closes.
const resolvedRootPath = await resolveAuthorizedPath(
rootPath,
this.host.requireStore()
).catch((error: unknown) => {
if (!isPathOutsideAllowedDirectoriesError(error)) {
throw error
}
return resolve(rootPath)
})
await closeFileExplorerWatcherInWatcherProcess(resolvedRootPath)
}
}
@@ -5,6 +5,7 @@ import type * as FsPromises from 'node:fs/promises'
import type * as FilesystemAuth from '../../../ipc/filesystem/access/filesystem-auth'
import { WatcherProcessFailure } from '../../../ipc/filesystem/watcher-process/parcel-watcher-process-failure'
import { PATH_ACCESS_DENIED_MESSAGE } from '../../../ipc/filesystem/access/filesystem-auth'
const {
resolveAuthorizedPathMock,
@@ -209,6 +210,22 @@ describe('RuntimeFileWatches', () => {
expect(watcher.close).toHaveBeenCalledTimes(1)
})
posixWatcherProcessIt(
'closes removal watchers after Git already dropped the path from allowed directories',
async () => {
resolveAuthorizedPathMock.mockRejectedValue(new Error(PATH_ACCESS_DENIED_MESSAGE))
const { commands } = createRuntimeFileWatches('/projects/repo-feature')
await expect(
commands.closeFileExplorerWatchersForPath('/projects/repo-feature')
).resolves.toBeUndefined()
expect(closeWatcherInWatcherProcessMock).toHaveBeenCalledExactlyOnceWith(
'/projects/repo-feature'
)
}
)
posixWatcherProcessIt('tracks watcher unsubscribe work so shutdown can await it', async () => {
resolveAuthorizedPathMock.mockResolvedValue('/repo')
statMock.mockResolvedValue({ isDirectory: () => true })