Skip to content

fix: use fileURLToPath for Windows path resolution#101

Open
voidborne-d wants to merge 1 commit intopbakaus:mainfrom
voidborne-d:fix/windows-detect-path-drive-letter
Open

fix: use fileURLToPath for Windows path resolution#101
voidborne-d wants to merge 1 commit intopbakaus:mainfrom
voidborne-d:fix/windows-detect-path-drive-letter

Conversation

@voidborne-d
Copy link
Copy Markdown

Problem

npx impeccable detect <url> fails on Windows with:

Error: Browser script not found at C:\C:\Users\...\detect-antipatterns-browser.js

Note the doubled C:\C:\.

Fixes #95.

Root Cause

Two locations in src/detect-antipatterns.mjs construct the path to detect-antipatterns-browser.js using:

path.dirname(new URL(import.meta.url).pathname)

On Windows, new URL("file:///C:/foo/bar").pathname returns /C:/foo/bar (with a leading slash). When passed to path.resolve() or path.join(), Node prepends the drive letter again, producing C:\C:\....

Fix

Replace both occurrences (~L2690 puppeteer scan and ~L3506 live detect) with fileURLToPath(import.meta.url) from node:url, which correctly strips the leading slash on Windows while remaining a no-op on POSIX.

The import is added inside the existing !IS_BROWSER guard alongside fs and path, so the browser bundle is unaffected.

Testing

Added tests/windows-path-fix.test.js with 4 regression tests:

  1. Drive-letter doubling guard — verifies fileURLToPath + path.resolve never produces C:\C:\ patterns
  2. POSIX file URL handling — verifies fileURLToPath returns correct paths on Linux/macOS
  3. import.meta.url contract — verifies it produces a valid file:// URL
  4. Source audit — asserts no remaining path.(resolve|join|dirname)(new URL(import.meta.url).pathname patterns in the source

All existing tests pass (157 pass, 1 pre-existing unrelated failure in jsdom stylesheet test).

On Windows, `new URL(import.meta.url).pathname` returns `/C:/...`
(with a leading slash). Passing that to `path.resolve()` or
`path.join()` causes Node to prepend the drive letter again, producing
doubled paths like `C:\C:\Users\...\detect-antipatterns-browser.js`.

Replace both occurrences (puppeteer scan at ~L2690 and live detect at
~L3506) with `fileURLToPath(import.meta.url)` from `node:url`, which
correctly strips the leading slash on Windows while remaining a no-op
on POSIX.

Add regression tests verifying the source no longer uses the raw
`.pathname` accessor for local path construction and that
`fileURLToPath` handles both Windows and POSIX file URLs correctly.

Closes pbakaus#95
@voidborne-d voidborne-d requested a review from pbakaus as a code owner April 16, 2026 14:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Windows: detect command fails with doubled drive letter in path

1 participant