Skip to content

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

Merged
camc314 merged 3 commits into
oxc-project:mainfrom
MatissJanis:matissjanis/oxlint-reentrant-lint
May 18, 2026
Merged

fix(oxlint): patch panics to allow programmatic usage#21899
camc314 merged 3 commits into
oxc-project:mainfrom
MatissJanis:matissjanis/oxlint-reentrant-lint

Conversation

@MatissJanis

Copy link
Copy Markdown
Contributor

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

@camc314 camc314 left a comment

Copy link
Copy Markdown
Contributor

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
@camc314 camc314 added the A-linter Area - Linter label May 7, 2026
…entrant-lint

# Conflicts:
#	apps/oxlint/src/command/lint.rs

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR makes oxlint’s global initialization paths idempotent so the NAPI lint() entry point can be invoked multiple times in a long-lived process.

Changes:

  • Wraps miette and tracing initialization in OnceLock.
  • Wraps Rayon global thread pool initialization in OnceLock.
  • Documents first-call-wins behavior for thread count selection.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
apps/oxlint/src/init.rs Makes miette/tracing initialization run only once per process.
apps/oxlint/src/command/lint.rs Makes Rayon global thread pool setup run only once per process.

Comment thread apps/oxlint/src/init.rs
Comment thread apps/oxlint/src/init.rs
Comment thread apps/oxlint/src/command/lint.rs
@camc314 camc314 merged commit 3f59e03 into oxc-project:main May 18, 2026
30 checks passed
camc314 pushed a commit that referenced this pull request May 18, 2026
# Oxlint
### 🚀 Features

- 1ae291e linter/no-underscore-dangle: Add `allowInUsingDeclarations`
option (#22483) (吴杨帆)
- 0440b0f linter/eslint: Implement `id-match` rule (#22379) (Vladislav
Sayapin)
- 65bf119 linter: Implement react no-object-type-as-default-prop
(#22481) (uhyo)
- 2a6ddce linter/eslint: Implement `no-implied-eval` rule (#22391)
(Vladislav Sayapin)
- d3a3c1d linter: Auto detect agents from CLI and transition to the
agent output format (#22068) (Jovi De Croock)
- 625758a linter/vitest: Implement padding-around-after-all-blocks rule
(#21788) (kapobajza)
- 37680b0 linter: Implement react no-unstable-nested-components (#22248)
(Jovi De Croock)
- d8d9c74 linter: Implement import/newline-after-import rule (#19142)
(Ryuya Yanagi)

### 🐛 Bug Fixes

- 3f59e03 linter: Only call rayon/miette/tracing inits once (#21899)
(Matiss Janis Aboltins)
- 602dfd6 linter/promise/no-return-wrap: Detect Promise calls in all
branches (#22474) (zennnnnnn11)
- e182aee linter: Allow dialogs and popovers for no_autofocus (#22289)
(mehm8128)
- 7ffb710 linter/jest/vitest: Jest/no-standalone-expect ignores
additionalTestBlockFunctions option for jest/vitest hooks (#22477)
(kapobajza)
- c6f2d3f linter: Add more expression support for iframe-has-title
(#22460) (mehm8128)
- 5747ff1 linter: Avoid enabling jest with vitest plugin (#22499)
(camc314)
- 863984f linter/no-find-dom-node: Run on all files (#22479) (bab)

### ⚡ Performance

- 2afef79 linter: Optimize `no-loop-func` (#22491) (camchenry)
- 4c9ca72 oxlint: Align walker thread count with rayon pool (#22494)
(Boshen)

### 📚 Documentation

- f7967c7 linter/id-match: Clarify `onlyDeclarations` config docs
(#22523) (camc314)
- 1e0c97f linter: Fix closing code block in documentation for
`padding-around-after-all-blocks` rule. (#22513) (connorshea)
- a9049fd linter: Exclude directly provide autoFocus to dialog pattern
(#22510) (mehm8128)
# Oxfmt
### 🐛 Bug Fixes

- 8ee946f formatter/sort_imports: Use label to classify lines (#22512)
(leaysgur)
- 8c1da44 formatter: Normalize destructuring keys in DCR (#22478)
(camc314)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-linter Area - Linter

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants