Skip to content

bun lint: hold back vitest/no-importing-vitest-globals fixes that change what the file does - #48780

Open
robobun wants to merge 3 commits into
mainfrom
robobun/e6c339b8/vitest-globals-require-fix-local-names
Open

robobun wants to merge 3 commits into
mainfrom
robobun/e6c339b8/vitest-globals-require-fix-local-names

Conversation

@robobun

@robobun robobun commented Nov 13, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

  • bun lint --fix with vitest/no-importing-vitest-globals deletes const { vi: spy, test, expect } = require("vitest"). The file still uses spy: ReferenceError: spy is not defined.
  • The fix is oxlint's plain .fix (src/lint/jest/rules/vitest_no_importing_vitest_globals.rs:77, :129): it goes by the key or the local name. A probe found it.

Fix

  • Both sites use Report::fix_where_safe: --fix applies the fix only where the same-named global is what each removed name was, --fix-dangerously the rest. Reports stay.
  • Held back: renames, @effect/vitest names, a TypeScript file that would become a script. Each is wrong for every runner.
  • New: Fixer::remove_statement refuses a removal that loses a comment, joins two statements or makes a directive.
  • Verified: test/cli/lint/no-importing-vitest-globals.test.ts (runs bun test on 91 files around each flag, and 49 of 92 tests fail on main), conformance, rule-docs. Self-reviewed: 116 concerns raised, 95 addressed.

Background

Downsides

  • --fix skips 853 of 5,573 fixes in 16 code bases (715 for @effect/vitest) and 2 of 41 recorded oxlint cases.
  • Release .text +10,240 bytes, and 3 more instructions per file that imports vitest.
  • bun test still breaks where it differs from Vitest: no global assert, and without globals = "vitest" the import selects the file's globals.
Notes

Origin and reach

Reach of each guard (5,573 reports in 16 code bases)

Guard Reports
a name from @effect/vitest 715 held back
a TypeScript file that would become a script 79 held back
a directive comment above, // @ts-ignore 58 without a fix
a rename 1 held back
every other guard 0

What --fix leaves to --fix-dangerously

Statement Why
{ vi: spy }, { vi: { fn } }, { [key]: spy }, { vi = fallback }, import { inject as vi }, import { "vi" as spy } the name is not the global of that name, or the fix drops it
a name from @effect/vitest the package declares some names itself (beforeAll, beforeEach and it in 4.1.4): its it has it.effect()
an exported require() another module imports the names
a require() that is a let or a var, or inside a function or a namespace a let can be assigned, and in a function the name can be an outer variable's
a require() declaration with a type annotation or type arguments the fix writes it back without them
the last import of a TypeScript file whose top level has more than expression statements, or an await TypeScript can then take the file for a script. That depends on tsconfig.json, which the rule does not read. A file with import.meta or import a = require("a") is a module in every case. An export {} does not count: another rule can remove it in the same pass

What no flag fixes: a statement with a comment that the fix would lose, a statement below // @ts-ignore or // prettier-ignore, a statement whose removal joins the next line to the one before (also after a++, which Bun and esbuild join to a (), a statement above a string statement (it could become a directive), a name that the file exports with export { name }, an import next to a default import, a require() that is the body of an if, an exported require() with no name that stays. oxlint's text is wrong for each of these.

