Skip to content

Commit baa016a

Browse files
rudivphilipp-spiessRobinMalfait
authored
Add Input & Output check to CLI (#17311)
Throw an error if the input and output file for the CLI are identical. --------- Co-authored-by: Philipp Spiess <hello@philippspiess.com> Co-authored-by: Robin Malfait <malfait.robin@gmail.com>
1 parent 711b9cd commit baa016a

File tree

3 files changed

+83
-1
lines changed

3 files changed

+83
-1
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2424

2525
### Fixed
2626

27-
- Vite: Ensure that updates to an imported CSS file are properly propagated after updating templates ([#17347](https://github.com/tailwindlabs/tailwindcss/pull/17347))
2827
- Fix class extraction followed by `(` in Pug ([#17320](https://github.com/tailwindlabs/tailwindcss/pull/17320))
28+
- Vite: Ensure that updates to an imported CSS file are properly propagated after updating templates ([#17347](https://github.com/tailwindlabs/tailwindcss/pull/17347))
2929
- Pre process `Slim` templates embedded in Ruby files ([#17336](https://github.com/tailwindlabs/tailwindcss/pull/17336))
30+
- Error when input and output files resolve to the same file when using the CLI ([#17311](https://github.com/tailwindlabs/tailwindcss/pull/17311))
3031

3132
### [4.0.15] - 2025-03-20
3233

integrations/cli/index.test.ts

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1387,6 +1387,76 @@ test(
13871387
},
13881388
)
13891389

1390+
test(
1391+
'fails when input file does not exist',
1392+
{
1393+
fs: {
1394+
'package.json': json`
1395+
{
1396+
"dependencies": {
1397+
"tailwindcss": "workspace:^",
1398+
"@tailwindcss/cli": "workspace:^"
1399+
}
1400+
}
1401+
`,
1402+
},
1403+
},
1404+
async ({ exec, expect }) => {
1405+
await expect(exec('pnpm tailwindcss --input index.css --output dist/out.css')).rejects.toThrow(
1406+
/Specified input file.*does not exist./,
1407+
)
1408+
},
1409+
)
1410+
1411+
test(
1412+
'fails when input file and output file are the same',
1413+
{
1414+
fs: {
1415+
'package.json': json`
1416+
{
1417+
"dependencies": {
1418+
"tailwindcss": "workspace:^",
1419+
"@tailwindcss/cli": "workspace:^"
1420+
}
1421+
}
1422+
`,
1423+
'input.css': '',
1424+
},
1425+
},
1426+
async ({ exec, expect }) => {
1427+
await expect(exec('pnpm tailwindcss --input input.css --output input.css')).rejects.toThrow(
1428+
/Specified input file.*and output file.*are identical./,
1429+
)
1430+
await expect(
1431+
exec('pnpm tailwindcss --input input.css --output ./src/../input.css'),
1432+
).rejects.toThrow(/Specified input file.*and output file.*are identical./)
1433+
},
1434+
)
1435+
1436+
test(
1437+
'input and output flags can be the same if `-` is used',
1438+
{
1439+
fs: {
1440+
'package.json': json`
1441+
{
1442+
"dependencies": {
1443+
"tailwindcss": "workspace:^",
1444+
"@tailwindcss/cli": "workspace:^"
1445+
}
1446+
}
1447+
`,
1448+
'index.html': html`<div class="flex"></div>`,
1449+
},
1450+
},
1451+
async ({ exec, expect }) => {
1452+
expect(
1453+
await exec('pnpm tailwindcss --input - --output -', undefined, {
1454+
stdin: '@tailwind utilities;',
1455+
}),
1456+
).toContain(candidate`flex`)
1457+
},
1458+
)
1459+
13901460
function withBOM(text: string): string {
13911461
return '\uFEFF' + text
13921462
}

packages/@tailwindcss-cli/src/commands/build/index.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,17 @@ export async function handle(args: Result<ReturnType<typeof options>>) {
9595
}
9696
}
9797

98+
// Check if the input and output file paths are identical, otherwise return an
99+
// error to the user.
100+
if (args['--input'] === args['--output'] && args['--input'] !== '-') {
101+
eprintln(header())
102+
eprintln()
103+
eprintln(
104+
`Specified input file ${highlight(relative(args['--input']))} and output file ${highlight(relative(args['--output']))} are identical.`,
105+
)
106+
process.exit(1)
107+
}
108+
98109
let start = process.hrtime.bigint()
99110

100111
let input = args['--input']

0 commit comments

Comments
 (0)