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>
This commit is contained in:
Rudi Visser 2025-03-24 13:00:36 +02:00 • committed by GitHub
parent 711b9cd0db
commit baa016a1c9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 83 additions and 1 deletions

View file

@ -24,9 +24,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed
- Vite: Ensure that updates to an imported CSS file are properly propagated after updating templates ([#17347](https://github.com/tailwindlabs/tailwindcss/pull/17347))
- Fix class extraction followed by `(` in Pug ([#17320](https://github.com/tailwindlabs/tailwindcss/pull/17320))
- Vite: Ensure that updates to an imported CSS file are properly propagated after updating templates ([#17347](https://github.com/tailwindlabs/tailwindcss/pull/17347))
- Pre process `Slim` templates embedded in Ruby files ([#17336](https://github.com/tailwindlabs/tailwindcss/pull/17336))
- Error when input and output files resolve to the same file when using the CLI ([#17311](https://github.com/tailwindlabs/tailwindcss/pull/17311))
### [4.0.15] - 2025-03-20

View file

@ -1387,6 +1387,76 @@ test(
},
)
test(
'fails when input file does not exist',
{
fs: {
'package.json': json`
{
"dependencies": {
"tailwindcss": "workspace:^",
"@tailwindcss/cli": "workspace:^"
}
}
`,
},
},
async ({ exec, expect }) => {
await expect(exec('pnpm tailwindcss --input index.css --output dist/out.css')).rejects.toThrow(
/Specified input file.*does not exist./,
)
},
)
test(
'fails when input file and output file are the same',
{
fs: {
'package.json': json`
{
"dependencies": {
"tailwindcss": "workspace:^",
"@tailwindcss/cli": "workspace:^"
}
}
`,
'input.css': '',
},
},
async ({ exec, expect }) => {
await expect(exec('pnpm tailwindcss --input input.css --output input.css')).rejects.toThrow(
/Specified input file.*and output file.*are identical./,
)
await expect(
exec('pnpm tailwindcss --input input.css --output ./src/../input.css'),
).rejects.toThrow(/Specified input file.*and output file.*are identical./)
},
)
test(
'input and output flags can be the same if `-` is used',
{
fs: {
'package.json': json`
{
"dependencies": {
"tailwindcss": "workspace:^",
"@tailwindcss/cli": "workspace:^"
}
}
`,
'index.html': html`<div class="flex"></div>`,
},
},
async ({ exec, expect }) => {
expect(
await exec('pnpm tailwindcss --input - --output -', undefined, {
stdin: '@tailwind utilities;',
}),
).toContain(candidate`flex`)
},
)
function withBOM(text: string): string {
return '\uFEFF' + text
}

View file

@ -95,6 +95,17 @@ 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'] !== '-') {
eprintln(header())
eprintln()
eprintln(
`Specified input file ${highlight(relative(args['--input']))} and output file ${highlight(relative(args['--output']))} are identical.`,
)
process.exit(1)
}
let start = process.hrtime.bigint()
let input = args['--input']