Skip to content

Throw an error instead of crashing on an invalid @source glob - #20773

Open
RobinMalfait wants to merge 3 commits into
mainfrom
robin/open-pr-error-instead-of-a-crash-on-invalid-source-not-globs
Open

RobinMalfait wants to merge 3 commits into
mainfrom
robin/open-pr-error-instead-of-a-crash-on-invalid-source-not-globs

Conversation

@RobinMalfait

Copy link
Copy Markdown
Contributor

Summary

An @source pattern that is not a valid glob crashed the whole Node.js process:

@import "tailwindcss";
@source not "./src/**/*.{js";
thread '<unnamed>' panicked at crates/oxide/src/scanner/mod.rs:843:53:
called `Result::unwrap()` on an `Err` value: Glob { glob: Some("/**/*.{js"), err: "unclosed alternate group; missing '}' (maybe escape '{' with '[{]'?)" }
fatal runtime error: failed to initiate panic, error 5, aborting

The scanner turns each @source into a gitignore rule, and create_walker compiled these rules with GitignoreBuilder::add_line(…).unwrap(). When the brace expansion of a pattern fails, the scanner keeps the raw pattern, so an invalid glob reaches that unwrap(). The panic crossed the NAPI boundary, and the build tool died without a useful error.

This happens for @source and @source not, with:

  • an unclosed {, e.g. ./src/**/*.{js
  • a } without a {, e.g. ./src/a}
  • an invalid range, e.g. ./src/[z-a].html
  • a trailing \, e.g. ./src/a\

Now create_walker returns the error, and the napi constructor of Scanner throws it as a regular JavaScript error. The build fails with a message that names the pattern:

The `@source` pattern `/path/to/project/src/**/*.{js` is not a valid glob: unclosed alternate group; missing '}' (maybe escape '{' with '[{]'?)

The error names the pattern of the @source, resolved against its base. It does not name the gitignore rule, which can differ (e.g. @source "./src/**/*.{js" becomes the rule !*.{js). The CSS file of the @source is not known in the scanner, so the message does not include it.

Only patterns that crashed before throw an error now. Patterns that the gitignore rules accept keep their behavior. For example, @source "./src/**/a}.js" still scans a file named a}.js, because only the !*.js rule reaches the gitignore builder.

In Rust, Scanner::try_new returns the error. Scanner::new keeps its signature and panics with the same message, for the existing Rust callers (the tests).

This does not overlap with #20759, which changes scanFiles and getCandidatesWithPositions. This PR only changes the constructor.

Test plan

  • Added it_should_error_on_invalid_source_globs to crates/oxide/tests/scanner.rs. It checks the error for each case above, for negated and positive patterns. Without the fix, each case panics at crates/oxide/src/scanner/mod.rs:843.
  • Added a test to packages/@tailwindcss-postcss/src/index.test.ts with @source not "./src/**/*.{js": the build now rejects with the error. Without the fix, the test worker died with Worker exited unexpectedly.
  • cargo test --workspace
  • pnpm run build, pnpm run lint and vitest run (6128 tests pass)

An `@source` pattern that is not a valid glob, like `@source not
"./src/**/*.{js"` with an unclosed `{`, reached
`GitignoreBuilder::add_line(…).unwrap()` in `create_walker`. The panic
aborted the whole Node process.

`create_walker` now returns the error, and the new `Scanner::try_new`
passes it on. The napi constructor uses `try_new`, so `new Scanner(…)`
throws a JS error that names the pattern:

    The `@source` pattern `/path/to/project/src/**/*.{js` is not a valid
    glob: unclosed alternate group; missing '}' (maybe escape '{' with
    '[{]'?)

Each gitignore rule keeps the pattern of the source it comes from, so
the error names the `@source` pattern and not the rule (e.g. `!*.{js`).

Only patterns that crashed before get an error. Patterns that the
gitignore rules accept keep working as before.
@RobinMalfait
RobinMalfait requested a review from a team as a code owner November 27, 2026 11:22
@RobinMalfait RobinMalfait self-assigned this Nov 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant