Skip to content

@tailwindcss/postcss: compiler cache ignores changed input CSS when from's mtime is unchanged — stale output with any preprocessor (Sass) pipeline #20307

Closed

Description

@MaksymPlotnikov

Tailwind version: 4.3.2 (tailwindcss + @tailwindcss/postcss, latest at time of filing)
Node: 22.x · reproduces on macOS and Linux

Summary

@tailwindcss/postcss keeps a per-entry compiler cache keyed by opts.from. On each run it decides between the incremental and full rebuild paths by stat-ing the mtimes of only [...fullRebuildPaths, from] — i.e. the entry file on disk plus Tailwind's own resolved @import/@config/@plugin graph. The actual input CSS handed to the plugin is never consulted. When none of those mtimes changed, the plugin discards the fresh input root and re-emits the cached AST (root.removeAll(); root.append(cachedPostCssAst.clone().nodes)).

This assumption — that the input is a pure function of from's own mtime plus Tailwind-visible imports — breaks in any pipeline where the input is derived: most commonly Sass, where entry.scss inlines dozens of @used partials before PostCSS runs. Editing a partial re-runs Sass and produces different CSS for the same unchanged-on-disk from → Tailwind silently serves the previous compile. All changes are lost, not just Tailwind-relevant ones, because the whole root is replaced by the cached AST.

Minimal reproduction (no Sass needed — 15 lines)

// repro.mjs — npm i postcss [email protected] @tailwindcss/[email protected]
import postcss from 'postcss';
import tailwindPostcss from '@tailwindcss/postcss';
import { writeFileSync } from 'node:fs';

writeFileSync('entry.css', '/* on-disk stand-in; content irrelevant, mtime never changes */');
const processor = postcss([tailwindPostcss()]);
const compile = css => processor.process(css, { from: 'entry.css' }).then(r => r.css);

const first = await compile('@import "tailwindcss/utilities.css"; .first { @apply underline; }');
const second = await compile('@import "tailwindcss/utilities.css"; .second { @apply font-bold; }');

console.log('first compile contains .first  :', first.includes('.first'));   // true
console.log('second compile contains .second:', second.includes('.second')); // false  ❌
console.log('second === first (stale)      :', second === first);            // true   ❌

Actual output on pristine 4.3.2:

first compile contains .first  : true
second compile contains .second: false
second === first (stale)      : true

Real-world impact

Angular 21 dev server (@angular/build, esbuild) + Sass + .postcssrc.json with @tailwindcss/postcss: the global stylesheet styles.scss @uses a tree of partials containing @apply/theme(). Editing any partial triggers a correct Angular rebuild (dart-sass re-runs, fresh CSS goes into PostCSS), but the served bundle stays byte-identical until the developer touches styles.scss itself or restarts the dev server. Verified live: rebuild fires within 1s of the edit, output hash unchanged, new rule absent; touch styles.scss (whose mtime IS checked) flushes it through. The same applies to webpack sass-loader → postcss-loader chains and any other preprocessor-in-front-of-postcss setup.

Angular-side watching/invalidation was ruled out (sass loadedUrls → esbuild watchFiles include the partials; its stylesheet cache invalidates correctly — the fresh input demonstrably reaches the plugin).

Suggested fix

Include an input fingerprint in the cache-validity check: store (a hash of) the incoming root's CSS per cache entry and take the existing "full" rebuild path when it differs from the previous compile. We've been running exactly that as a 2-line local patch of dist/index.{js,mjs} — insert after the let isInitialBuild = context.compiler === null equivalent:

let inputCss = root.toString();
let inputChanged = context.previousInputCss !== undefined && context.previousInputCss !== inputCss;
context.previousInputCss = inputCss;
// ...
let rebuildStrategy = inputChanged ? 'full' : 'incremental';

Single-pass production builds are unaffected (the warm-cache path never triggers); the root.toString() cost is only paid on watch-mode recompiles of that entry.

Precedent: the 4.3.2 changelog fixed the analogous blind spot for the CLI watcher ("Ensure @tailwindcss/cli in --watch mode rebuilds when the input CSS file changes") — the PostCSS plugin needs the equivalent for content arriving via process() rather than from disk.

Activity

  1. added a commit that references this issue on Jul 6, 2026
    9b0e8af
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions