Skip to content

Commit e4856c9

Browse files
RobinMalfaitSychO9
andauthored
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>
1 parent 2c1ef9e commit e4856c9

9 files changed

Lines changed: 512 additions & 24 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2323
- Canonicalization: collapse `overflow-{x,y}-*` into `overflow-*` ([#19842](https://github.com/tailwindlabs/tailwindcss/pull/19842))
2424
- Canonicalization: collapse `overscroll-{x,y}-*` into `overscroll-*` ([#19842](https://github.com/tailwindlabs/tailwindcss/pull/19842))
2525
- Read from `--placeholder-color` instead of `--background-color` for `placeholder-*` utilities ([#19843](https://github.com/tailwindlabs/tailwindcss/pull/19843))
26+
- Upgrade: Ensure files are not emptied out when killing the upgrade process while it's running ([#19846](https://github.com/tailwindlabs/tailwindcss/pull/19846))
27+
- Upgrade: Use `config.content` when migrating from Tailwind CSS v3 to Tailwind CSS v4 ([#19846](https://github.com/tailwindlabs/tailwindcss/pull/19846))
28+
- Upgrade: Never migrate files that are ignored by git ([#19846](https://github.com/tailwindlabs/tailwindcss/pull/19846))
29+
- Add `.env` and `.env.*` to default ignored content files ([#19846](https://github.com/tailwindlabs/tailwindcss/pull/19846))
2630

2731
## [4.2.2] - 2026-03-18
2832

crates/oxide/src/scanner/fixtures/ignored-files.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@ package-lock.json
22
pnpm-lock.yaml
33
bun.lockb
44
.gitignore
5+
.env
6+
.env.*

integrations/upgrade/index.test.ts

Lines changed: 243 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import path from 'node:path'
22
import { isRepoDirty } from '../../packages/@tailwindcss-upgrade/src/utils/git'
3-
import { candidate, css, html, js, json, test, ts, yaml } from '../utils'
3+
import { candidate, css, html, js, json, test, ts, txt, yaml } from '../utils'
44

55
test(
66
'error when no CSS file with @tailwind is used',
@@ -171,6 +171,56 @@ test(
171171
},
172172
)
173173

174+
test(
175+
'only migrates files matched by `config.content` when upgrading from v3 to v4',
176+
{
177+
fs: {
178+
'package.json': json`
179+
{
180+
"dependencies": {
181+
"tailwindcss": "^3",
182+
"@tailwindcss/upgrade": "workspace:^"
183+
}
184+
}
185+
`,
186+
'tailwind.config.js': js`
187+
/** @type {import('tailwindcss').Config} */
188+
module.exports = {
189+
content: ['./src/**/*.html'],
190+
}
191+
`,
192+
'src/index.html': html`
193+
<div class="order-[0] bg-[--my-red]"></div>
194+
`,
195+
'src/input.css': css`
196+
@tailwind base;
197+
@tailwind components;
198+
@tailwind utilities;
199+
`,
200+
'templates/email.php': html`
201+
<div class="order-[0] bg-[--my-red]"></div>
202+
`,
203+
'notes/unrelated.txt': `order-[0] bg-[--my-red]`,
204+
},
205+
},
206+
async ({ exec, fs, expect }) => {
207+
await exec('npx @tailwindcss/upgrade')
208+
209+
expect(await fs.dumpFiles('./**/*.{html,php,txt}')).toMatchInlineSnapshot(`
210+
"
211+
--- notes/unrelated.txt ---
212+
order-[0] bg-[--my-red]
213+
214+
--- src/index.html ---
215+
<div class="order-0 bg-(--my-red)"></div>
216+
217+
--- templates/email.php ---
218+
<div class="order-[0] bg-[--my-red]"></div>
219+
"
220+
`)
221+
},
222+
)
223+
174224
test(
175225
`upgrades a v3 project with prefixes to v4`,
176226
{
@@ -2972,6 +3022,198 @@ test(
29723022
},
29733023
)
29743024

3025+
test(
3026+
'v4 ignores .env files during template migration',
3027+
{
3028+
fs: {
3029+
'package.json': json`
3030+
{
3031+
"dependencies": {
3032+
"tailwindcss": "^4",
3033+
"@tailwindcss/upgrade": "workspace:^"
3034+
}
3035+
}
3036+
`,
3037+
'src/app.css': css`@import 'tailwindcss';`,
3038+
'src/index.html': html`
3039+
<div class="order-[0]"></div>
3040+
`,
3041+
'src/.env': `TW_TEST_CLASS=order-[0]`,
3042+
'src/.env.production': `TW_TEST_CLASS=order-[0]`,
3043+
},
3044+
},
3045+
async ({ exec, fs, expect }) => {
3046+
await exec('npx @tailwindcss/upgrade')
3047+
3048+
expect(await fs.dumpFiles('./src/**/{*,.env,.env.*}')).toMatchInlineSnapshot(`
3049+
"
3050+
--- ./src/index.html ---
3051+
<div class="order-0"></div>
3052+
3053+
--- ./src/.env ---
3054+
TW_TEST_CLASS=order-[0]
3055+
3056+
--- ./src/.env.production ---
3057+
TW_TEST_CLASS=order-[0]
3058+
3059+
--- ./src/app.css ---
3060+
@import 'tailwindcss';
3061+
"
3062+
`)
3063+
},
3064+
)
3065+
3066+
test(
3067+
'v4 linked configs respect `content` and still ignore gitignored files',
3068+
{
3069+
fs: {
3070+
'package.json': json`
3071+
{
3072+
"dependencies": {
3073+
"tailwindcss": "^4",
3074+
"@tailwindcss/upgrade": "workspace:^"
3075+
}
3076+
}
3077+
`,
3078+
'tailwind.config.js': js`
3079+
/** @type {import('tailwindcss').Config} */
3080+
module.exports = {
3081+
content: ['./src/*.html', './src/*.less'],
3082+
}
3083+
`,
3084+
'src/app.css': css`
3085+
@import 'tailwindcss';
3086+
@config '../tailwind.config.js';
3087+
`,
3088+
3089+
// Ignore all .html files
3090+
'src/.gitignore': txt`
3091+
*.html
3092+
`,
3093+
3094+
// HTML files are in .gitignore, even though they are explicitly mentioned
3095+
// in the `content` array. Still ignore them
3096+
'src/do-not-migrate-me.html': html`
3097+
<div class="order-[0]"></div>
3098+
`,
3099+
3100+
// Should be picked up by auto-content detection
3101+
'templates/migrate-me.php': html`
3102+
<div class="order-[0]"></div>
3103+
`,
3104+
3105+
// Does not get picked up by auto content detection (because it's a less
3106+
// file), but was explicitly listed in the `content` array.
3107+
//
3108+
// A bit of a hacky way, I admit, but it allows us to differentiate
3109+
// between git ignored files, auto content detection and explicitly listed
3110+
// files.
3111+
'src/migrate-me.less': html`
3112+
<div class="order-[0]"></div>
3113+
`,
3114+
},
3115+
},
3116+
async ({ exec, fs, expect }) => {
3117+
await exec('npx @tailwindcss/upgrade')
3118+
3119+
expect(await fs.dumpFiles('./{src,templates}/**/*')).toMatchInlineSnapshot(`
3120+
"
3121+
--- ./src/app.css ---
3122+
@import 'tailwindcss';
3123+
@config '../tailwind.config.js';
3124+
3125+
--- ./src/do-not-migrate-me.html ---
3126+
<div class="order-[0]"></div>
3127+
3128+
--- ./src/migrate-me.less ---
3129+
<div class="order-0"></div>
3130+
3131+
--- ./templates/migrate-me.php ---
3132+
<div class="order-0"></div>
3133+
"
3134+
`)
3135+
},
3136+
)
3137+
3138+
test(
3139+
'interrupting template migration does not truncate files',
3140+
{
3141+
timeout: 180_000,
3142+
fs: {
3143+
'package.json': json`
3144+
{
3145+
"dependencies": {
3146+
"tailwindcss": "^4",
3147+
"@tailwindcss/upgrade": "workspace:^"
3148+
}
3149+
}
3150+
`,
3151+
'src/app.css': css` @import 'tailwindcss'; `,
3152+
'src/index.html': html`
3153+
<div class="order-[0]"></div>
3154+
`,
3155+
'hook.cjs': js`
3156+
let fs = require('node:fs/promises')
3157+
let path = require('node:path')
3158+
let originalWriteFile = fs.writeFile.bind(fs)
3159+
3160+
fs.writeFile = async (file, contents, ...rest) => {
3161+
// Mimic a bad write
3162+
await originalWriteFile(file, '') // As-if we truncated first
3163+
console.error('__TRUNCATED_TARGET__')
3164+
await new Promise((r) => setTimeout(r, 50)) // Wait 50ms to allow us to kill the process
3165+
await originalWriteFile(file, contents, ...rest) // Write the actual contents
3166+
}
3167+
`,
3168+
'src/keep.php': `
3169+
<?php
3170+
3171+
return [
3172+
'keep' => 'this file should never be truncated',
3173+
];
3174+
`,
3175+
},
3176+
},
3177+
async ({ spawn, fs, expect }) => {
3178+
let repeatedCandidates = Array.from(
3179+
{ length: 250 },
3180+
() => '<div class="order-[0]"></div>',
3181+
).join('\n')
3182+
3183+
for (let i = 0; i < 100; i++) {
3184+
await fs.write(
3185+
`src/templates/template-${i}.php`,
3186+
`<?php\n\n${repeatedCandidates}\n\nreturn ['template' => ${i}];\n`,
3187+
)
3188+
}
3189+
3190+
let originalKeepFile = await fs.read('src/keep.php')
3191+
let originalTemplate = await fs.read('src/templates/template-0.php')
3192+
3193+
let process = await spawn('npx @tailwindcss/upgrade --force', {
3194+
env: {
3195+
NODE_OPTIONS: '--require=./hook.cjs',
3196+
},
3197+
})
3198+
3199+
// We're only interested once we start migrating the templates
3200+
await process.onStderr((message) => message.includes('Migrating templates'))
3201+
3202+
// Wait for the trigger that we are mid-write
3203+
await process.onStderr((message) => message === '__TRUNCATED_TARGET__')
3204+
3205+
// Kill the process
3206+
await process.dispose()
3207+
3208+
expect(await fs.read('src/keep.php')).toBe(originalKeepFile)
3209+
expect(await fs.read('src/templates/template-0.php')).toBe(originalTemplate)
3210+
3211+
for (let [file, contents] of await fs.glob('src/**/*.{html,php,css}')) {
3212+
expect(contents.trim(), `${file} should not be empty after interruption`).not.toBe('')
3213+
}
3214+
},
3215+
)
3216+
29753217
test(
29763218
'upgrades can run in a pnpm workspace',
29773219
{

integrations/upgrade/js-config.test.ts

Lines changed: 100 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import path from 'node:path'
22
import { describe } from 'vitest'
3-
import { css, html, json, test, ts } from '../utils'
3+
import { css, html, json, test, ts, txt } from '../utils'
44

55
test(
66
`upgrade JS config files with flat theme values, darkMode, and content fields`,
@@ -322,6 +322,105 @@ test(
322322
},
323323
)
324324

325+
test(
326+
'skips gitignored template files even when they are explicitly referenced in `content`',
327+
{
328+
fs: {
329+
'package.json': json`
330+
{
331+
"dependencies": {
332+
"tailwindcss": "^3",
333+
"@tailwindcss/upgrade": "workspace:^"
334+
}
335+
}
336+
`,
337+
'.gitignore': txt`
338+
node_modules
339+
src/ignored
340+
`,
341+
'tailwind.config.ts': ts`
342+
import { type Config } from 'tailwindcss'
343+
344+
module.exports = {
345+
content: [
346+
'./src/migrate-me.html',
347+
'./src/ignored/do-not-migrate-me.html',
348+
'./node_modules/my-external-lib/template.html',
349+
],
350+
theme: {},
351+
plugins: [],
352+
} satisfies Config
353+
`,
354+
'src/input.css': css`
355+
@tailwind base;
356+
@tailwind components;
357+
@tailwind utilities;
358+
`,
359+
'src/migrate-me.html': html`
360+
<div class="order-[0]"></div>
361+
`,
362+
'src/ignored/do-not-migrate-me.html': html`
363+
<div class="order-[0]"></div>
364+
`,
365+
'node_modules/my-external-lib/template.html': html`
366+
<div
367+
class="order-[0]"
368+
></div>
369+
`,
370+
},
371+
},
372+
async ({ exec, fs, expect }) => {
373+
await exec('npx @tailwindcss/upgrade')
374+
375+
expect(
376+
await fs.dumpFiles(
377+
'{src/**/*.{css,html},node_modules/my-external-lib/template.html,.gitignore}',
378+
),
379+
).toMatchInlineSnapshot(`
380+
"
381+
--- .gitignore ---
382+
node_modules
383+
src/ignored
384+
385+
--- src/input.css ---
386+
@import 'tailwindcss';
387+
388+
@source './ignored/do-not-migrate-me.html';
389+
@source '../node_modules/my-external-lib/template.html';
390+
391+
/*
392+
The default border color has changed to \`currentcolor\` in Tailwind CSS v4,
393+
so we've added these compatibility styles to make sure everything still
394+
looks the same as it did with Tailwind CSS v3.
395+
396+
If we ever want to remove these styles, we need to add an explicit border
397+
color utility to any element that depends on these defaults.
398+
*/
399+
@layer base {
400+
*,
401+
::after,
402+
::before,
403+
::backdrop,
404+
::file-selector-button {
405+
border-color: var(--color-gray-200, currentcolor);
406+
}
407+
}
408+
409+
--- src/migrate-me.html ---
410+
<div class="order-0"></div>
411+
412+
--- node_modules/my-external-lib/template.html ---
413+
<div
414+
class="order-[0]"
415+
></div>
416+
417+
--- src/ignored/do-not-migrate-me.html ---
418+
<div class="order-[0]"></div>
419+
"
420+
`)
421+
},
422+
)
423+
325424
test(
326425
'upgrades JS config files with plugins',
327426
{

0 commit comments

Comments
 (0)