fixhunt

biome correctness/noGlobalDirnameFilename: fix changes program behavior

fix-changes-behaviorlabelled safeprogram-bug · lowno match
Findings / 2026-10-17T20-16-26-722Z-l85y-biome-correctness_noGlobalDirnameFilename / r2-p001biome 2.5.158 → 4 lines2026-10-17 20:16 UTC

Hypothesis Nemotron 3 Super predicted to fire fired 1×

If executed in CommonJS, import.meta.dirname/filename are undefined while __dirname/__filename exist.

Program

before
1const dir = __dirname;
2try {
3} catch (e) {
4}
after
1const dir = import.meta.dirname;
2try {
3} catch (e) {
4}

Output

stdout beforeexit 1
(no stdout)
ReferenceError: __dirname is not defined in ES module scope
stdout afterexit 0
(no stdout)
fired
1 before · 0 after
runs
each version twice, same file name, no network

Judge Nemotron 3 Ultra advisory program-bug · low

The program runs as an ES module where __dirname is not defined, causing a ReferenceError before the fix. The fix replaces it with import.meta.dirname, which is the correct ES module equivalent, so the behavioral change is due to the module system environment, not a bug in the rewrite itself.

Explanation Nemotron 3 Super unverified

The original script uses `__dirname`, which is unavailable in ES modules, causing a `ReferenceError` that exits with code 1. The replacement uses `import.meta.dirname`, which is only available when the file is treated as an ES module. If the environment still interprets the file as CommonJS (e.g., due to file extension or lack of `"type": "module"`), `import.meta` is undefined, making `import.meta.dirname` also throw a `ReferenceError`. However, if the environment now correctly treats it as an ES module, the error disappears and execution succeeds (exit 0). The behavioral change arises because the fix alters the module system’s interpretation of the file, affecting the availability of module-specific variables—not merely substituting equivalent values within the same system. This shift in execution context changes whether the program throws or runs, so the rewrite is not semantics-preserving.

Reproduce

file
prog.js (the before program)
minimized program
r2-p001.js
evidence sheet
r2-p001.md
regression fixture
r2-p001.js the minimized program with a header stating the observed change, for the tool's test suite
node prog.js
bunx @biomejs/biome lint --only=correctness/noGlobalDirnameFilename --write prog.js
node prog.js

Upstream search GitHub issues Tavily Nemotron 3 Super + Ultra

noGlobalDirnameFilename fix · __dirname usage in ES modules · import.meta.dirname replacement pattern · correctness noGlobalDirnameFilename linting

no duplicate or related report 35 hits triaged

Details

program
Non-ESM environment failure
hazard
If executed in CommonJS, import.meta.dirname/filename are undefined while __dirname/__filename exist.
oracle
exit 1 → 0 stderr before: ReferenceError: __dirname is not defined in ES module scope stderr after: (none)
run
2026-10-17T20-16-26-722Z-l85y-biome-correctness_noGlobalDirnameFilename
generated program (8 lines) and the fix's diff
1const dir = __dirname;
2console.log('original:', typeof dir === 'string' ? dir : 'not string');
3try {
4 const fixed = import.meta.dirname;
5 console.log('fixed:', typeof fixed === 'string' ? fixed : 'not string');
6} catch (e) {
7 console.log('error:', e.name, e.message);
8}
--- orig.js	2026-10-17 16:16:49.078932784 -0400
+++ fixed.js	2026-10-17 16:16:49.341929639 -0400
@@ -1,4 +1,4 @@
-const dir = __dirname;
+const dir = import.meta.dirname;
 console.log('original:', typeof dir === 'string' ? dir : 'not string');
 try {
   const fixed = import.meta.dirname;
rule documentation given to the models
Summary

- Name: noGlobalDirnameFilename
- Fix: safe
- Default severity: info
- Available from version: 2.0.0
- Diagnostic category: lint/correctness/noGlobalDirnameFilename


Description

 Disallow the use of `__dirname` and `__filename` in the global scope.

 They are [not available in ES modules](https://nodejs.org/api/esm.html#esm_no_filename_or_dirname).
 Starting with Node.js 20.11, `import.meta.dirname` and `import.meta.filename` have been introduced in ES modules, providing identical functionality to `__dirname` and `__filename` in CommonJS (CJS).

Examples

Invalid

 ```js,expect_diagnostic
 const dirname = __dirname;
 ```

 ```js,expect_diagnostic
 const filename = __filename;
 ```

 ``` js,expect_diagnostic
 const foo = { __filename }
 ```

 ```js,expect_diagnostic
 if (__dirname.startsWith("/project/src/")) {}
 ```

Valid

 ```js
 const dirname = import.meta.dirname
 const filename = import.meta.filename
 const foo = {__filename: import.meta.filename };
 if (import.meta.dirname.startsWith("/project/src/")) {}
 ```