Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Path.resolve reports the wrong path for junctioned paths in windows. #22592

Closed
Pomax opened this issue Aug 29, 2018 · 4 comments
Closed

Path.resolve reports the wrong path for junctioned paths in windows. #22592

Pomax opened this issue Aug 29, 2018 · 4 comments

Comments

@Pomax
Copy link

Pomax commented Aug 29, 2018

Running Path.resolve on a junctioned path in Windows 10 yields the "true" path, rather than the path that as seen from the working directory. This means that anything that checks whether a path is located relative to another path may fail, even though the junctions were set up in a way where this should absolutely not be the case.

This is particularly obvious when running web servers with static directories, so a demonstrator:

var Path = require('path');
var cwd = __dirname;
var fullPath = Path.resolve(cwd);
console.log(fullPath);
  • have two drives, C: and D:
  • create a D:\junctions\git\test\ dir
  • in C:\Users\YourName\Documents\, create a directory junction git <==> D:\junctions\git
  • cd into C:\Users\YourName\Documents\git\test
  • run node test.js

While this should output C:\Users\YourName\Documents\git\test\, because that's what symlinks/junctions are used for (using paths as if they are local to somewhere they are not local to) it will instead output the original D:\junctions\git\projects\test path.

For a practical demonstration of how this ruins the fun, the Hapi inert middleware (responsible for static file serving) grinds to a halt on this because it sees an attempt to load a file that we as users think we've housed in a project-local directory, except Path.resolve turns that local path into a path on a completely different drive, massively violating containment rules and leading to a situation that requires diving into node_modules dir and debugging code two dependencies deep, only to find Node itself is doing the wrong thing =)

So, as the documentation for Path.resolve does not mention what it will do with symlinks, this is both a bug report and a request:

  • can we fix Path.resolve so that it generates a proper path, only asking for the true resource path belonging to a symlink/junction when an option is passed to enable that? And,
  • can we update the documentation for Path.resolve to mention what it does when it encounters a symlink or junction, so that developers know what behaviour can be expected from the Path module when it comes to "fake paths"?
@bzoz
Copy link
Contributor

bzoz commented Aug 30, 2018

What is the value of cwd in your example?

@Pomax
Copy link
Author

Pomax commented Aug 30, 2018

Code:

var Path = require('path');
var cwd = __dirname;
var fullPath = Path.resolve(cwd);
console.log("cwd:", process.cwd());
console.log("resolved test.js:", fullPath);

Result:

c:\Users\Pomax\Documents\Git\temp>node test.js
cwd: c:\Users\Pomax\Documents\Git\temp
resolved test.js: J:\Junctions\Users\Pomax\Documents\Git\temp

So cwd() is doing exactly what one would expect.

@bzoz
Copy link
Contributor

bzoz commented Aug 30, 2018

The issue here is that __dirname points to J:\Junctions\Users\Pomax\Documents\Git\temp. The path.resolve does not care for symlinks, it just concatenates strings. There isn't any bug here, so I'm gonna close the issue.

You can try node --preserve-symlinks test.js - this should make Node behave like you would expect.

@bzoz bzoz closed this as completed Aug 30, 2018
@Pomax
Copy link
Author

Pomax commented Aug 30, 2018

Looking at https://nodejs.org/docs/latest/api/modules.html#modules_dirname, which points us to https://nodejs.org/docs/latest/api/modules.html#modules_filename, there is no mention what this value is supposed to be other than an "absolute path", which does not say what it does (or rather, what it defines as what will happen) on symlinks and junctions. So I'll file a new issue for that. Thanks for the help.

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

No branches or pull requests

2 participants