Skip to content

Commit

Permalink
Fix v4 theme reloading on Windows (#960)
Browse files Browse the repository at this point in the history
We now normalize case of windows drive letters of watched files because we may recieve a path that has `C:` at the start of a path in some places and `c:` in others because they’re coming from different sources (filesystem vs VS Code file watching notifications).
  • Loading branch information
thecrypticace committed May 2, 2024
1 parent 9d7006d commit 086ffff
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
5 changes: 4 additions & 1 deletion packages/tailwindcss-language-server/src/tw.ts
Expand Up @@ -40,7 +40,7 @@ import resolveFrom from './util/resolveFrom'
import * as parcel from './watcher/index.js'
import { equal } from '@tailwindcss/language-service/src/util/array'
import { CONFIG_GLOB, CSS_GLOB, PACKAGE_LOCK_GLOB } from './lib/constants'
import { clearRequireCache, isObject, changeAffectsFile } from './utils'
import { clearRequireCache, isObject, changeAffectsFile, normalizeDriveLetter } from './utils'
import { DocumentService } from './documents'
import { createProjectService, type ProjectService } from './projects'
import { type SettingsCache, createSettingsCache } from './config'
Expand Down Expand Up @@ -273,6 +273,7 @@ export class TW {

changeLoop: for (let change of changes) {
let normalizedFilename = normalizePath(change.file)
normalizedFilename = normalizeDriveLetter(normalizedFilename)

for (let ignorePattern of ignore) {
let isIgnored = picomatch(ignorePattern, { dot: true })
Expand Down Expand Up @@ -312,6 +313,8 @@ export class TW {
...project.projectConfig.config.entries.map((entry) => entry.path),
]

reloadableFiles = reloadableFiles.map(normalizeDriveLetter)

if (!changeAffectsFile(normalizedFilename, reloadableFiles)) continue

needsSoftRestart = true
Expand Down
11 changes: 11 additions & 0 deletions packages/tailwindcss-language-server/src/utils.ts
Expand Up @@ -70,6 +70,17 @@ export function dirContains(dir: string, file: string): boolean {
return !!relative && !relative.startsWith('..') && !path.isAbsolute(relative)
}

const WIN_DRIVE_LETTER = /^([a-zA-Z]):/

/**
* Windows drive letters are case-insensitive and we may get them as either
* lower or upper case. This function normalizes the drive letter to uppercase
* to be consistent with the rest of the codebase.
*/
export function normalizeDriveLetter(filepath: string) {
return filepath.replace(WIN_DRIVE_LETTER, (_, letter) => letter.toUpperCase() + ':')
}

export function changeAffectsFile(change: string, files: string[]): boolean {
for (let file of files) {
if (change === file || dirContains(change, file)) {
Expand Down

0 comments on commit 086ffff

Please sign in to comment.