Skip to content

Throw an error instead of crashing in Scanner#scanFiles and Scanner#getCandidatesWithPositions - #20759

Open
RobinMalfait wants to merge 3 commits into
mainfrom
robin/open-pr-errors-instead-of-panics-in-getcandidateswithpositions
Open

RobinMalfait wants to merge 3 commits into
mainfrom
robin/open-pr-errors-instead-of-panics-in-getcandidateswithpositions

Conversation

@RobinMalfait

@RobinMalfait RobinMalfait commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Two methods of Scanner from @tailwindcss/oxide could panic on invalid input. The panic crossed the NAPI boundary and aborted the whole Node.js process.

getCandidatesWithPositions used unwrap() and expect() to get the content of its input:

let content = input.content.unwrap_or_else(|| {
  std::fs::read_to_string(input.file.unwrap()).expect("Failed to read file")
});

It crashed when:

  • The input has neither content nor file.
  • The input has only file, and the file cannot be read (it does not exist, or it is not valid UTF-8).

scanFiles converted each entry with a From implementation that ended in unreachable!(), so it crashed on an entry with neither content nor file.

A try/catch around the call did not help:

const { Scanner } = require('@tailwindcss/oxide')

try {
  new Scanner({}).getCandidatesWithPositions({ extension: 'html' })
} catch (e) {
  // never reached
}
called `Option::unwrap()` on a `None` value
fatal runtime error: failed to initiate panic, error 5, aborting

Our own callers (@tailwindcss/upgrade and @tailwindcss/cli) always pass valid input, so they are not affected. But @tailwindcss/oxide is public, and other tools call these methods.

Both methods now return a napi::Result, so these cases throw a regular JavaScript error instead:

  • Either \content` or `file` must be provided`
  • Failed to read file `<path>`: <reason> (only getCandidatesWithPositions, because scanFiles already skips files that it cannot read)

The TypeScript signatures do not change. Valid input gives the same result as before.

Test plan

  • Added tests in packages/@tailwindcss-upgrade/src/codemods/template/candidates.test.ts, next to the existing test of getCandidatesWithPositions: input with only file, an unreadable file, neither field, and a scanFiles entry with neither field. Before the fix, the test worker died with Worker exited unexpectedly. Now all tests pass.
  • cargo test
  • vitest run --project @tailwindcss/upgrade

`getCandidatesWithPositions` used `unwrap()` and `expect()` to get the
content. When a caller passed neither `content` nor `file`, or a `file`
that could not be read, the panic crossed the NAPI boundary and aborted
the Node.js process. A `try/catch` around the call could not catch it.

The method now returns a `napi::Result`, so these cases throw a regular
JavaScript error with a clear message.
@RobinMalfait
RobinMalfait requested a review from a team as a code owner November 25, 2026 12:28
@RobinMalfait RobinMalfait self-assigned this Nov 25, 2026
`scanFiles` converted each entry with a `From` implementation that ended
in `unreachable!()`. An entry with neither `content` nor `file` hit it,
and the panic aborted the Node.js process.

The conversion is now a `TryFrom` that returns an error for that entry,
and `scanFiles` returns a `napi::Result`, so the call throws a regular
JavaScript error instead. `getCandidatesWithPositions` uses the same
error message.
@RobinMalfait RobinMalfait changed the title Throw an error instead of crashing in Scanner#getCandidatesWithPositions Throw an error instead of crashing in Scanner#scanFiles and Scanner#getCandidatesWithPositions Nov 25, 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