Skip to content

Commit 405502b

Browse files
Fix loading projects on Windows network drives (#996)
* Improve support on windows network shares * Don’t use network share paths on Windows Some bugs in enhanced-resolve prevent this from working currently * Update changelog
1 parent 486c239 commit 405502b

File tree

6 files changed

+63
-9
lines changed

6 files changed

+63
-9
lines changed

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

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import * as path from 'node:path'
33
import * as fs from 'node:fs/promises'
44
import glob from 'fast-glob'
55
import picomatch from 'picomatch'
6-
import normalizePath from 'normalize-path'
76
import type { Settings } from '@tailwindcss/language-service/src/util/state'
87
import { CONFIG_GLOB, CSS_GLOB } from './lib/constants'
98
import { readCssFile } from './util/css'
@@ -14,9 +13,8 @@ import { CacheMap } from './cache-map'
1413
import { getPackageRoot } from './util/get-package-root'
1514
import resolveFrom from './util/resolveFrom'
1615
import { type Feature, supportedFeatures } from '@tailwindcss/language-service/src/features'
17-
import { pathToFileURL } from 'node:url'
1816
import { resolveCssImports } from './resolve-css-imports'
19-
import { normalizeDriveLetter } from './utils'
17+
import { normalizeDriveLetter, normalizePath, pathToFileURL } from './utils'
2018

2119
export interface ProjectConfig {
2220
/** The folder that contains the project */
@@ -244,8 +242,20 @@ export class ProjectLocator {
244242
concurrency: Math.max(os.cpus().length, 1),
245243
})
246244

247-
// Resolve symlinks for all found files
248-
files = await Promise.all(files.map(async (file) => normalizePath(await fs.realpath(file))))
245+
files = await Promise.all(
246+
files.map(async (file) => {
247+
// Resolve symlinks for all found files
248+
let actualPath = await fs.realpath(file)
249+
250+
// Ignore network paths on Windows. Resolving relative paths on a
251+
// netshare throws in `enhanced-resolve` :/
252+
if (actualPath.startsWith('\\') && process.platform === 'win32') {
253+
return normalizePath(file)
254+
}
255+
256+
return normalizePath(actualPath)
257+
}),
258+
)
249259

250260
// Deduplicate the list of files and sort them for deterministic results
251261
// across environments

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import { FileChangeType } from 'vscode-languageserver/node'
2020
import type { TextDocument } from 'vscode-languageserver-textdocument'
2121
import { URI } from 'vscode-uri'
2222
import { showError, SilentError } from './util/error'
23-
import normalizePath from 'normalize-path'
2423
import * as path from 'path'
2524
import * as fs from 'fs'
2625
import findUp from 'find-up'
@@ -70,14 +69,15 @@ import {
7069
clearRequireCache,
7170
withFallback,
7271
isObject,
72+
pathToFileURL,
7373
changeAffectsFile,
74+
normalizePath,
7475
} from './utils'
7576
import type { DocumentService } from './documents'
7677
import type { ProjectConfig } from './project-locator'
7778
import { supportedFeatures } from '@tailwindcss/language-service/src/features'
7879
import { loadDesignSystem } from './util/v4'
7980
import { readCssFile } from './util/css'
80-
import { pathToFileURL } from 'url'
8181

8282
const colorNames = Object.keys(namedColors)
8383

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// https://github.com/tailwindlabs/tailwindcss/blob/bac5ecf0040aa9a788d1b22d706506146ee831ff/src/lib/getModuleDependencies.js
22
import fs from 'fs'
33
import path from 'path'
4-
import normalizePath from 'normalize-path'
4+
import { normalizePath } from '../utils'
55

66
let jsExtensions = ['.js', '.cjs', '.mjs']
77

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { equal } from '@tailwindcss/language-service/src/util/array'
2+
import * as path from 'node:path'
23
import { createResolver } from './resolve'
34

45
let pnpApi: any
@@ -16,8 +17,17 @@ export function setPnpApi(newPnpApi: any): void {
1617
}
1718

1819
export default function resolveFrom(from?: string, id?: string): string {
20+
// Network share path on Windows
1921
if (id.startsWith('\\\\')) return id
2022

23+
// Normalized network share path on Windows
24+
if (id.startsWith('//') && path.sep === '\\') return id
25+
26+
// Normalized network share path on Windows
27+
if (from.startsWith('//') && path.sep === '\\') {
28+
from = '\\\\' + from.slice(2)
29+
}
30+
2131
let newExtensions = Object.keys(require.extensions)
2232
if (!equal(newExtensions, extensions)) {
2333
extensions = newExtensions

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import Module from 'node:module'
22
import path from 'node:path'
3+
import { URI } from 'vscode-uri'
4+
import normalizePathBase from 'normalize-path'
5+
import { pathToFileURL as pathToFileURLBase } from 'node:url'
36

47
export function withoutLogs<T>(getter: () => T): T {
58
let fns = {
@@ -89,3 +92,34 @@ export function changeAffectsFile(change: string, files: string[]): boolean {
8992
}
9093
return false
9194
}
95+
96+
export function normalizePath(originalPath: string) {
97+
let normalized = normalizePathBase(originalPath)
98+
99+
// This is Windows network share but the normalize path had one of the leading
100+
// slashes stripped so we need to add it back
101+
if (
102+
originalPath.startsWith('\\\\') &&
103+
normalized.startsWith('/') &&
104+
!normalized.startsWith('//')
105+
) {
106+
return `/${normalized}`
107+
}
108+
109+
return normalized
110+
}
111+
112+
export function pathToFileURL(filepath: string) {
113+
try {
114+
return pathToFileURLBase(filepath)
115+
} catch (err) {
116+
if (process.platform !== 'win32') throw err
117+
118+
// If `pathToFileURL` failsed on windows it's probably because the path was
119+
// a windows network share path and there were mixed slashes.
120+
// Fix the path and try again.
121+
filepath = URI.file(filepath).fsPath
122+
123+
return pathToFileURLBase(filepath)
124+
}
125+
}

packages/vscode-tailwindcss/CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
## Prerelease
44

5-
- Nothing yet!
5+
- Fix loading projects on Windows network drives ([#996](https://github.com/tailwindlabs/tailwindcss-intellisense/pull/996))
66

77
## 0.12.1
88

0 commit comments

Comments
 (0)