fixhunt

biome correctness/noGlobalDirnameFilename: fix changes program behavior

fix-changes-behaviorlabelled safeprogram-bug · highno match
Findings / 2026-10-17T20-16-26-722Z-l85y-biome-correctness_noGlobalDirnameFilename / r1-p001biome 2.5.153 → 1 lines2026-10-17 20:16 UTC

Hypothesis Nemotron 3 Super predicted to fire fired 1×

When __dirname/__filename is used in a dynamic import() argument, the rewrite may alter timing or availability if import.meta is not yet resolved.

Program

before
1const mod = await import(`./${__dirname}/data.json`);
after
1const mod = await import(`./${import.meta.dirname}/data.json`);

Output

stdout beforeexit 1
(no stdout)
ReferenceError: __dirname is not defined in ES module scope
stdout afterexit 1
(no stdout)
Error [ERR_MODULE_NOT_FOUND]: Cannot find module '/w//w/data.json' imported from /w/prog.js
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 as an ES module where __dirname is not defined, so it already fails before the fix with a ReferenceError. The fix replaces __dirname with import.meta.dirname, which is the correct ES module equivalent, but the resulting path '/w//w/data.json' suggests the module resolution environment differs from what the original CommonJS-style code expected. The behavior change stems from the module system environment, not from an incorrect rewrite.

Explanation Nemotron 3 Super unverified

The original code uses `__dirname`, which resolves to the directory of the current file (e.g., `/w`). After rewriting with `import.meta.dirname`, it attempts to use that value directly as part of a path string (`./${import.meta.dirname}`). However, when concatenated into `"../w/w"` or similar due to incorrect relative resolution logic—especially if `import.meta.dirname` already includes an absolute path—the resulting module specifier becomes malformed (e.g., `'/w//w/data.json'`), leading to a failed lookup. This occurs because `import.meta.dirname` returns an absolute path, making `./${import.meta.dirname}` resolve incorrectly compared to the intended relative path based on `__dirname`. Thus, the semantic intent of resolving a module relative to the current file’s directory is broken.

Reproduce

file
prog.js (the before program)
minimized program
r1-p001.js
evidence sheet
r1-p001.md
regression fixture
r1-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 with dynamic import · top-level await with __dirname error · import.meta.dirname replacement pattern

no duplicate or related report 30 hits triaged

Details

program
Dynamic import argument uses __dirname
hazard
When __dirname/__filename is used in a dynamic import() argument, the rewrite may alter timing or availability if import.meta is not yet resolved.
oracle
stderr before: ReferenceError: __dirname is not defined in ES module scope stderr after: Error [ERR_MODULE_NOT_FOUND]: Cannot find module '/w//w/data.json' imported from /w/prog.js
run
2026-10-17T20-16-26-722Z-l85y-biome-correctness_noGlobalDirnameFilename
generated program (3 lines) and the fix's diff
1// Hazard 1
2const mod = await import(`./${__dirname}/data.json`);
3console.log('done');
--- orig.js	2026-10-17 16:16:38.781057717 -0400
+++ fixed.js	2026-10-17 16:16:39.079054053 -0400
@@ -1,3 +1,3 @@
 // Hazard 1
-const mod = await import(`./${__dirname}/data.json`);
+const mod = await import(`./${import.meta.dirname}/data.json`);
 console.log('done');
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/")) {}
 ```