Skip to content

Commit

Permalink
fix fallback watcher
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonlyu123 committed Oct 27, 2024
1 parent 156bd7d commit 85bc1b5
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 14 deletions.
25 changes: 14 additions & 11 deletions packages/language-server/src/lib/FallbackWatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
} from 'vscode-languageserver';
import { pathToUrl } from '../utils';
import { fileURLToPath } from 'url';
import { Stats } from 'fs';

type DidChangeHandler = (para: DidChangeWatchedFilesParams) => void;

Expand All @@ -20,18 +21,20 @@ export class FallbackWatcher {

private undeliveredFileEvents: FileEvent[] = [];

constructor(recursivePatterns: string, workspacePaths: string[]) {
constructor(watchExtensions: string[], workspacePaths: string[]) {
const gitOrNodeModules = /\.git|node_modules/;
this.watcher = watch(
workspacePaths.map((workspacePath) => join(workspacePath, recursivePatterns)),
{
ignored: gitOrNodeModules,
// typescript would scan the project files on init.
// We only need to know what got updated.
ignoreInitial: true,
ignorePermissionErrors: true
}
);
const ignoredExtensions = (fileName: string, stats?: Stats) => {
return (
stats?.isFile() === true && !watchExtensions.some((ext) => fileName.endsWith(ext))
);
};
this.watcher = watch(workspacePaths, {
ignored: [gitOrNodeModules, ignoredExtensions],
// typescript would scan the project files on init.
// We only need to know what got updated.
ignoreInitial: true,
ignorePermissionErrors: true
});

this.watcher
.on('add', (path) => this.onFSEvent(path, FileChangeType.Created))
Expand Down
8 changes: 5 additions & 3 deletions packages/language-server/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,9 @@ export function startServer(options?: LSOptions) {

// Include Svelte files to better deal with scenarios such as switching git branches
// where files that are not opened in the client could change
const nonRecursiveWatchPattern = '*.{ts,js,mts,mjs,cjs,cts,json,svelte}';
const watchExtensions = ['.ts', '.js', '.mts', '.mjs', '.cjs', '.cts', '.json', '.svelte'];
const nonRecursiveWatchPattern =
'*.{' + watchExtensions.map((ext) => ext.slice(1)).join(',') + '}';
const recursiveWatchPattern = '**/' + nonRecursiveWatchPattern;

connection.onInitialize((evt) => {
Expand All @@ -120,7 +122,7 @@ export function startServer(options?: LSOptions) {

if (!evt.capabilities.workspace?.didChangeWatchedFiles) {
const workspacePaths = workspaceUris.map(urlToPath).filter(isNotNullOrUndefined);
watcher = new FallbackWatcher(recursiveWatchPattern, workspacePaths);
watcher = new FallbackWatcher(watchExtensions, workspacePaths);
watcher.onDidChangeWatchedFiles(onDidChangeWatchedFiles);

watchDirectory = (patterns) => {
Expand Down Expand Up @@ -338,7 +340,7 @@ export function startServer(options?: LSOptions) {
connection?.client.register(DidChangeWatchedFilesNotification.type, {
watchers: [
{
// Editors have exlude configs, such as VSCode with `files.watcherExclude`,
// Editors have exclude configs, such as VSCode with `files.watcherExclude`,
// which means it's safe to watch recursively here
globPattern: recursiveWatchPattern
}
Expand Down

0 comments on commit 85bc1b5

Please sign in to comment.