src/vs/platform/agentHost/node/agentHostFileMonitorService.ts

185 LOC · 178 covered · 7 uncovered · 49 ranges · 993 concepts · 11 introducers · 501 tests

File neighbourhood

The centred file is linked to every concept that introduces one of its ranges, every test that runs code from the file, and the gray connector concepts standing between those tests and the file's own introducer concepts. Undirected links join concepts to every file where they introduce source and concepts to the tests they introduce; arrows show specialization between the displayed concepts and bridge only concepts omitted from this view. Concept colors match the source ranges below; connector concepts have no source color and are shown in gray.

Focused file, its introducer and connector concepts, their introduced files, and tests that run code from the file

In the embedded map, ordinary wheel input scrolls the page; use the visible controls to zoom and drag to pan. Open the full-screen map for canvas navigation: wheel pans, Ctrl/Command plus wheel zooms, and arrow keys pan when this region is focused. On touch screens, open the full-screen map to pan or pinch. If JavaScript or WebGL is unavailable, use the related-file, concept, and source links on this page.

Graph controls are ready.

Interactive rendering requires JavaScript and WebGL. Use the related-file, concept, and source links on this page while the interactive map is unavailable.

1 > /*--------------------------------------------------------------------------------------------- agentHostFileMonitorService.ts ×14
2 > * Copyright (c) Microsoft Corporation. All rights reserved.
3 > * Licensed under the MIT License. See License.txt in the project root for license information.
4 > *--------------------------------------------------------------------------------------------*/
5 >
6 > import { disposableTimeout } from '../../../base/common/async.js';
7 > import { IExpression, ParsedExpression, parse } from '../../../base/common/glob.js';
8 > import { Disposable, DisposableMap, DisposableStore, IDisposable, MutableDisposable, toDisposable } from '../../../base/common/lifecycle.js';
9 > import { extUriBiasedIgnorePathCase } from '../../../base/common/resources.js';
10 > import { URI } from '../../../base/common/uri.js';
11 > import { FileChangesEvent, IFileService } from '../../files/common/files.js';
12 > import { createDecorator } from '../../instantiation/common/instantiation.js';
13 > import { ILogService } from '../../log/common/log.js';
14 >
15 > export const IAgentHostFileMonitorService = createDecorator<IAgentHostFileMonitorService>('agentHostFileMonitorService');
16 >
17 > export const DEFAULT_AGENT_HOST_WATCH_EXCLUDES: readonly string[] = Object.freeze([
18 > '**/.git',
19 > '**/.git/lfs/**',
20 > '**/.git/logs/**',
21 > '**/.git/objects/**',
22 > '**/.git/subtree-cache/**',
23 > '**/.git/**/*.lock',
24 > '**/.git/**/FETCH_HEAD',
25 > '**/.git/**/fsmonitor--daemon/**',
26 > '**/*.watchman-cookie-*',
27 > ]);
28 >
29 > export interface IAgentHostFileMonitorOptions {
30 > readonly excludes?: readonly string[];
31 > readonly debounceMs?: number;
32 > }
33 >
34 > export interface IAgentHostFileMonitorService extends IDisposable {
35 > readonly _serviceBrand: undefined;
36 > acquire(folder: URI, callback: () => void, options?: IAgentHostFileMonitorOptions): IDisposable | undefined;
37 > }
38 >
39 > interface IMonitorEntry extends IDisposable {
40 > readonly folder: URI;
41 > readonly callbacks: Set<() => void>;
42 > readonly debounce: MutableDisposable<IDisposable>;
43 > readonly debounceMs: number;
44 > readonly excludeMatcher: ParsedExpression;
45 > }
46 >
47 > function normalizeExcludes(excludes: readonly string[]): readonly string[] { agentHostFileMonitorService.ts ×8
48 > return [...excludes].sort();
49 > }
51 > function parseExcludes(excludes: readonly string[]): ParsedExpression { agentHostFileMonitorService.ts ×8
52 > const expression: IExpression = Object.create(null);
53 > for (const exclude of excludes) {
54 > expression[exclude] = true;
55 > }
56 > return parse(expression);
57 > }
59 > export class AgentHostFileMonitorService extends Disposable implements IAgentHostFileMonitorService {
60 > declare readonly _serviceBrand: undefined;
61 >
62 > private static readonly _DEFAULT_DEBOUNCE_MS = 750;
63 >
64 > private readonly _entries = this._register(new DisposableMap<string, IMonitorEntry>());
65 >
66 > constructor(
67 > @IFileService private readonly _fileService: IFileService, agentHostFileMonitorService.ts ×2
68 > @ILogService private readonly _logService: ILogService,
69 > ) {
70 > super();
71 > this._register(this._fileService.onDidFilesChange(event => this._onDidFilesChange(event)));
72 > this._register(this._fileService.onDidWatchError(error => {
73 this._logService.warn('[AgentHostFileMonitorService] File watcher error', error);
75 > }
77 > acquire(folder: URI, callback: () => void, options: IAgentHostFileMonitorOptions = {}): IDisposable | undefined {
78 > const canonicalFolder = this._canonicalizeFolder(folder); agentHostFileMonitorService.ts ×8
79 > const excludes = normalizeExcludes(options.excludes ?? DEFAULT_AGENT_HOST_WATCH_EXCLUDES);
80 > const debounceMs = options.debounceMs ?? AgentHostFileMonitorService._DEFAULT_DEBOUNCE_MS;
81 > const key = this._key(canonicalFolder, excludes, debounceMs);
82 >
83 > let entry = this._entries.get(key);
84 > if (!entry) {
85 > try {
86 > entry = this._createEntry(key, canonicalFolder, excludes, debounceMs);
87 > } catch (err) {
88 > this._logService.warn(`[AgentHostFileMonitorService] Failed to watch ${canonicalFolder.toString()}`, err); agentHostFileMonitorService.ts ×2
89 > return undefined;
90 > }
91 > this._entries.set(key, entry); agentHostFileMonitorService.ts ×2
92 > }
93 >
94 > entry.callbacks.add(callback);
95 > return toDisposable(() => {
96 > const current = this._entries.get(key);
97 > if (!current) {
99 > }
100 > current.callbacks.delete(callback); agentHostFileMonitorService.ts ×1
101 > if (current.callbacks.size === 0) {
102 > this._entries.deleteAndDispose(key);
103 > }
107 > private _createEntry(_key: string, folder: URI, excludes: readonly string[], debounceMs: number): IMonitorEntry {
108 > const disposable = new DisposableStore(); agentHostFileMonitorService.ts ×8
109 > try {
110 > const debounce = disposable.add(new MutableDisposable<IDisposable>());
111 > const callbacks = new Set<() => void>();
112 > const excludeMatcher = parseExcludes(excludes);
113 > disposable.add(this._fileService.watch(folder, { recursive: true, excludes: [...excludes] }));
114 > return { folder, callbacks, debounce, debounceMs, excludeMatcher, dispose: () => disposable.dispose() };
115 > } catch (err) {
116 > disposable.dispose(); agentHostFileMonitorService.ts ×2
117 > throw err;
118 > }
121 > private _onDidFilesChange(event: FileChangesEvent): void {
122 > for (const key of this._entries.keys()) { agentHostFileMonitorService.ts ×2
123 > this._onDidFilesChangeEntry(key, event); agentHostFileMonitorService.ts ×12
124 > }
127 > private _onDidFilesChangeEntry(key: string, event: FileChangesEvent): void {
128 > const entry = this._entries.get(key); agentHostFileMonitorService.ts ×12
129 > if (!entry || entry.callbacks.size === 0) {
130 return;
131 }
132 > if (!event.affects(entry.folder) || !this._hasRelevantRawChange(entry, event)) { agentHostFileMonitorService.ts ×12
134 > }
135 > entry.debounce.value = disposableTimeout(() => { agentHostFileMonitorService.ts ×12
136 > entry.debounce.clear(); agentHostFileMonitorService.ts ×2
137 > for (const callback of [...entry.callbacks]) {
138 > try {
139 > callback();
140 > } catch (err) {
141 this._logService.warn('[AgentHostFileMonitorService] Folder change callback failed', err);
142 }
144 > }, entry.debounceMs); agentHostFileMonitorService.ts ×12
145 > }
147 > private _hasRelevantRawChange(entry: IMonitorEntry, event: FileChangesEvent): boolean {
148 > return this._hasRelevantRawResources(entry, event.rawAdded) agentHostFileMonitorService.ts ×12
149 > || this._hasRelevantRawResources(entry, event.rawUpdated)
150 > || this._hasRelevantRawResources(entry, event.rawDeleted); agentHostFileMonitorService.ts ×3
153 > private _hasRelevantRawResources(entry: IMonitorEntry, resources: readonly URI[]): boolean {
154 > for (const resource of resources) { agentHostFileMonitorService.ts ×12
155 > if (!extUriBiasedIgnorePathCase.isEqualOrParent(resource, entry.folder)) {
156 continue;
157 }
158 > if (!this._isExcluded(entry, resource)) { agentHostFileMonitorService.ts ×12
159 > return true;
160 > }
161 > }
162 > return false;
163 > }
165 > private _isExcluded(entry: IMonitorEntry, resource: URI): boolean {
166 > const basename = extUriBiasedIgnorePathCase.basename(resource); agentHostFileMonitorService.ts ×12
167 > const relativePath = extUriBiasedIgnorePathCase.relativePath(entry.folder, resource);
168 > if (relativePath !== undefined && this._matchesExclude(entry, relativePath, basename)) {
170 > }
171 > return this._matchesExclude(entry, resource.path, basename); agentHostFileMonitorService.ts ×12
172 > }
174 > private _matchesExclude(entry: IMonitorEntry, path: string, basename: string): boolean {
175 > return typeof entry.excludeMatcher(path, basename) === 'string'; agentHostFileMonitorService.ts ×12
176 > }
178 > private _canonicalizeFolder(folder: URI): URI {
179 > return extUriBiasedIgnorePathCase.removeTrailingPathSeparator(extUriBiasedIgnorePathCase.normalizePath(folder)); agentHostFileMonitorService.ts ×8
180 > }
182 > private _key(folder: URI, excludes: readonly string[], debounceMs: number): string {
183 > return `${extUriBiasedIgnorePathCase.getComparisonKey(folder)}\u0000${debounceMs}\u0000${excludes.join('\n')}`; agentHostFileMonitorService.ts ×8
184 > }