fixhunt

biome correctness/noGlobalDirnameFilename: fix changes program behavior

fix-changes-behaviorlabelled safeprogram-bug · highrelated
Findings / 2026-10-17T20-16-26-722Z-l85y-biome-correctness_noGlobalDirnameFilename / r2-p008biome 2.5.154 → 1 lines2026-10-17 20:16 UTC

Hypothesis Nemotron 3 Super predicted to fire fired 1×

+ __dirname may coerce non-string; import.meta.dirname is always string.

Program

before
1console.log('template:', `${__dirname}${x}`);
after
1console.log('template:', `${import.meta.dirname}${x}`);

Output

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

Judge Nemotron 3 Ultra advisory program-bug · high

The program runs in an ES module environment where __dirname is not defined, causing a ReferenceError before the fix. The fix correctly rewrites __dirname to import.meta.dirname for ES modules, but the program also references an undefined variable x, which causes a different ReferenceError after the fix. The behavior change is due to the execution environment and a pre-existing undefined variable, not a bug in the fix itself.

Explanation Nemotron 3 Super unverified

The original code uses `__dirname`, which is undefined in ES modules, causing a ReferenceError when evaluating the template literal; this error occurs before `x` is accessed. After rewriting to `import.meta.dirname`, that variable is defined (assuming the environment supports it), so the template literal evaluates successfully, attempting to concatenate the directory path with `x`. Since `x` remains undefined, a ReferenceError now arises from `x` instead. The change shifts the point of failure from `__dirname` to `x`, altering the observed error and thus the program’s behavior under execution. This difference in error timing and target violates semantic equivalence.

Reproduce

file
prog.js (the before program)
minimized program
r2-p008.js
evidence sheet
r2-p008.md
regression fixture
r2-p008.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 · correctness noGlobalDirnameFilename template literal · __dirname usage in ES module string interpolation · replace __dirname with import.meta.dirname

  • related biomejs/biome/issues/5778 Same rule category (correctness/noGlobalDirnameFilename vs noProcessGlobal) and same class of defect (global replacement in ES modules), but different construct (import.meta.dirname vs node:process import).

Details

program
Coercion differences: + vs template literal
hazard
+ __dirname may coerce non-string; import.meta.dirname is always string.
oracle
stderr before: ReferenceError: __dirname is not defined in ES module scope stderr after: ReferenceError: x is not defined
run
2026-10-17T20-16-26-722Z-l85y-biome-correctness_noGlobalDirnameFilename
generated program (4 lines) and the fix's diff
1const x = { toString: () => 42 };
2console.log('+ coercion:', __dirname + x);
3console.log('template:', `${__dirname}${x}`);
4console.log('import.meta + x:', import.meta.dirname + x);
--- orig.js	2026-10-17 16:16:51.912898979 -0400
+++ fixed.js	2026-10-17 16:16:52.176895844 -0400
@@ -1,4 +1,4 @@
 const x = { toString: () => 42 };
-console.log('+ coercion:', __dirname + x);
-console.log('template:', `${__dirname}${x}`);
+console.log('+ coercion:', import.meta.dirname + x);
+console.log('template:', `${import.meta.dirname}${x}`);
 console.log('import.meta + x:', import.meta.dirname + x);
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/")) {}
 ```