Skip to content

Inconsistent resolution of path "C:" with spawn on Windows #7215

Closed

Description

@ScottFreeCode
  • Version: 6.2.1
  • Platform: Windows 8.1, 64-bit
  • Subsystem: child_process, path

On Windows, when Node is run from another Node instance using child_process.spawn, path.resolve("C:") yields "C:". Calling path.resolve("C:") from a Node script run directly yields the current working directory. Changing directories, even to the current working directory, causes further calls to path.resolve("C:") to yield the current working directory.

This issue was originally reported by @sttk to mochajs/mocha#2242

Reproduce with the following code:

test.js

const path = require("path")

console.log(path.resolve("C:"))
console.log(process.cwd())

process.chdir(process.cwd())

console.log(path.resolve("C:"))
console.log(process.cwd())

wrap.js

var spawn = require('child_process').spawn,
  path = require('path'),
  args = [path.join(__dirname, 'test.js')]

spawn(process.execPath, args, { stdio: 'inherit' })

Calling node test.js prints the current working directory four times; calling node wrap.js prints "C:" once followed by the current working directory three times.

Activity

  1. added
    child_process
    windows
    path
    on Jun 8, 2016
  2. RReverser commented on Jun 8, 2016

    @RReverser
    Member

    Generally that's how DOS / Windows always worked - there are separate current working directories per-disk, so when you were not specifying some path (even if \), disk itself resolves to that current directory.

    However, I agree that it might be confusing behavior in context of Node.

  3. ScottFreeCode commented on Jun 9, 2016

    @ScottFreeCode
    Author

    Yeah, I get that C: is the current working directory for the C disk; what I don't get is that when Node first spawns a process the current working directory for the current disk is not set to the same as the general current working directory, whereas they're the same in all other circumstances I can find (although I guess to really dig into it I may have to check the behavior in other programs/languages that can spawn processes and resolve C:...). Is it normal Windows behavior that by default the general current working directory is inherited but the per-disk current working directory is not inherited even for the current disk, thus causing the two to differ when (and only when) a process is first spawned?

  4. izatop commented on Jun 22, 2016

    @izatop

    I have same problem when scripts resolves path via npm run. You may use path.join instead of path.resolve, this solves issue.

  5. ScottFreeCode commented on Sep 24, 2016

    @ScottFreeCode
    Author

    I love how that commit/PR explains and answers everything I was wondering about how and why this was happening, as well as confirming the expected behavior based on other systems (including Microsoft's own .Net!) and providing a simple fix. 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    child_processpathwindows

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions