Skip to content
Open
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
3 changes: 3 additions & 0 deletions .changelog/cli-windows-watch-path-case.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
### Fixed

- Ensure `@tailwindcss/cli` compares file paths case-insensitively on Windows in watch mode ([#16784](https://github.com/tailwindlabs/tailwindcss/issues/16784))
57 changes: 56 additions & 1 deletion integrations/cli/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,19 @@ import os from 'node:os'
import path from 'node:path'
import { fileURLToPath } from 'node:url'
import { describe } from 'vitest'
import { candidate, css, html, js, json, retryAssertion, test, ts, txt, yaml } from '../utils'
import {
candidate,
css,
html,
IS_WINDOWS,
js,
json,
retryAssertion,
test,
ts,
txt,
yaml,
} from '../utils'

const __dirname = path.dirname(fileURLToPath(import.meta.url))

Expand Down Expand Up @@ -489,6 +501,49 @@ describe.each([
},
)

// https://github.com/tailwindlabs/tailwindcss/issues/16784
test(
'watch mode rebuilds when the input path casing differs from the file on disk',
{
fs: {
'package.json': json`
{
"dependencies": {
"tailwindcss": "workspace:^",
"@tailwindcss/cli": "workspace:^"
}
}
`,
'index.html': html`
<div class="underline text-primary"></div>
`,
'src/index.css': css`@import 'tailwindcss';`,
},
},
async ({ fs, spawn }) => {
let process = await spawn(`${command} --input Src/Index.css --output dist/out.css --watch`)
await process.onStderr((m) => m.includes('Done in'))

await fs.expectFileToContain('dist/out.css', [candidate`underline`])
await fs.expectFileNotToContain('dist/out.css', [candidate`text-primary`])

// Changing the theme is only picked up by a full rebuild, which only
// happens when the changed file is recognized as the input file.
await fs.write(
'src/index.css',
css`
@import 'tailwindcss';
@theme {
--color-primary: #c0ffee;
}
`,
)

await fs.expectFileToContain('dist/out.css', [candidate`text-primary`])
},
{ skip: !IS_WINDOWS },
)

test(
'production build (stdin)',
{
Expand Down
46 changes: 17 additions & 29 deletions packages/@tailwindcss-cli/src/commands/build/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ import {
type SourceMap,
} from '@tailwindcss/node'
import { clearRequireCache } from '@tailwindcss/node/require-cache'
import { Scanner, type ChangedContent } from '@tailwindcss/oxide'
import { Scanner } from '@tailwindcss/oxide'
import { existsSync, type Stats } from 'node:fs'
import fs from 'node:fs/promises'
import path from 'node:path'
import type { Arg, Result } from '../../utils/args'
import { Disposables } from '../../utils/disposables'
import { pathsEqual } from '../../utils/paths-equal'
import {
eprintln,
formatDuration,
Expand All @@ -23,7 +24,7 @@ import {
wordWrap,
} from '../../utils/renderer'
import { serializeBatches, type SerialBatches } from '../../utils/serial-batches'
import { drainStdin, outputFile } from './utils'
import { drainStdin, getRebuildStrategy, outputFile } from './utils'

const css = String.raw
const DEBUG = env.DEBUG
Expand Down Expand Up @@ -154,7 +155,12 @@ export async function handle(args: Result<ReturnType<typeof options>>) {

// Check if the input and output file paths are identical, otherwise return an
// error to the user.
if (args['--input'] === args['--output'] && args['--input'] !== '-') {
if (
args['--input'] &&
args['--input'] !== '-' &&
args['--output'] &&
pathsEqual(args['--input'], args['--output'])
) {
eprintln(
`Specified input file ${highlight(relative(args['--input']))} and output file ${highlight(relative(args['--output']))} are identical.`,
)
Expand Down Expand Up @@ -622,7 +628,9 @@ export async function handle(args: Result<ReturnType<typeof options>>) {
DEBUG && I.end('Scan for candidates')

let files = scanner.scannedFiles.filter(
(file) => file !== args['--output'] && file !== args['--map'],
(file) =>
!pathsEqual(file, args['--output']!) &&
(typeof args['--map'] !== 'string' || !pathsEqual(file, args['--map'])),
)

if (files.length <= 0) return
Expand Down Expand Up @@ -866,31 +874,11 @@ export function filterChangedFiles(
output: string | null,
map: boolean | string,
): string[] {
return files.filter((file) => file !== output && file !== map)
}

function getRebuildStrategy(
files: string[],
fullRebuildPaths: string[],
): { kind: 'incremental'; changedFiles: ChangedContent[] } | { kind: 'full' } {
let changedFiles: ChangedContent[] = []

for (let file of files) {
// If one of the changed files is related to the input CSS or JS
// config/plugin files, then we need to do a full rebuild because
// the theme might have changed.
if (fullRebuildPaths.includes(file)) {
return { kind: 'full' }
}

// Track new and updated files for incremental rebuilds.
changedFiles.push({
file,
extension: path.extname(file).slice(1),
} satisfies ChangedContent)
}

return { kind: 'incremental', changedFiles }
return files.filter(
(file) =>
!(output !== null && pathsEqual(file, output)) &&
!(typeof map === 'string' && pathsEqual(file, map)),
)
}

function createPollingWatcher(cb: () => Promise<void>, pollInterval: number) {
Expand Down
25 changes: 25 additions & 0 deletions packages/@tailwindcss-cli/src/commands/build/utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { describe, expect, it } from 'vitest'
import { getRebuildStrategy } from './utils'

describe('getRebuildStrategy', () => {
it('rebuilds fully when a differently cased input file changes on Windows', () => {
let input = 'C:\\Project\\src\\input.css'
let changed = 'c:\\project\\src\\Input.css'

expect(getRebuildStrategy([changed], [input], 'win32')).toEqual({ kind: 'full' })
})

it('stays incremental for a differently cased path on case-sensitive platforms', () => {
expect(getRebuildStrategy(['/src/Input.css'], ['/src/input.css'], 'linux')).toEqual({
kind: 'incremental',
changedFiles: [{ file: '/src/Input.css', extension: 'css' }],
})
})

it('stays incremental on Windows for a file that is not a rebuild path', () => {
expect(getRebuildStrategy(['C:\\src\\index.html'], ['C:\\src\\input.css'], 'win32')).toEqual({
kind: 'incremental',
changedFiles: [{ file: 'C:\\src\\index.html', extension: 'html' }],
})
})
})
27 changes: 27 additions & 0 deletions packages/@tailwindcss-cli/src/commands/build/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import type { ChangedContent } from '@tailwindcss/oxide'
import fs from 'node:fs/promises'
import path from 'node:path'
import { pathsEqual } from '../../utils/paths-equal'

export function drainStdin() {
return new Promise<string>((resolve, reject) => {
Expand Down Expand Up @@ -33,3 +35,28 @@ export async function outputFile(file: string, contents: string) {
// Write the file
await fs.writeFile(file, contents, 'utf8')
}

export function getRebuildStrategy(
files: string[],
fullRebuildPaths: string[],
platform: NodeJS.Platform = process.platform,
): { kind: 'incremental'; changedFiles: ChangedContent[] } | { kind: 'full' } {
let changedFiles: ChangedContent[] = []

for (let file of files) {
// If one of the changed files is related to the input CSS or JS
// config/plugin files, then we need to do a full rebuild because
// the theme might have changed.
if (fullRebuildPaths.some((p) => pathsEqual(p, file, platform))) {
return { kind: 'full' }
}

// Track new and updated files for incremental rebuilds.
changedFiles.push({
file,
extension: path.extname(file).slice(1),
} satisfies ChangedContent)
}

return { kind: 'incremental', changedFiles }
}
25 changes: 25 additions & 0 deletions packages/@tailwindcss-cli/src/utils/paths-equal.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { describe, expect, it } from 'vitest'
import { pathsEqual } from './paths-equal'

describe('pathsEqual', () => {
it('returns true for identical paths', () => {
expect(pathsEqual('/foo/bar/baz.css', '/foo/bar/baz.css')).toBe(true)
})

it('returns false for completely different paths', () => {
expect(pathsEqual('/foo/bar.css', '/foo/baz.css')).toBe(false)
})

it('ignores case on Windows', () => {
expect(pathsEqual('C:\\src\\Input.css', 'C:\\src\\input.css', 'win32')).toBe(true)
})
Comment thread
mvanhorn marked this conversation as resolved.
Resolved

it('still compares different paths on Windows', () => {
expect(pathsEqual('C:\\src\\input.css', 'C:\\src\\other.css', 'win32')).toBe(false)
})

it('respects case on other platforms', () => {
expect(pathsEqual('/src/Input.css', '/src/input.css', 'linux')).toBe(false)
expect(pathsEqual('/src/Input.css', '/src/input.css', 'darwin')).toBe(false)
})
})
14 changes: 14 additions & 0 deletions packages/@tailwindcss-cli/src/utils/paths-equal.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* Compare two file paths for equality. On Windows, the comparison is
* case-insensitive because the filesystem is case-insensitive.
*/
export function pathsEqual(
a: string,
b: string,
platform: NodeJS.Platform = process.platform,
): boolean {
if (platform === 'win32') {
return a.toLowerCase() === b.toLowerCase()
}
return a === b
}