Skip to content

Inconsistent results for path.toNamespacedPath() for win32 UNC paths #30224

Closed
@btsimonh

Description

@btsimonh
  • Version: v12.10.0
  • Platform: windows 10 64 bit
  • Subsystem: path/fs

when using UNC paths, the function path.toNamespacedPath() gives different results from non UNC paths.

This is reproduced by running the code attached below.
Basically, most of the time in Node, you can freely use '/' and '', and most repos just use '/', for example this snippet:
fs.readdirSync(__dirname + '/grant').forEach(function(filename) {
from
https://github.com/jaredhanson/oauth2orize/blob/master/lib/index.js

The issue is that if the path being accessed is a UNC style (actually, problem is only if accessed pre-namespaced?) path, then the '/' does not get translated to a '' on windows. If the path is a 'normal' drive based windows path, then the '/' is replaced by '' in path.toNamespacedPath(), which is used by most fs functions to qualify a path, and so there is no issue.

As you can see from the results of the simple tests, path.win32.resolve('\\?\c:\Windows/System') returns '\\?\c:\Windows\System'.
path.toNamespacedPath(path) uses this internally, but returns path, not resolvedPath in this circumstance. So the resulting path from path.toNamespacedPath('\\?\c:\Windows/System') is '\\?\c:\Windows/System' - which is invalid.
(I will post a proposed solution in the next comment).

Simple example test:

on a windows system, run the following code:

var tests = [
  'c:\\Windows\\System',
  'c:\\Windows/System',
  '\\\\?\\c:\\Windows\\System',
  '\\\\?\\c:\\Windows/System',
]

var path = require('path');
var fs = require('fs');

for (var i = 0; i < tests.length; i++) {
  console.log('path.toNamespacedPath("'+tests[i]+'") -> "'+path.toNamespacedPath(tests[i])+'"');
  console.log('path.win32.normalize("'+tests[i]+'") -> "'+path.win32.normalize(tests[i])+'"');
  console.log('path.win32.resolve("'+tests[i]+'") -> "'+path.win32.resolve(tests[i])+'"');
  try{
    var dir = fs.readdirSync(tests[i]);
    console.log('fs.readdirSync("'+tests[i]+'") success');
  } catch(e) {
    console.log(e);
    console.log('fs.readdirSync("'+tests[i]+'") failed');
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    pathIssues and PRs related to the path subsystem.windowsIssues and PRs related to the Windows platform.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions