Skip to content

Commit 772cc6c

Browse files
Fix matching files when config is not in the workspace root (#1412)
In IntelliSense we have this special behavior where if we only detect a single config in a workspace then that config will cover files opened in the entire workspace. We broke this accidentally in v0.14.16 because we made sure document selectors were re-calculated correctly for v4 projects files. With how things are structured/shared this setup is the same for v3 and v4 and we accidentally wiped out the "root" selector we'd add to the project if it was the only one in a given workspace folder. So this isn't *usually* a problem but definitely can be if: - You have a v3 project where the config file isn't in the root - You have a v4 project where automatic source detection was disabled or failed to run This should hopefully fix any project matching issues people have had that broke from v0.14.17 and later. Fixes #1404 (and maybe #1377) - [x] tests
1 parent cb58c8a commit 772cc6c

File tree

6 files changed

+63
-7
lines changed

6 files changed

+63
-7
lines changed

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

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,17 @@ export interface ProjectConfig {
2222
folder: string
2323

2424
/** The path to the config file (if it exists) */
25-
configPath?: string
25+
configPath: string
2626

2727
/** The list of documents that are related to this project */
28-
documentSelector?: DocumentSelector[]
28+
documentSelector: DocumentSelector[]
29+
30+
/**
31+
* Additional selectors that should be matched with this project
32+
*
33+
* These are *never* reset
34+
*/
35+
additionalSelectors: DocumentSelector[]
2936

3037
/** Whether or not this project was explicitly defined by the user */
3138
isUserConfigured: boolean
@@ -65,7 +72,7 @@ export class ProjectLocator {
6572
}
6673

6774
if (projects.length === 1) {
68-
projects[0].documentSelector.push({
75+
projects[0].additionalSelectors.push({
6976
pattern: normalizePath(path.join(this.base, '**')),
7077
priority: DocumentSelectorPriority.ROOT_DIRECTORY,
7178
})
@@ -85,6 +92,10 @@ export class ProjectLocator {
8592
for (let selector of project.documentSelector) {
8693
selector.pattern = normalizeDriveLetter(selector.pattern)
8794
}
95+
96+
for (let selector of project.additionalSelectors) {
97+
selector.pattern = normalizeDriveLetter(selector.pattern)
98+
}
8899
}
89100

90101
return projects
@@ -132,6 +143,7 @@ export class ProjectLocator {
132143
priority: DocumentSelectorPriority.USER_CONFIGURED,
133144
pattern: selector,
134145
})),
146+
additionalSelectors: [],
135147
tailwind,
136148
}
137149
}
@@ -214,6 +226,7 @@ export class ProjectLocator {
214226
isUserConfigured: false,
215227
configPath: config.path,
216228
documentSelector: selectors,
229+
additionalSelectors: [],
217230
tailwind,
218231
}
219232
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1174,7 +1174,7 @@ export async function createProjectService(
11741174

11751175
state,
11761176
documentSelector() {
1177-
return documentSelector
1177+
return [...documentSelector, ...projectConfig.additionalSelectors]
11781178
},
11791179
tryInit,
11801180
async dispose() {

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ export interface TestConfig<Extras extends {}, TestInput extends Record<string,
3232
name: string
3333
inputs?: TestInput[]
3434

35+
skipNPM?: boolean
3536
fs?: Storage
3637
debug?: boolean
3738
prepare?(utils: TestUtils<TestInput>): Promise<Extras>
@@ -70,7 +71,10 @@ async function setup<T, I>(config: TestConfig<T, I>, input: I): Promise<TestUtil
7071

7172
if (config.fs) {
7273
await prepareFileSystem(baseDir, config.fs)
73-
await installDependencies(baseDir, config.fs)
74+
75+
if (!config.skipNPM) {
76+
await installDependencies(baseDir, config.fs)
77+
}
7478
}
7579

7680
onTestFinished(async (ctx) => {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ export class TW {
351351
return {
352352
folder: workspace.folder,
353353
config: workspace.config.path,
354-
selectors: workspace.documentSelector,
354+
selectors: [...workspace.documentSelector, ...workspace.additionalSelectors],
355355
user: workspace.isUserConfigured,
356356
tailwind: workspace.tailwind,
357357
}

packages/tailwindcss-language-server/tests/env/v4.test.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -856,3 +856,42 @@ defineTest({
856856
})
857857
},
858858
})
859+
860+
defineTest({
861+
name: 'Matches files in a workspace when only one config exists and is nested in a package',
862+
skipNPM: true,
863+
fs: {
864+
'some-dir/package.json': json`{"type": "commonjs"}`,
865+
'some-dir/tailwind.config.js': js`
866+
module.exports = { content: [] }
867+
`,
868+
},
869+
prepare: async ({ root }) => ({ client: await createClient({ root }) }),
870+
handle: async ({ client }) => {
871+
let doc = await client.open({
872+
name: 'files/index.html',
873+
lang: 'html',
874+
text: html`<div class="bg-[#000]"></div>`,
875+
})
876+
877+
// <div class="bg-[#000]"></div>
878+
// ^
879+
let hover = await doc.hover({ line: 0, character: 13 })
880+
881+
expect(hover).toEqual({
882+
contents: {
883+
language: 'css',
884+
value: dedent`
885+
.bg-\[\#000\] {
886+
--tw-bg-opacity: 1;
887+
background-color: rgb(0 0 0 / var(--tw-bg-opacity, 1)) /* #000000 */;
888+
}
889+
`,
890+
},
891+
range: {
892+
start: { line: 0, character: 12 },
893+
end: { line: 0, character: 21 },
894+
},
895+
})
896+
},
897+
})

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 matching files when config is not in the workspace root ([#1412](https://github.com/tailwindlabs/tailwindcss-intellisense/pull/1412))
66

77
## 0.14.21
88

0 commit comments

Comments
 (0)