Skip to content

fix(oxlint): patch panics to allow programmatic usage#21899

Open
MatissJanis wants to merge 2 commits intooxc-project:mainfrom
MatissJanis:matissjanis/oxlint-reentrant-lint
Open

fix(oxlint): patch panics to allow programmatic usage#21899
MatissJanis wants to merge 2 commits intooxc-project:mainfrom
MatissJanis:matissjanis/oxlint-reentrant-lint

Conversation

@MatissJanis
Copy link
Copy Markdown

Discord conversation: https://discord.com/channels/1079625926024900739/1498648746433445988

Long story short: we are looking to move from eslint to oxlint in a large (>15m LOC) monorepo. Internally we use lage for fast runs of lint + caching. Eslint exposes a programmatic API that we can use to share the same instance of eslint across different node processes. Thus saving us time in startup. However, oxlint does not currently expose a programmatic API. The workaround: reach deep into oxlint built source to extract lint() API (from dist/bindings.js) and use that. Yes - it is very hacky. But it allows us to use oxlint + lage. Reproduction of this hacky solution can be found here: https://github.com/MatissJanis/oxlint-lage-repro

In order for the hacky solution to work: we need to fix a few instances of panic. Hence the PR.

Hope we can make this work!

Full disclosure: this was built using claude code (opus 4.7).

`oxlint::lint` is exposed via napi and can therefore be invoked more
than once in the same Node process (e.g. from a long-lived linter
daemon, a build-tool worker, or an LSP host). Today the second call
panics in one of three places:

1. `init_tracing` calls `.init()` on a `tracing_subscriber::registry()`,
   which internally `set_global_default(...).unwrap()`s. The second
   call returns `Err(SetGlobalDefaultError("a global default trace
   dispatcher has already been set"))` and panics.
2. `init_miette` calls `miette::set_hook(...).unwrap()`. `set_hook`
   returns `Err` if a hook is already installed, so it panics on the
   second call.
3. `LintCommand::init_rayon_thread_pool` calls
   `rayon::ThreadPoolBuilder::new()...build_global().unwrap()`. The
   second call returns
   `Err(ThreadPoolBuildError { kind: GlobalPoolAlreadyInitialized })`
   and panics.

All three are one-shot global inits that should silently no-op on
repeat invocation. Wrap the first two in a `OnceLock` and discard the
result; replace the `.unwrap()` on `build_global` with `let _ = ...`.

`oxfmt` already uses the same `OnceLock` + `try_init` pattern at
`apps/oxfmt/src/core/utils.rs`, so this brings oxlint in line.

Caveat for users: rayon's global pool keeps the thread count from the
first call. Subsequent calls with a different `--threads` value are
silently ignored. That matches rayon's documented semantics and is the
only sensible behaviour without tearing down and recreating the pool.

This change unblocks the daemon-style worker pattern at
https://github.com/MatissJanis/oxlint-lage-repro, which on the Datadog
web-ui codebase (~7,500 packages) cuts cold-cache lint runtime from
~20 minutes to ~10 minutes by amortising oxlint's ~1.6s startup across
many packages handled by the same lage worker thread.
Comment thread apps/oxlint/src/init.rs Outdated
Comment thread apps/oxlint/src/init.rs Outdated
Comment thread apps/oxlint/src/command/lint.rs Outdated
Address review feedback on oxc-project#21899. Now that the panic-prone init
calls live inside `OnceLock::get_or_init`, the closure runs at most
once per process, so the inner result is safe to `.unwrap()` instead
of swallowing with `let _ = ...`. Also wrap the rayon `build_global`
call in its own `OnceLock` so it follows the same pattern.
@MatissJanis MatissJanis marked this pull request as ready for review April 29, 2026 10:15
Copy link
Copy Markdown
Contributor

@camc314 camc314 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Just as a note, we are providing no guarentees that this won't break in the future.

@overlookmotel do you mind reviewing this as well

@camc314 camc314 self-assigned this Apr 29, 2026
@camc314 camc314 requested a review from overlookmotel April 29, 2026 17:10
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.

2 participants