angular/vscode-ng-language-service/server/src/server.ts
Andrew Scott 6fb39d9b62 feat(language-server): Support client-side file watching via onDidChangeWatchedFiles
This implements `onDidChangedWatchedFiles` in the language server, which
allows the client to communicate changes to files rather than having the
server create system file/directory watchers.

This option is enabled in the extension via the
`angular.server.useClientSideFileWatcher` setting.
When enabled, the extension registers a FileSystemWatcher for .ts, .html, and package.json files and forwards events to the server. The server completely disables its internal native file watchers (via a new 'ServerHost' implementation that stubs watchFile/watchDirectory).

This is significantly more performant and reliable than native watching for several reasons:
- Deduplication: VS Code already watches the workspace. Piggybacking on these events prevents the server from duplicating thousands of file watchers.
- OS Limits: Since the server opens zero watcher handles, it is impossible to hit OS limits (ENOSPC), no matter how large the repo is.
- Optimization: VS Code's watcher uses highly optimized native implementations (like Parcel Watcher in Rust/C++) which handle recursive directory watching far better than Node.js's 'fs.watch'.
- Debouncing: The client aggregates extremely frequent file events (e.g., during 'git checkout'), reducing the flood of processing requests to the server.

This option was tested in one very large internal project and observed
~10-50x improvement of initialization times.

fixes #66543
2026-01-23 19:52:37 +00:00

74 lines
2.7 KiB
TypeScript

/*!
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.dev/license
*/
import {version as tsServerVersion} from 'typescript/lib/tsserverlibrary';
import {generateHelpMessage, parseCommandLine} from './cmdline_utils';
// Parse command line arguments
const options = parseCommandLine(process.argv);
if (options.help) {
console.error(generateHelpMessage(process.argv));
process.exit(0);
}
import {createLogger} from './logger';
import {ServerHost} from './server_host';
import {Session} from './session';
import {resolveNgLangSvc, resolveTsServer} from './version_provider';
function main() {
// Create a logger that logs to file. OK to emit verbose entries.
const logger = createLogger({
logFile: options.logFile,
logVerbosity: options.logVerbosity,
});
const ts = resolveTsServer(options.tsProbeLocations, options.tsdk);
const ng = resolveNgLangSvc(options.ngProbeLocations);
const isG3 = ts.resolvedPath.includes('/google3/');
// ServerHost provides native OS functionality
const host = new ServerHost(isG3, options.useClientSideFileWatcher ?? false);
// Establish a new server session that encapsulates lsp connection.
const session = new Session({
host,
logger,
// TypeScript allows only package names as plugin names.
ngPlugin: '@angular/language-service',
resolvedNgLsPath: ng.resolvedPath,
logToConsole: options.logToConsole,
includeAutomaticOptionalChainCompletions: options.includeAutomaticOptionalChainCompletions,
includeCompletionsWithSnippetText: options.includeCompletionsWithSnippetText,
includeCompletionsForModuleExports: options.includeCompletionsForModuleExports,
forceStrictTemplates: isG3 || options.forceStrictTemplates,
disableBlockSyntax: options.disableBlockSyntax,
disableLetSyntax: options.disableLetSyntax,
angularCoreVersion: options.angularCoreVersion ?? null,
suppressAngularDiagnosticCodes: options.suppressAngularDiagnosticCodes ?? null,
});
// Log initialization info
session.info(`Angular language server process ID: ${process.pid}`);
session.info(`Imported typescript/lib/tsserverlibrary is version ${tsServerVersion}.`);
session.info(`Using ${ng.name} v${ng.version} from ${ng.resolvedPath}`);
if (logger.loggingEnabled()) {
session.info(`Log file: ${logger.getLogFileName()}`);
} else {
session.info(`Logging is turned off. To enable, run command 'Open Angular server log'.`);
}
if (process.env.NG_DEBUG === 'true') {
session.info('Angular Language Service is running under DEBUG mode');
}
session.listen();
}
main();