Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only watch directories up to workspace root #709

Merged
merged 4 commits into from
Jan 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 22 additions & 12 deletions packages/tailwindcss-language-server/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -346,16 +346,22 @@ function changeAffectsFile(change: string, files: string[]): boolean {

// We need to add parent directories to the watcher:
// https://github.com/microsoft/vscode/issues/60813
function getWatchPatternsForFile(file: string): string[] {
function getWatchPatternsForFile(file: string, root: string): string[] {
let tmp: string
let dir = path.dirname(file)
let patterns: string[] = [file, dir]
if (dir === root) {
return patterns
}
while (true) {
dir = path.dirname((tmp = dir))
if (tmp === dir) {
break
} else {
patterns.push(dir)
if (dir === root) {
break
}
}
}
return patterns
Expand Down Expand Up @@ -426,8 +432,8 @@ async function createProjectService(
deps = getModuleDependencies(projectConfig.configPath)
} catch {}
watchPatterns([
...getWatchPatternsForFile(projectConfig.configPath),
...deps.flatMap((dep) => getWatchPatternsForFile(dep)),
...getWatchPatternsForFile(projectConfig.configPath, projectConfig.folder),
...deps.flatMap((dep) => getWatchPatternsForFile(dep, projectConfig.folder)),
])
}

Expand All @@ -445,7 +451,7 @@ async function createProjectService(
let file = normalizePath(change.file)

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

if (!isConfigFile && !isDependency && !isPackageFile) continue
Expand Down Expand Up @@ -550,7 +556,7 @@ async function createProjectService(
throw new SilentError('No config file found.')
}

watchPatterns(getWatchPatternsForFile(configPath))
watchPatterns(getWatchPatternsForFile(configPath, projectConfig.folder))

const pnpPath = findUp.sync(
(dir) => {
Expand All @@ -562,7 +568,7 @@ async function createProjectService(
if (findUp.sync.exists(pnpFile)) {
return pnpFile
}
if (dir === folder) {
if (dir === path.normalize(folder)) {
return findUp.stop
}
},
Expand Down Expand Up @@ -1044,7 +1050,11 @@ async function createProjectService(
// }
state.dependencies = getModuleDependencies(state.configPath)
// chokidarWatcher?.add(state.dependencies)
watchPatterns((state.dependencies ?? []).flatMap((dep) => getWatchPatternsForFile(dep)))
watchPatterns(
(state.dependencies ?? []).flatMap((dep) =>
getWatchPatternsForFile(dep, projectConfig.folder)
)
)

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

Expand Down Expand Up @@ -1505,7 +1515,7 @@ async function getConfigFileFromCssFile(cssFile: string): Promise<string | null>
if (!match) {
return null
}
return path.resolve(path.dirname(cssFile), match.groups.config.slice(1, -1))
return normalizePath(path.resolve(path.dirname(cssFile), match.groups.config.slice(1, -1)))
}

function getPackageRoot(cwd: string, rootDir: string) {
Expand All @@ -1516,7 +1526,7 @@ function getPackageRoot(cwd: string, rootDir: string) {
if (findUp.sync.exists(pkgJson)) {
return pkgJson
}
if (dir === rootDir) {
if (dir === path.normalize(rootDir)) {
return findUp.stop
}
},
Expand Down Expand Up @@ -1596,7 +1606,7 @@ class TW {
let ignore = globalSettings.tailwindCSS.files.exclude
let configFileOrFiles = globalSettings.tailwindCSS.experimental.configFile

let base = normalizeFileNameToFsPath(this.initializeParams.rootPath)
let base = normalizePath(normalizeFileNameToFsPath(this.initializeParams.rootPath))
let cssFileConfigMap: Map<string, string> = new Map()
let configTailwindVersionMap: Map<string, string> = new Map()

Expand Down Expand Up @@ -1630,10 +1640,10 @@ class TW {
([relativeConfigPath, relativeDocumentSelectorOrSelectors]) => {
return {
folder: base,
configPath: path.resolve(userDefinedConfigBase, relativeConfigPath),
configPath: normalizePath(path.resolve(userDefinedConfigBase, relativeConfigPath)),
documentSelector: [].concat(relativeDocumentSelectorOrSelectors).map((selector) => ({
priority: DocumentSelectorPriority.USER_CONFIGURED,
pattern: path.resolve(userDefinedConfigBase, selector),
pattern: normalizePath(path.resolve(userDefinedConfigBase, selector)),
})),
isUserConfigured: true,
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ import fs from 'fs'
import path from 'path'
import resolve from 'resolve'
import detective from 'detective'
import normalizePath from 'normalize-path'

export function getModuleDependencies(modulePath: string): string[] {
return _getModuleDependencies(modulePath)
.map(({ file }) => file)
.filter((file) => file !== modulePath)
.map((file) => normalizePath(file))
}

function createModule(file) {
Expand Down