tailwindcss/packages/@tailwindcss-upgrade/src/index.ts
Robin Malfait e4856c9720
Make upgrade tooling more stable (#19846)
This PR is an attempt to make the upgrade tooling more stable.


### TL;DR

1. When migrating from Tailwind CSS v3 → Tailwind CSS v4, only migrate
files listed in the `config.content` instead of relying on v4's auto
content detection feature
2. Skip writing files that have not been changed
3. Write changed files in a safe way: first write to a temporary file,
then rename the file atomically
4. Never migrate files that are git ignored, even if they are listed in
the `config.content` file
5. Always ignore `.env` and `.env.*` files when scanning for files. Most
people will have this in their `.gitignore` file, but if not, then this
is a fallback mechanism.

---

Looking at the #18972 issue, it looks like some people are running into
weird situations where some of the contents is just gone.

I have never been able to reproduce this on my own devices and in my own
projects unfortunately. But there is definitely _something_ happening
that's not right that people are running into.

Therefore, this PR is an attempt to fix what I think _might_ be wrong,
but I'm not 100% sure if these fixes are enough, or if something else is
still happening here.

This builds on top of the #19779 PR which has some small fixes, but is
incomplete to make this work.

### What's happening

Looking at some of the comments, it looks like a few things are
happening such as:

1. The upgrade tool is emptying out my files — it looks like these are
only happening if you ctrl+c while the process is taking a while. It
could be that a lot of files are being checked and therefore the tooling
looks like its stuck.
6. The upgrade tool is looking at files it shouldn't look at — in
Tailwind CSS v4 we have this concept of the auto-content detection. This
means that we will look at any plain text file that is not git ignored.

### Fixes


#### Emptying out files

The files being emptied looks like it's because how `fs.writeFile`
behaves by default. It opens the file handle with the `w` flag, which
will first truncate the file before writing the new contents. This is
not a single atomic operation, so a killed process in the middle will
cause invalid state.

When we migrate your template files, everything is happening in promises
to migrate things at the same time. When a lot of files are being
scanned, truncating might have happened already before we write the new
content. Since we migrate a bunch of files in parallel, a ctrl+c could
cause data loss in multiple files.

To mitigate this, I switched to an alternative way of writing files. 

1. First, we do some quick checks where if the contents didn't change we
just bail out immediately. Files that don't include Tailwind CSS classes
won't change, and therefore we don't need to override these files with
the same contents.
2. When the migrated contents is empty, we bail out as well. I'm 100%
sure that this is not the spot where the "emptying out" happens, I still
believe it happens in the `writeFile` itself, but added it just in case.
7. Next, I introduced a safe write, where we first write to a temporary
file in the same folder. We could write it to `/tmp`, but then we can't
guarantee that we are on the same file system.
   
If we ctrl+c at this stage, then the worst case scenario is that you
have additional temporary files in your project, but your original files
are still there.
   
Once that file was written, we will use the atomic `fs.rename`. This
should be atomic as long as we are on the same file system, so either
the rename didn't happen yet, or it completed.

I added an integration test for this, but I had to change the
`writeFile` implementation slightly. In the test, we will truncate the
file first, after that we will write the new contents. This is so that
we have enough time to kill the current process and allows us to verify
that we didn't clear out the file. Again, this is a hacky way of testing
this, just because I can't reproduce this issue myself, let alone
reproduce it reliable in a CI environment.

Note: we are also using `realpath` to make sure that we are updating the
real file. Otherwise, if we were dealing with a symlinked file, we would
override the symlink with a "hard" copy instead.

#### Touching files that should not be touched

During the migration, we rely on the Tailwind CSS v4 auto detection
logic which means that it will scan any plain text file that is not git
ignored. Therefore changes to php files could happen because in theory
they could contain Tailwind CSS classes.

To solve this, when migrating from Tailwind CSS v3 to Tailwind CSS v4,
we will _only_ take the sources into account that were listed in the
`config.content` array. Since this was a requirement in Tailwind CSS v3,
it should be safe to rely on this array.

Additionally, this will make sure that we are dealing with way fewer
files to migrate as well.

On top of that, files that match the patterns in the content array that
are git ignored will also be skipped. This is to prevent that we mutate
files in `node_modules` for example.

In one of the comments I read that `.env` files were emptied out. In
most cases people will have these files gitignored but I explicitly
added `.env` and `.env.*` as files to never ever touch by default when
scanning.

Last but not least, this also updates the output a little bit of the
upgrade tool in case we skip content files (because of git ignore) and
if we changed a file.

<img width="1122" height="1376" alt="image"
src="https://github.com/user-attachments/assets/318fdbbf-e319-4c7e-9648-ee9283842624"
/>

- "Git ignored folder, skipping: `./node_modules`": this is because the
content array looks like this while the `node_modules` are being
ignored:
<img width="1090" height="398" alt="image"
src="https://github.com/user-attachments/assets/7d694720-5671-47ec-bb2e-f24c5f2c4248"
/>

- "Migrated
`./resources/views/vendor/filament-panels/components/logo.blade.php`":
this is because **I** made a change to showcase this feature.

Fixes: #18972
Closes: #19779

### Test plan

1. Existing tests still pass
1. Added a dedicated integration test to ensure that we only take
`config.content` into account when migrating from Tailwind CSS v3 to
Tailwind CSS v4 projects.
1. Added a dedicated integration test to make sure that files listed in
`config.content` that are also git ignored, will still be skipped.
1. Added a dedicated integration test to ensure that when `writeFile` is
cancelled mid-write that our old files are still present.
1. Added a dedicated integration test to ensure that we ignore `.env`
and `.env.*` files even if you didn't git ignore them.

[ci-all] To verify on Windows

---------

Co-authored-by: Sami <sychocouldy@gmail.com>
2026-03-24 17:24:12 +01:00

450 lines
15 KiB
JavaScript

#!/usr/bin/env node
import { Scanner } from '@tailwindcss/oxide'
import { globby, isGitIgnored } from 'globby'
import fs from 'node:fs/promises'
import path from 'node:path'
import pc from 'picocolors'
import postcss from 'postcss'
import { migrateJsConfig } from './codemods/config/migrate-js-config'
import { migratePostCSSConfig } from './codemods/config/migrate-postcss'
import { analyze as analyzeStylesheets } from './codemods/css/analyze'
import { formatNodes } from './codemods/css/format-nodes'
import { linkConfigs as linkConfigsToStylesheets } from './codemods/css/link'
import { migrate as migrateStylesheet } from './codemods/css/migrate'
import { sortBuckets } from './codemods/css/sort-buckets'
import { split as splitStylesheets } from './codemods/css/split'
import { migrate as migrateTemplate } from './codemods/template/migrate'
import { prepareConfig } from './codemods/template/prepare-config'
import { help } from './commands/help'
import { Stylesheet } from './stylesheet'
import { args, type Arg } from './utils/args'
import { isRepoDirty } from './utils/git'
import { pkg } from './utils/packages'
import { eprintln, error, header, highlight, info, relative, success } from './utils/renderer'
import * as version from './utils/version'
import { writeFileSafely } from './utils/write-file-safely'
const options = {
'--config': { type: 'string', description: 'Path to the configuration file', alias: '-c' },
'--help': { type: 'boolean', description: 'Display usage information', alias: '-h' },
'--force': { type: 'boolean', description: 'Force the migration', alias: '-f' },
'--version': { type: 'boolean', description: 'Display the version number', alias: '-v' },
} satisfies Arg
const flags = args(options)
if (flags['--help']) {
help({
usage: ['npx @tailwindcss/upgrade'],
options,
})
process.exit(0)
}
async function run() {
let base = process.cwd()
let isIgnored = await isGitIgnored({ cwd: base })
eprintln(header())
eprintln()
let cleanup: (() => void)[] = []
if (!flags['--force']) {
// Require a clean git directory
if (isRepoDirty()) {
error('Git directory is not clean. Please stash or commit your changes before migrating.')
info(
`You may use the ${highlight('--force')} flag to silence this warning and perform the migration.`,
)
process.exit(1)
}
}
info(`Upgrading from Tailwind CSS ${highlight(`v${version.installedTailwindVersion(base)}`)}`, {
prefix: '↳ ',
})
if (version.installedTailwindVersion(base) !== version.expectedTailwindVersion(base)) {
let pkgManager = await pkg(base).manager()
error(
[
'Version mismatch',
'',
pc.dim('```diff'),
`${pc.red('-')} ${`${pc.dim('"tailwindcss":')} ${`${pc.dim('"')}${pc.blue(version.expectedTailwindVersion(base))}${pc.dim('"')}`}`} (expected version in ${highlight('package.json')})`,
`${pc.green('+')} ${`${pc.dim('"tailwindcss":')} ${`${pc.dim('"')}${pc.blue(version.installedTailwindVersion(base))}${pc.dim('"')}`}`} (installed version in ${highlight('node_modules')})`,
pc.dim('```'),
'',
`Make sure to run ${highlight(`${pkgManager} install`)} and try again.`,
].join('\n'),
{
prefix: '↳ ',
},
)
process.exit(1)
}
{
// Stylesheet migrations
// Use provided files
let files = flags._.map((file) => path.resolve(base, file))
// Discover CSS files in case no files were provided
if (files.length === 0) {
info('Searching for CSS files in the current directory and its subdirectories…')
files = await globby(['**/*.css'], {
cwd: base,
absolute: true,
gitignore: true,
// gitignore: true will first search for all .gitignore including node_modules folders, this makes the initial search much faster
ignore: ['**/node_modules/**'],
})
}
// Ensure we are only dealing with CSS files
files = files.filter((file) => file.endsWith('.css'))
// Analyze the stylesheets
let loadResults = await Promise.allSettled(files.map((filepath) => Stylesheet.load(filepath)))
// Load and parse all stylesheets
for (let result of loadResults) {
if (result.status === 'rejected') {
error(`${result.reason?.message ?? result.reason}`, { prefix: '↳ ' })
}
}
let stylesheets = loadResults
.filter((result) => result.status === 'fulfilled')
.map((result) => result.value)
let originals = new Map(stylesheets.map((sheet) => [sheet, sheet.root.toString()]))
// Analyze the stylesheets
try {
await analyzeStylesheets(stylesheets)
} catch (e: any) {
error(`${e?.message ?? e}`, { prefix: '↳ ' })
}
// Ensure stylesheets are linked to configs. But this is only necessary when
// migrating from v3 to v4.
if (version.isMajor(3)) {
try {
await linkConfigsToStylesheets(stylesheets, {
configPath: flags['--config'],
base,
})
} catch (e: any) {
error(`${e?.message ?? e}`, { prefix: '↳ ' })
}
}
// Migrate js config files, linked to stylesheets
if (stylesheets.some((sheet) => sheet.isTailwindRoot && sheet.linkedConfigPath)) {
info('Migrating JavaScript configuration files…')
}
let configBySheet = new Map<Stylesheet, Awaited<ReturnType<typeof prepareConfig>>>()
let jsConfigMigrationBySheet = new Map<
Stylesheet,
Awaited<ReturnType<typeof migrateJsConfig>>
>()
for (let sheet of stylesheets) {
if (!sheet.isTailwindRoot) continue
if (!version.isMajor(3) && !sheet.linkedConfigPath) continue
let config = await prepareConfig(sheet.linkedConfigPath, { base })
configBySheet.set(sheet, config)
let jsConfigMigration = await migrateJsConfig(
config.designSystem,
config.configFilePath,
base,
)
jsConfigMigrationBySheet.set(sheet, jsConfigMigration)
if (jsConfigMigration !== null) {
// Remove the JS config if it was fully migrated
cleanup.push(() => fs.rm(config.configFilePath))
}
if (jsConfigMigration !== null) {
success(
`Migrated configuration file: ${highlight(relative(config.configFilePath, base))}`,
{ prefix: '↳ ' },
)
}
}
// Migrate each CSS file
if (stylesheets.length > 0) {
info('Migrating stylesheets…')
}
await Promise.all(
stylesheets.map(async (sheet) => {
try {
let config = configBySheet.get(sheet)
let jsConfigMigration = jsConfigMigrationBySheet.get(sheet) ?? null
if (!config) {
for (let parent of sheet.ancestors()) {
if (parent.isTailwindRoot) {
config ??= configBySheet.get(parent)!
jsConfigMigration ??= jsConfigMigrationBySheet.get(parent) ?? null
break
}
}
}
await migrateStylesheet(sheet, {
newPrefix: config?.newPrefix ?? null,
designSystem: config?.designSystem ?? (await sheet.designSystem()),
userConfig: config?.userConfig ?? null,
configFilePath: config?.configFilePath ?? null,
jsConfigMigration,
})
} catch (e: any) {
error(`${e?.message ?? e} in ${highlight(relative(sheet.file!, base))}`, { prefix: '↳ ' })
}
}),
)
// Split up stylesheets (as needed)
if (version.isMajor(3)) {
try {
await splitStylesheets(stylesheets)
} catch (e: any) {
error(`${e?.message ?? e}`, { prefix: '↳ ' })
}
// Cleanup `@import "…" layer(utilities)`
for (let sheet of stylesheets) {
for (let importRule of sheet.importRules) {
if (!importRule.raws.tailwind_injected_layer) continue
let importedSheet = stylesheets.find(
(sheet) => sheet.id === importRule.raws.tailwind_destination_sheet_id,
)
if (!importedSheet) continue
// Only remove the `layer(…)` next to the import if any of the children
// contain `@utility`. Otherwise `@utility` will not be top-level.
if (
!importedSheet.containsRule((node) => node.type === 'atrule' && node.name === 'utility')
) {
continue
}
// Make sure to remove the `layer(…)` from the `@import` at-rule
importRule.params = importRule.params.replace(/ layer\([^)]+\)/, '').trim()
}
}
}
// Format nodes
for (let sheet of stylesheets) {
if (originals.get(sheet) === sheet.root.toString()) continue
await postcss([sortBuckets(), formatNodes()]).process(sheet.root!, { from: sheet.file! })
}
// Write all files to disk
for (let sheet of stylesheets) {
if (!sheet.file) continue
await writeFileSafely(sheet.file, sheet.root.toString())
if (sheet.isTailwindRoot) {
success(`Migrated stylesheet: ${highlight(relative(sheet.file, base))}`, { prefix: '↳ ' })
}
}
info('Updating dependencies…')
{
let pkgManager = pkg(base)
let dependencies = [
'tailwindcss',
'@tailwindcss/cli',
'@tailwindcss/postcss',
'@tailwindcss/vite',
'@tailwindcss/node',
'@tailwindcss/oxide',
'prettier-plugin-tailwindcss',
].filter((dependency) => dependency === 'tailwindcss' || pkgManager.has(dependency))
try {
await pkgManager.add(dependencies.map((dependency) => `${dependency}@latest`))
for (let dependency of dependencies) {
success(`Updated package: ${highlight(dependency)}`, { prefix: '↳ ' })
}
} catch {}
}
let tailwindRootStylesheets = stylesheets.filter((sheet) => sheet.isTailwindRoot && sheet.file)
// Migrate source files
if (tailwindRootStylesheets.length > 0) {
info('Migrating templates…')
}
{
let seenFiles = new Set()
// Template migrations
for (let sheet of tailwindRootStylesheets) {
let compiler = await sheet.compiler()
if (!compiler) continue
let designSystem = await sheet.designSystem()
if (!designSystem) continue
let config = configBySheet.get(sheet)
// Figure out the source files to migrate
let sources = (() => {
// Disable auto source detection
if (compiler.root === 'none') {
return []
}
// No root specified
if (compiler.root === null) {
// When coming from Tailwind CSS v3, we have to use the
// `config.sources` (which came from `config.content` originally)
if (version.isMajor(3)) {
if (config?.sources) {
return config.sources.map((source) => ({ ...source, negated: false }))
}
// When we don't have any sources, then we have to fallback to no
// sources at all. We cannot fallback to the `**/*` pattern.
return []
}
// When we are upgrading a Tailwind CSS v4 and up version, we use
// the default `**/*` pattern. All custom `@source` directives will
// be attached later as sources.
return [{ base, pattern: '**/*', negated: false }]
}
// Use the specified root
return [{ ...compiler.root, negated: false }]
})().concat(compiler.sources)
let scanner = new Scanner({ sources })
let filesToMigrate = []
let ignoredPaths = new Set<string>()
for (let file of scanner.files) {
file = await fs.realpath(file).catch(() => file) // Ensure we are dealing with the real path, not symlinks
if (file.endsWith('.css')) continue
// When a file is git ignored, then we don't want to migrate it even
// if it was listed in the `config.content` array or part of any
// `@source` directives.
//
// We can make this an option later to explicitly allow this, but
// this should be the default. This guarantees that:
//
// 1. Files coming from node_modules aren't touched
// 2. Generated files aren't changed (the source should update, not the target)
// 3. You can see all the changes that happened
try {
if (isIgnored(file)) {
let culprit = file
// To prevent print all ignored files, we can also walk up the
// parent tree and log those instead _if_ they are:
//
// 1. Also git ignored
// 2. Are not going outside of the current repo
let parent = path.dirname(file)
do {
try {
if (isIgnored(parent)) {
culprit = parent
}
} catch {
// Escaping the current repo
break
}
parent = path.dirname(parent)
} while (parent)
if (ignoredPaths.has(culprit)) continue // Already logged, skip
ignoredPaths.add(culprit)
if (culprit === file) {
info(`Git ignored, skipping: ${highlight(relative(culprit, base))}`, {
prefix: '↳ ',
})
} else {
info(`Git ignored folder, skipping: ${highlight(relative(culprit, base))}`, {
prefix: '↳ ',
})
}
continue
}
} catch (err) {
info(`Outside repository, skipping: ${highlight(relative(file, base))}`, {
prefix: '↳ ',
})
// Skip this file when we run into errors. E.g.: when the current
// file is not part of the current git repo it will throw an error.
continue
}
if (seenFiles.has(file)) continue
seenFiles.add(file)
filesToMigrate.push(file)
}
// Migrate each file
let changes = 0
await Promise.allSettled(
filesToMigrate.map(async (file) => {
let changed = await migrateTemplate(designSystem, config?.userConfig ?? null, file)
if (changed) {
changes++
info(`Migrated ${highlight(relative(file, base))}`, { prefix: '↳ ' })
}
}),
)
if (config?.configFilePath) {
success(
`Migrated templates for configuration file: ${highlight(relative(config.configFilePath, base))} (${changes} file${changes === 1 ? '' : 's'} changed)`,
{ prefix: '↳ ' },
)
} else {
success(
`Migrated templates for: ${highlight(relative(sheet.file ?? '<unknown>', base))} (${changes} file${changes === 1 ? '' : 's'} changed)`,
{ prefix: '↳ ' },
)
}
}
}
}
if (version.isMajor(3)) {
// PostCSS config migration
await migratePostCSSConfig(base)
}
// Run all cleanup functions because we completed the migration
await Promise.allSettled(cleanup.map((fn) => fn()))
// Figure out if we made any changes
if (isRepoDirty()) {
success('Verify the changes and commit them to your repository.')
} else {
success('No changes were made to your repository.')
}
}
run()
.then(() => process.exit(0))
.catch((err) => {
console.error(err)
process.exit(1)
})