Repository navigation
Build every configured input CSS, not just the first - #137
Conversation
runBuild() takes a single input and the command passed it $this->inputPaths[0], so configuring several input_css files built only the first one. The reported workaround was to run tailwind:build once per file. Tailwind takes one -i/-o pair per run, so the command now starts one process per configured input and waits on all of them. Starting them together is also what makes --watch cover every file. An explicit input_css argument still builds that one file only.
bocharsky-bw
left a comment
There was a problem hiding this comment.
Thanks a lot for tackling this, the approach (one process per input) makes sense, and the non-watch build works as expected.
There is one blocking issue with --watch though, see the inline comment on the wait() loop. A couple of smaller things are in the other comments.
Also, not introduced by this PR, but I see that now more likely to bite: getInternalOutputCssPath() uses only the basename, so assets/app.css and assets/admin/app.css both write to var/tailwind/app.built.css, now concurrently. Same if a path is listed twice. It probably would be great to fix with a follow-up PR (not here), what do you think?
| $failed = false; | ||
|
|
||
| foreach ($processes as $process) { | ||
| $process->wait(static function ($type, $buffer) use ($io) { |
There was a problem hiding this comment.
The main problem is that in --watch mode the first process never exits, so wait() on it blocks forever, and the other processes are never read from. That has a few consequences:
- output (including compile errors) of inputs 2..N is never shown
- Symfony Process only reads a process’s pipes when one of its methods is called, so their stdout/stderr pipes eventually fill up (Tailwind writes
Done in xx mson every rebuild) and those watchers block on write() and silently stop rebuilding - a failure in inputs 2..N is not detected
I think we should try polling all processes in a loop instead - that should work for both watch and non-watch modes
There was a problem hiding this comment.
Good catch, thanks! I could reproduce it: with two inputs in --watch, an error in the second file never showed up. The command now polls every process in a loop (non-blocking reads, plus checkTimeout() so the non-watch timeout still applies), so all outputs are drained and a failure in any input is reported. Checked by hand with --watch on two files: the error in the second one is shown, and fixing it rebuilds it.
| $processes = []; | ||
|
|
||
| foreach ($inputFiles as $file) { | ||
| $processes[] = $this->tailwindBuilder->runBuild( |
There was a problem hiding this comment.
With several inputs, the "Executing Tailwind" message is printed N times, and it’s hard to tell which output/error belongs to which file. Could we mention the input file, e.g. Executing Tailwind for "assets/styles/admin.css"?
There was a problem hiding this comment.
Done, the note now reads Executing Tailwind for "assets/styles/admin.css" (path relative to the project). Since the outputs of several watchers interleave, I also print the input path before a chunk of output when it comes from another file than the previous one (only with several inputs). Happy to drop that part if you find it noisy.
| $this->assertFileExists($builtCss); | ||
| } | ||
|
|
||
| public function testBuildEveryConfiguredInput(): void |
There was a problem hiding this comment.
the PR says that a failing input no longer hides the others, but there is no test for it. Could you add one where the second input is invalid and assert the command fails?
There was a problem hiding this comment.
Added testBuildFailsWhenAnInputIsInvalid, with the invalid file both last and first: the command fails, the Tailwind error is displayed, and the valid file is still built.
| 'tailwind_config' => [ | ||
| 'input_css' => [ | ||
| self::FIXTURES_DIR.'/assets/styles/v4.css', | ||
| self::FIXTURES_DIR.'/assets/styles/second.css', |
There was a problem hiding this comment.
IIRC this second.css is v3 syntax. If so, probably better create a new fixture for v4 and use both for v4 as you're running it on v4.0.7
There was a problem hiding this comment.
Right, that was v3 syntax. Added a v4-second.css fixture and used it here.
|
Thanks a lot for the thorough review! Everything is addressed in the new commit, see the replies in each thread. About the basename collision: agreed, two watchers writing the same |
bocharsky-bw
left a comment
There was a problem hiding this comment.
Thanks, this looks great the polling loop reads nicely.
One thing before merging: test_windows / PHP 8.5, highest deps fails. Seems v4.built.css was not created, so the valid input didn’t build. The other Windows jobs pass, so it might be a race between the two Tailwind processes running at the same time on Windows. Could you take a look, please?
|
|
||
| foreach ($processes as $i => $process) { | ||
| $process->checkTimeout(); | ||
| $running = $process->isRunning() || $running; |
There was a problem hiding this comment.
It would be cheaper to check in reverse order:
| $running = $process->isRunning() || $running; | |
| $running = $running || $process->isRunning(); |
Check bool var would be faster than a method call. But's pretty nit, feel free to ignore
There was a problem hiding this comment.
Done, thanks! (Safe to skip the call: reading the incremental output refreshes the status of each process anyway.)
| { | ||
| $projectRootDir = realpath($this->projectRootDir) ?: $this->projectRootDir; | ||
|
|
||
| return str_starts_with($inputPath, $projectRootDir.'/') ? substr($inputPath, \strlen($projectRootDir) + 1) : $inputPath; |
There was a problem hiding this comment.
I see we check for / separator here - I suppose on Windows it may be a backslash \ that will cause problems in this logic. I think we can leverage symfony/filesystem here and use Path::makeRelative() - that's already a dependency
There was a problem hiding this comment.
Good point, done with Path::isBasePath() and Path::makeRelative(), and a test that uses a realpath() (so backslashes on the Windows jobs). One thing: symfony/filesystem was only in require-dev, so I moved it to require. It was already installed through symfony/asset-mapper, but now the bundle uses it directly.
…ir relative path with Path
|
Thanks for checking the Windows jobs! I couldn't reproduce it here (no Windows box at hand), and I couldn't find a known Tailwind issue about two instances starting at the same time. In that job the exit code and the error message were as expected, only Rather than guessing, I removed the concurrency where it isn't needed: without |
|
Good news: This time it's It looks like the Windows flakiness that the scheduled runs on Could you re-run the failed job, please? If you think it's worth it, I can also send a separate PR that retries the removal in |
bocharsky-bw
left a comment
There was a problem hiding this comment.
Indeed, failed tests are random. I will take a look.
Thanks for working on this feature!
Fixes #78, fixes #46
runBuild()takes a single input, and the command passed it$this->inputPaths[0], so configuring severalinput_cssfiles built only the first one. That is why @kniziol's workaround was to runtailwind:buildonce per file.You called it a problem upstream, and the binary part is true: Tailwind takes one
-i/-opair per run. The consequence is not that nothing can be done though, only that it needs one run per input, which is what the command now does: a plain build runs one process per configured input, one after the other. sass-bundle does not have this problem only because Dart Sass acceptsinput:outputpairs in a single pass.Two things fall out of it:
--watchnow covers every input: the watchers are started together, and the command polls all of them so that the output (and the errors) of every input is shown, where only the first one was watched before. With several inputs, each chunk of output is prefixed with the file it comes from.Passing
input_cssexplicitly still builds that one file only.The new tests fail on
1.x(v4-second.built.cssmissing, the error of an invalid input not reported). Suite is green at 55 tests, and php-cs-fixer is clean.