Skip to content

🐛 Bug: resolveBin function doesn't work correctly on Windows #2064

Closed
@astrochemx

Description

@astrochemx

Bug Report Checklist

  • I have tried restarting my IDE and the issue persists.
  • I have pulled the latest main branch of the repository.
  • I have searched for related issues and found none that matched my issue.

Expected

To create a new app without any errors.

Actual

On Windows, an error occurs in the resolveBin function due to incorrect path handling, which results in a duplicate drive letter, such as C:\C:\GHub\create-typescript-app\node_modules\.pnpm\cspell-populate-words@0.3.0\node_modules\cspell-populate-words\bin\index.mjs.

Example of an error:
Error: Cannot find module 'C:\C:\GHub\create-typescript-app\node_modules\.pnpm\cspell-populate-words@0.3.0\node_modules\cspell-populate-words\bin\index.mjs'
    at Function._resolveFilename (node:internal/modules/cjs/loader:1405:15)
    at defaultResolveImpl (node:internal/modules/cjs/loader:1061:19)
    at resolveForCJSWithHooks (node:internal/modules/cjs/loader:1066:22)
    at Function._load (node:internal/modules/cjs/loader:1215:37)
    at TracingChannel.traceSync (node:diagnostics_channel:322:14)
    at wrapModuleLoad (node:internal/modules/cjs/loader:235:24)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:151:5)
    at node:internal/main/run_main_module:33:47 {
      code: 'MODULE_NOT_FOUND',
      requireStack: []
    }
Node.js v23.10.0
    at getFinalError (file:///C:/GHub/bingo/node_modules/.pnpm/execa@9.5.2/node_modules/execa/lib/return/final-error.js:6:9)
    at makeError (file:///C:/GHub/bingo/node_modules/.pnpm/execa@9.5.2/node_modules/execa/lib/return/result.js:108:16)
    at getAsyncResult (file:///C:/GHub/bingo/node_modules/.pnpm/execa@9.5.2/node_modules/execa/lib/methods/main-async.js:167:4)       
    at handlePromise (file:///C:/GHub/bingo/node_modules/.pnpm/execa@9.5.2/node_modules/execa/lib/methods/main-async.js:150:17)       
    at async runCommand (file:///C:/GHub/bingo/packages/bingo/lib/runners/applyScriptsToSystem.js:10:24)
    at async file:///C:/GHub/bingo/packages/bingo/lib/runners/applyScriptsToSystem.js:22:17
    at async Promise.all (index 1)
    at async applyScriptsToSystem (file:///C:/GHub/bingo/packages/bingo/lib/runners/applyScriptsToSystem.js:20:9)
    at async Promise.all (index 0)
    at async runCreation (file:///C:/GHub/bingo/packages/bingo/lib/runners/runCreation.js:8:5)

Additional Info

🧐 Cause of the Issue

On Windows, file paths typically begin with a drive letter (e.g., C:). However, to conform to the file:// URI scheme in Node.js, they must be prefixed with a slash (e.g., /C:), resulting in a correct file URL like file:///C:/GHub/file.js.

Currently, the resolveBin function does not properly handle this conversion and mistakenly outputs an absolute path with a leading slash, such as /C:/GHub/file.js. This format is invalid on Windows, where the correct absolute path should be C:/GHub/file.js.

Mishandling paths that start with a leading slash on Windows can cause them to be interpreted as relative to the current drive, leading to incorrect resolutions. In this case, it results in an erroneous path with duplicated drive letters (like C:/C:/GHub/file.js), as seen in the error output.

💡 Proposed Solution

To resolve this issue, we should use the fileURLToPath function from the node:url module instead of manually handling paths via regular expressions.

🖥️ Proposed Code

Original code:

export function resolveBin(bin: string) {
	return import.meta.resolve(bin).replace(/^file:\/\//gu, "");
}

Updated code:

import { fileURLToPath } from "node:url";

export function resolveBin(bin: string) {
	return fileURLToPath(import.meta.resolve(bin));
}

This approach ensures that paths are correctly converted and eliminates the risk of incorrect path handling on Windows.

✔️ Testing

I have tested such change on Windows and WSL (Ubuntu 24.04.2 LTS), and it works as expected.


💖

Metadata

Metadata

Assignees

No one assigned

    Labels

    status: accepting prsPlease, send a pull request to resolve this!type: bugSomething isn't working :(

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions