From e9060c2039183df4bccf092fc117b8de133c3c2d Mon Sep 17 00:00:00 2001 From: katon Date: Tue, 17 Nov 2026 11:47:06 +0800 Subject: [PATCH] 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> --- src/main/runtime/files/watch/file-watches.ts | 13 ++++++++++++- .../watch/orca-runtime-files-watch.test.ts | 17 +++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/src/main/runtime/files/watch/file-watches.ts b/src/main/runtime/files/watch/file-watches.ts index 450e1acef00..c923477ca98 100644 --- a/src/main/runtime/files/watch/file-watches.ts +++ b/src/main/runtime/files/watch/file-watches.ts @@ -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) } } diff --git a/src/main/runtime/files/watch/orca-runtime-files-watch.test.ts b/src/main/runtime/files/watch/orca-runtime-files-watch.test.ts index bc7034c11ed..3f8bd26a752 100644 --- a/src/main/runtime/files/watch/orca-runtime-files-watch.test.ts +++ b/src/main/runtime/files/watch/orca-runtime-files-watch.test.ts @@ -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 })