Repository navigation
Throw an error instead of crashing in Scanner#scanFiles and Scanner#getCandidatesWithPositions - #20759
Open
RobinMalfait wants to merge 3 commits into
Open
Throw an error instead of crashing in Scanner#scanFiles and Scanner#getCandidatesWithPositions#20759RobinMalfait wants to merge 3 commits into
Scanner#scanFiles and Scanner#getCandidatesWithPositions#20759RobinMalfait wants to merge 3 commits into
Conversation
`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.
`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.
Scanner#getCandidatesWithPositionsScanner#scanFiles and Scanner#getCandidatesWithPositions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Two methods of
Scannerfrom@tailwindcss/oxidecould panic on invalid input. The panic crossed the NAPI boundary and aborted the whole Node.js process.getCandidatesWithPositionsusedunwrap()andexpect()to get the content of its input:It crashed when:
contentnorfile.file, and the file cannot be read (it does not exist, or it is not valid UTF-8).scanFilesconverted each entry with aFromimplementation that ended inunreachable!(), so it crashed on an entry with neithercontentnorfile.A
try/catcharound the call did not help:Our own callers (
@tailwindcss/upgradeand@tailwindcss/cli) always pass valid input, so they are not affected. But@tailwindcss/oxideis 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>(onlygetCandidatesWithPositions, becausescanFilesalready skips files that it cannot read)The TypeScript signatures do not change. Valid input gives the same result as before.
Test plan
packages/@tailwindcss-upgrade/src/codemods/template/candidates.test.ts, next to the existing test ofgetCandidatesWithPositions: input with onlyfile, an unreadablefile, neither field, and ascanFilesentry with neither field. Before the fix, the test worker died withWorker exited unexpectedly. Now all tests pass.cargo testvitest run --project @tailwindcss/upgrade