Skip to content

Add Input & Output check to CLI #17311

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
Mar 24, 2025
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
70 changes: 70 additions & 0 deletions integrations/cli/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
11 changes: 11 additions & 0 deletions packages/@tailwindcss-cli/src/commands/build/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Expand Down