Skip to content

Commit 8c152bd

Browse files
authored
Only watch directories up to workspace root (#709)
* Don't watch beyond workspace root * Catch errors when adding paths to chokidar watcher * Normalize paths * wip
1 parent 7fdf6c4 commit 8c152bd

File tree

2 files changed

+23
-11
lines changed

2 files changed

+23
-11
lines changed

packages/tailwindcss-language-server/src/server.ts

+21-11
Original file line numberDiff line numberDiff line change
@@ -346,16 +346,22 @@ function changeAffectsFile(change: string, files: string[]): boolean {
346346

347347
// We need to add parent directories to the watcher:
348348
// https://github.com/microsoft/vscode/issues/60813
349-
function getWatchPatternsForFile(file: string): string[] {
349+
function getWatchPatternsForFile(file: string, root: string): string[] {
350350
let tmp: string
351351
let dir = path.dirname(file)
352352
let patterns: string[] = [file, dir]
353+
if (dir === root) {
354+
return patterns
355+
}
353356
while (true) {
354357
dir = path.dirname((tmp = dir))
355358
if (tmp === dir) {
356359
break
357360
} else {
358361
patterns.push(dir)
362+
if (dir === root) {
363+
break
364+
}
359365
}
360366
}
361367
return patterns
@@ -440,8 +446,8 @@ async function createProjectService(
440446
deps = getModuleDependencies(projectConfig.configPath)
441447
} catch {}
442448
watchPatterns([
443-
...getWatchPatternsForFile(projectConfig.configPath),
444-
...deps.flatMap((dep) => getWatchPatternsForFile(dep)),
449+
...getWatchPatternsForFile(projectConfig.configPath, projectConfig.folder),
450+
...deps.flatMap((dep) => getWatchPatternsForFile(dep, projectConfig.folder)),
445451
])
446452
}
447453

@@ -459,7 +465,7 @@ async function createProjectService(
459465
let file = normalizePath(change.file)
460466

461467
let isConfigFile = changeAffectsFile(file, [projectConfig.configPath])
462-
let isDependency = changeAffectsFile(change.file, state.dependencies ?? [])
468+
let isDependency = changeAffectsFile(file, state.dependencies ?? [])
463469
let isPackageFile = minimatch(file, `**/${PACKAGE_LOCK_GLOB}`, { dot: true })
464470

465471
if (!isConfigFile && !isDependency && !isPackageFile) continue
@@ -564,7 +570,7 @@ async function createProjectService(
564570
throw new SilentError('No config file found.')
565571
}
566572

567-
watchPatterns(getWatchPatternsForFile(configPath))
573+
watchPatterns(getWatchPatternsForFile(configPath, projectConfig.folder))
568574

569575
const pnpPath = findUp.sync(
570576
(dir) => {
@@ -576,7 +582,7 @@ async function createProjectService(
576582
if (findUp.sync.exists(pnpFile)) {
577583
return pnpFile
578584
}
579-
if (dir === folder) {
585+
if (dir === path.normalize(folder)) {
580586
return findUp.stop
581587
}
582588
},
@@ -1062,7 +1068,11 @@ async function createProjectService(
10621068
// }
10631069
state.dependencies = getModuleDependencies(state.configPath)
10641070
// chokidarWatcher?.add(state.dependencies)
1065-
watchPatterns((state.dependencies ?? []).flatMap((dep) => getWatchPatternsForFile(dep)))
1071+
watchPatterns(
1072+
(state.dependencies ?? []).flatMap((dep) =>
1073+
getWatchPatternsForFile(dep, projectConfig.folder)
1074+
)
1075+
)
10661076

10671077
state.configId = getConfigId(state.configPath, state.dependencies)
10681078

@@ -1515,7 +1525,7 @@ async function getConfigFileFromCssFile(cssFile: string): Promise<string | null>
15151525
if (!match) {
15161526
return null
15171527
}
1518-
return path.resolve(path.dirname(cssFile), match.groups.config.slice(1, -1))
1528+
return normalizePath(path.resolve(path.dirname(cssFile), match.groups.config.slice(1, -1)))
15191529
}
15201530

15211531
function getPackageRoot(cwd: string, rootDir: string) {
@@ -1526,7 +1536,7 @@ function getPackageRoot(cwd: string, rootDir: string) {
15261536
if (findUp.sync.exists(pkgJson)) {
15271537
return pkgJson
15281538
}
1529-
if (dir === rootDir) {
1539+
if (dir === path.normalize(rootDir)) {
15301540
return findUp.stop
15311541
}
15321542
},
@@ -1606,7 +1616,7 @@ class TW {
16061616
let ignore = globalSettings.tailwindCSS.files.exclude
16071617
let configFileOrFiles = globalSettings.tailwindCSS.experimental.configFile
16081618

1609-
let base = normalizeFileNameToFsPath(this.initializeParams.rootPath)
1619+
let base = normalizePath(normalizeFileNameToFsPath(this.initializeParams.rootPath))
16101620
let cssFileConfigMap: Map<string, string> = new Map()
16111621
let configTailwindVersionMap: Map<string, string> = new Map()
16121622

@@ -1640,7 +1650,7 @@ class TW {
16401650
([relativeConfigPath, relativeDocumentSelectorOrSelectors]) => {
16411651
return {
16421652
folder: base,
1643-
configPath: path.resolve(userDefinedConfigBase, relativeConfigPath),
1653+
configPath: normalizePath(path.resolve(userDefinedConfigBase, relativeConfigPath)),
16441654
documentSelector: [].concat(relativeDocumentSelectorOrSelectors).map((selector) => ({
16451655
priority: DocumentSelectorPriority.USER_CONFIGURED,
16461656
pattern: normalizePath(path.resolve(userDefinedConfigBase, selector)),

packages/tailwindcss-language-server/src/util/getModuleDependencies.ts

+2
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@ import fs from 'fs'
22
import path from 'path'
33
import resolve from 'resolve'
44
import detective from 'detective'
5+
import normalizePath from 'normalize-path'
56

67
export function getModuleDependencies(modulePath: string): string[] {
78
return _getModuleDependencies(modulePath)
89
.map(({ file }) => file)
910
.filter((file) => file !== modulePath)
11+
.map((file) => normalizePath(file))
1012
}
1113

1214
function createModule(file) {

0 commit comments

Comments
 (0)