From 4b02622b4d15cee016a36eb725763d4af3b60556 Mon Sep 17 00:00:00 2001 From: Rudi Visser Date: Thu, 20 Mar 2025 21:56:21 +0200 Subject: [PATCH 1/7] Add Input & Output check to CLI Throw an error if the input and output file for the CLI are identical. --- packages/@tailwindcss-cli/src/commands/build/index.ts | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/packages/@tailwindcss-cli/src/commands/build/index.ts b/packages/@tailwindcss-cli/src/commands/build/index.ts index 6991e7bf0f16..2b7091e1cf32 100644 --- a/packages/@tailwindcss-cli/src/commands/build/index.ts +++ b/packages/@tailwindcss-cli/src/commands/build/index.ts @@ -95,6 +95,15 @@ export async function handle(args: Result>) { } } + // Check if the input and output file paths are identical, otherwise return an + // error to the user. + if (args['--input'] === args['--output']) { + 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'] From b5972f825adf6f4a510d0b4cc257ca4ba47da5aa Mon Sep 17 00:00:00 2001 From: Rudi Visser Date: Fri, 21 Mar 2025 19:45:20 +0200 Subject: [PATCH 2/7] Add tests for input file does not exist (missing) and input/output files identical (along with ensuring path resolution) --- integrations/cli/index.test.ts | 47 ++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/integrations/cli/index.test.ts b/integrations/cli/index.test.ts index 1e1fca38b324..ea180348fe69 100644 --- a/integrations/cli/index.test.ts +++ b/integrations/cli/index.test.ts @@ -1387,6 +1387,53 @@ 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./, + ) + }, +) + function withBOM(text: string): string { return '\uFEFF' + text } From 11f358b8a4b9c52d851a57c3c0b14ce09747e67e Mon Sep 17 00:00:00 2001 From: Philipp Spiess Date: Mon, 24 Mar 2025 11:28:56 +0100 Subject: [PATCH 3/7] Update changelog --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 63265693d249..ef07aacb5a5e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,6 +22,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Add support for literal values in `--value('…')` and `--modifier('…')` ([#17304](https://github.com/tailwindlabs/tailwindcss/pull/17304)) - Add suggestions when `--spacing(--value(integer, number))` is used ([#17308](https://github.com/tailwindlabs/tailwindcss/pull/17308)) +### Fixed + +- 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 ### Fixed From 0a774bc50cb176a8c86e3c4266294b237db0665b Mon Sep 17 00:00:00 2001 From: Robin Malfait Date: Mon, 24 Mar 2025 11:38:41 +0100 Subject: [PATCH 4/7] run prettier --- integrations/cli/index.test.ts | 7 +++---- packages/@tailwindcss-cli/src/commands/build/index.ts | 4 +++- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/integrations/cli/index.test.ts b/integrations/cli/index.test.ts index ea180348fe69..6477d9522642 100644 --- a/integrations/cli/index.test.ts +++ b/integrations/cli/index.test.ts @@ -1408,7 +1408,6 @@ test( }, ) - test( 'fails when input file and output file are the same', { @@ -1428,9 +1427,9 @@ test( 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./, - ) + await expect( + exec('pnpm tailwindcss --input input.css --output ./src/../input.css'), + ).rejects.toThrow(/Specified input file.*and output file.*are identical./) }, ) diff --git a/packages/@tailwindcss-cli/src/commands/build/index.ts b/packages/@tailwindcss-cli/src/commands/build/index.ts index 2b7091e1cf32..cd4c82ff899d 100644 --- a/packages/@tailwindcss-cli/src/commands/build/index.ts +++ b/packages/@tailwindcss-cli/src/commands/build/index.ts @@ -100,7 +100,9 @@ export async function handle(args: Result>) { if (args['--input'] === args['--output']) { eprintln(header()) eprintln() - eprintln(`Specified input file ${highlight(relative(args['--input']))} and output file ${highlight(relative(args['--output']))} are identical.`) + eprintln( + `Specified input file ${highlight(relative(args['--input']))} and output file ${highlight(relative(args['--output']))} are identical.`, + ) process.exit(1) } From 4a64f9c35d9f320e12debcf3e547b98e75a393ad Mon Sep 17 00:00:00 2001 From: Robin Malfait Date: Mon, 24 Mar 2025 11:43:10 +0100 Subject: [PATCH 5/7] add failing test for `--input - --output -` --- integrations/cli/index.test.ts | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/integrations/cli/index.test.ts b/integrations/cli/index.test.ts index 6477d9522642..487c88e40b76 100644 --- a/integrations/cli/index.test.ts +++ b/integrations/cli/index.test.ts @@ -1433,6 +1433,30 @@ test( }, ) +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`
`, + }, + }, + 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 } From 14c01317cb215914559c917969755acc11cefaaf Mon Sep 17 00:00:00 2001 From: Robin Malfait Date: Mon, 24 Mar 2025 11:43:21 +0100 Subject: [PATCH 6/7] ensure `--input` and `--output` don't use `-` --- packages/@tailwindcss-cli/src/commands/build/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/@tailwindcss-cli/src/commands/build/index.ts b/packages/@tailwindcss-cli/src/commands/build/index.ts index cd4c82ff899d..4e9c99e23459 100644 --- a/packages/@tailwindcss-cli/src/commands/build/index.ts +++ b/packages/@tailwindcss-cli/src/commands/build/index.ts @@ -97,7 +97,7 @@ export async function handle(args: Result>) { // Check if the input and output file paths are identical, otherwise return an // error to the user. - if (args['--input'] === args['--output']) { + if (args['--input'] === args['--output'] && args['--input'] !== '-') { eprintln(header()) eprintln() eprintln( From 668615accd089a307bd19feb5cfebf3448d2e63f Mon Sep 17 00:00:00 2001 From: Robin Malfait Date: Mon, 24 Mar 2025 12:00:15 +0100 Subject: [PATCH 7/7] Update CHANGELOG.md --- CHANGELOG.md | 1 - 1 file changed, 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 87cf17e4b243..377ef036230a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -24,7 +24,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed - - 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))