Not in this PR

  • The holds that are only true for bun test. They wait for the answer to the question above, and they need one shared check for the three rules that add or remove this import.
    • assert and chai are globals in Vitest. bun test has neither, with any setting (docs/test/configuration.mdx:120). 67 of the 5,573 reports import assert.
    • Without globals = "vitest", the import selects the file's globals (docs/test/configuration.mdx:107). A file that loses it has no suite, vitest, assertType or onTestFailed. The check needs [test].globals.
    • Bun runs a file as CommonJS or as an ES module by its import, export and require (src/js_parser/parse/parse_entry.rs:1939). A file that loses its last import or its only require() can change. The first commit held back a last import in a file that mentions require, module or exports. A review found that check wrong in both directions, so it is gone.
  • Both mirror rules on. With the style category, each run of --fix undoes the run before. main and oxlint do the same.
  • The guarantee is per rule. The fixes of two rules in one pass can still combine. Only export {} is handled.
  • Sibling fixers that still delete a declaration in use: jest/prefer-importing-jest-globals (src/lint/jest/rules/jest_prefer_importing_jest_globals.rs:93), vitest/consistent-vitest-vi (vitest_consistent_vitest_vi.rs:53, :73), react/destructuring-assignment (react_destructuring_assignment.rs:338).
  • Ctx::risky_fix (src/lint/jest/jest.rs:53) keeps a plain fix for six rules whose fix can fail a test. That is a separate decision. This rule's fix leaves a name undeclared or bound to another value.
  • Other rules that remove a whole statement do not call Fixer::remove_statement yet. Three can take it as is: no_useless_empty_export.rs:51, unicorn_require_module_specifiers.rs:74, unicorn_prefer_class_fields.rs:92. Each changes what another rule fixes, so each needs its own test rows.
  • A doc comment above a removed statement stays, above the next statement. A check for it would also refuse the fix below every file header in /** */.
  • A JavaScript file that TypeScript checks (checkJs, // @ts-check) can become a script as well. The check is for TypeScript files.
  • A .mts or .cts file stays held back like a .ts file. With moduleDetection: "legacy" it is a script without an import (tsc 5.0.4 to 6.0.2: TS2451 for two such files).
  • TypeScript before 5.0 reports TS1208 for every script under isolatedModules. The check does not hold a file back for that: 4.9.5 reports it, 5.0.4 and later do not.
  • After the fix, no-undef reports assertType, vitest, onTestFailed and onTestFinished in a JavaScript file. It does not know these globals. main does the same.
  • Bun's parser takes a++ and a ( on a later line for a call (src/js_parser/parse/parse_suffix.rs:509). Node does not.
  • Two removable statements in a row before a line that starts with (, with an oxlint config: the second one waits for the next run.

Measurements (linux x64, main 9ea3c69 against this diff on that commit)

  • Conformance: the rule's 41 recorded cases pass. 2 have their fix held back, both for @effect/vitest. bun lint --run-eslint-tests --suite=oxlint prints the text of expected-oxlint.txt, which now has "434 cases have a fix that --fix holds back" (main: 432). The upstream and more suites have no new failure.
  • Real code, lint only: 16 repositories, 7,101 files that mention vitest, no config, the rule set with --rule. Both builds make the same 5,573 reports. 4,720 keep a plain fix, 795 are held back, 58 get no fix. The text and range of every remaining fix equal main's.
  • Held back by repository: Effect-TS/effect 707, vitest-dev/vitest 62, trpc/trpc 10, storybookjs/storybook 6, nuxt/nuxt 4, TanStack/router 3, colinhacks/zod 2, honojs/hono 1.
  • Calls of the check (gdb breakpoint counts, 1,000 files, --threads=1): 0 for a file that does not mention vitest, and 0 where fixes are not read (--format unix, oxlint's JSON). 1 per report with --fix-dry-run.
  • Scope, variable and reference tables built by the check: 0. Token table: 0, and 1 per file where the statement after a removed one starts with (, [, a backtick, /, +, - or <.
  • Allocator calls for 1,000 files (ASAN statistics, no config, --threads=1): equal to main for files that are not reported, for fixes that --fix applies, and where fixes are not read. A held-back report: 85,931 against 82,931 where JSON prints the fix, and 74,189 against 120,230 with --fix-dry-run, which applies nothing. An import before a line that starts with (: 5 more per file.
  • Release binary (llvm-size -A): .text 122,621,673 to 122,631,913 (+10,240), .rodata unchanged, file +12,288. The rule's symbols and its helpers: 20,013 bytes against 10,640. Fixer::remove_statement: 2,874 bytes.
  • User instructions for 10,000 files that import from vitest and are not reported (perf_event_open, median of 7 interleaved runs each, --threads=1): 285,766,038 against 285,736,079, which is +29,959 or 3 per file. Four of the seven runs of each binary are within 300. perf and valgrind are not installed here.

Tests

  • Each case is a test file that passes. The test lints all cases in one run as written, with --fix and with --fix-dangerously, with no config and next to an .oxlintrc.json. bun test runs them as written and after each flag, with globals = "vitest".
  • 30 cases remove each of the 15 names that bun test has as a global of Vitest, as an import and as a require(). One test asserts that bun test exits with 0 after --fix.
  • The runner repeats the one of mock-hoisting.test.ts in bun lint: hold back fixes that change what bun test does with module mocks on jest or vi #48674. The PR that lands second moves both tables onto one module.
  • The cases are not rows of the table in test/cli/lint/defaults.test.ts, because that table does not run bun test.

Review history

  • The first commit also held back assert, chai and a last import next to require, and it listed three names of @effect/vitest. The second commit removes the first two holds (see "Not in this PR") and holds back every name of that package.
  • Three self-reviews raised 116 concerns. The second one asked for the cut to the holds that are true for every runner.
  • Rejected, with the reason:
    • Run the type checker on the TypeScript cases: what TypeScript reports for a script is not this rule's behavior.
    • A fix key on the example markers of the docs pages: a change to the docs test, for its own PR.
    • Report the oxlint behavior upstream: not part of this repository's change.
    • Route three other rules through Fixer::remove_statement: each changes another rule's fixes. They are named above.
    • Test rows for a preload script, and rows that pin the two removed holds: they would pin behavior that waits for the open question.
    • A refusal below a used eslint-disable-next-line, above a string with a statement before it, or for @ts-ignore on the last line of a block comment: is_next_line_directive is main's and has other callers, and the refusals are in the safe direction.
    • A hold for this in a top-level arrow function of a would-be script, and for vi.mock() hoisting under bun test (bun lint: hold back fixes that change what bun test does with module mocks on jest or vi #48674 has that one).
    • A comment that becomes the file's first comment after the removal, such as a misplaced // @ts-nocheck.

…hange what the file does

`--fix` deleted `const { vi: spy, test, expect } = require("vitest")` and
left `spy` undeclared. The fix is oxlint's. It goes by the key of a
property, or by the local name of an import, and looks at nothing else.

Both report sites now use `Report::fix_where_safe`. `--fix` applies the fix
where the global of the same name is what each removed name was, and where
the rest of the file stays what it is. `--fix-dangerously` applies oxlint's
text to the other statements. The reports do not change.

`Fixer::remove_statement` is new. It removes a whole statement, unless that
loses a comment, leaves a `// @ts-ignore` to the next statement, joins the
statements around it, or takes the body of an `if` or a loop.
@coderabbitai

coderabbitai Bot commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The Vitest globals rule adds safety checks before fixing imports and require() calls. Statement-removal utilities support these checks. CLI tests cover fix outcomes, and documentation describes fix limitations and usage guidance.

Changes

Vitest global import fixes

Layer / File(s) Summary
Statement removal safety
src/lint/fix.rs, src/lint/utils/visible.rs
Statement removal checks statement-list placement, directives, token continuation, and comment preservation. A new helper checks whether a file remains a module after selected statements are excluded.
Import and require fix checks
src/lint/jest/rules/vitest_no_importing_vitest_globals.rs
Import fixes check recognized names, named exports, and TypeScript module status. Require fixes are limited to supported top-level const declarations and check retained properties, exports, and comments.
Rule tests and documentation
test/cli/lint/no-importing-vitest-globals.test.ts, test/cli/lint/conformance/expected-oxlint.txt, docs/lint/rules/vitest/no-importing-vitest-globals.mdx, docs/lint/rules/reports.json
CLI tests cover import and require forms and their fix outcomes. The expected count of cases held back by --fix changes from 432 to 434. The docs and report fixture describe diagnostics and fix behavior.

Suggested reviewers: jarred-sumner

Merge Risk: Minimal · up to 850af

The change makes bun lint --fix more conservative for Vitest global imports, so it no longer removes a binding that is still used under a different name. No actionable merge-blocking risk remains in the supplied evidence.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the main change: --fix for vitest/no-importing-vitest-globals now holds back fixes that change program behavior. The objectives and the description both describe this change. The t…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Description check Passed The description follows the repository template. It has both required sections: "What does this PR do?" (as ### Problem / ### Fix / ### Background / ### Downsides) and "How did you verify your code wo…
Full details: Title check

Explanation

The title names the main change: --fix for vitest/no-importing-vitest-globals now holds back fixes that change program behavior. The objectives and the description both describe this change. The title is specific and does not use vague terms. It is slightly long at 93 characters, but it remains clear.

Full details: Description check

Explanation

The description follows the repository template. It has both required sections: "What does this PR do?" (as ### Problem / ### Fix / ### Background / ### Downsides) and "How did you verify your code works?" (under ### Verified and in the Tests and Measurements notes). It states the bug, the fix at both fix sites, the new Fixer::remove_statement guards, the test commands, conformance and rule-docs results, and the known downsides and held-back behaviors. The details block is long but relevant. The section headings differ from the template wording, but the required information is present.

  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between 0e652ce and 850afd9.

Files selected for processing: 3

  • docs/lint/rules/vitest/no-importing-vitest-globals.mdx
  • src/lint/jest/rules/vitest_no_importing_vitest_globals.rs
  • test/cli/lint/no-importing-vitest-globals.test.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 4 remain after this review.

@robobun

robobun commented Nov 13, 2026 •

Copy link
Copy Markdown
Collaborator Author
Updated 12:32 PM PT - Nov 13th, 2026

✅ @robobun, your commit 850afd9015c06336f02c6b2af886e27bf18c0371 passed in Build #141153! 🎉


🧪   To try this PR locally:

bunx bun-pr 48780

That installs a local version of the PR into your bun-48780 executable, so you can run:

bun-48780 --bun

@robobun

robobun commented Nov 13, 2026 •

Copy link
Copy Markdown
Collaborator Author

Status

  • Reproduced on a debug build of main. a.test.js starts with const { vi: spy, test, expect } = require("vitest"); and passes under bun test. After bun lint --fix --rule 'vitest/no-importing-vitest-globals: warn' the line is gone, and bun test fails with ReferenceError: spy is not defined.
  • With this PR, --fix leaves the line and the test passes. --fix-dangerously still applies the fix.
  • Test: bun bd test test/cli/lint/no-importing-vitest-globals.test.ts. 49 of its 92 tests fail on main, and 92 pass with this change.

@claude claude Bot 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.

Nothing blocking. The comments below are optional suggestions. There is no need to push a fix for them before merging.

Comment thread src/lint/fix.rs Resolved
Comment thread src/lint/fix.rs Resolved
… are true for every runner

`--fix` now leaves every import from `@effect/vitest` to
`--fix-dangerously`, and a `require()` call with type arguments. The
rule has no list of the names that the package declares itself.

Two holds are gone, because they are only true for `bun test`:

- the last `import` of a file that mentions `require`, `module` or
  `exports`
- `assert` and `chai`, which Vitest has as globals

The check for a TypeScript file that would become a script counts
only the imports that the fix removes whole. It does not count an
export of no names, which `@typescript-eslint/no-useless-empty-export`
can remove in the same pass.

A name that the file exports with `export { name }` gets no fix: the
text would leave the export without its name.

`Fixer::remove_statement` accepts a statement in a `namespace` block
and ignores a directive comment in the header of the file. It does not
remove a statement above a string statement, which could become a
directive, or between `a++` and a line that starts with `(`, which Bun
and esbuild take for a call.

@claude claude Bot 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.

Nothing blocking. The comments below are optional suggestions. There is no need to push a fix for them before merging.

…ypeScript file with `import.meta`

TypeScript takes a file with `import.meta` or `import a = require("a")`
for a module in every `moduleDetection` mode. The removal of the last
`import` of such a file does not make it a script, so `--fix` applies
it.

A name that the file exports with `export { name }` gets no fix also
where a nested pattern of a `require()` binds it.
@robobun

robobun commented Nov 13, 2026

Copy link
Copy Markdown
Collaborator Author

Changes for the second round of review comments, in 850afd9:

  • A TypeScript file with import.meta or import a = require("a"): --fix applies the fix again. TypeScript takes such a file for a module in every moduleDetection mode. .mts and .cts files stay held back, and so does a file with a top-level for, if or try, which can hold a var.
  • A nested pattern next to export { name }: no flag fixes it. The check visits every name that a dropped property binds.

The test file has 92 tests now. 49 of them fail on main's lint code.

@claude claude Bot 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.

Nothing blocking. The comments below are optional suggestions. There is no need to push a fix for them before merging.

@robobun

robobun commented Nov 13, 2026

Copy link
Copy Markdown
Collaborator Author

No code change for the third round of review comments. I checked both with tsc:

  • .mts and .cts files: with "moduleDetection": "legacy" they are scripts without an import (TS2451 for two files that declare the same name, tsc 5.0.4 to 6.0.2). The fix stays for --fix-dangerously there.
  • TS1208 under isolatedModules: TypeScript 5.0 removed that error. tsc 4.9.5 reports it, 5.0.4 and later do not.

The PR notes name both cases under "Not in this PR".

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant