On OSX, if you are a temporary directory, as generated via require('os').tmpDir() (like /var/folders/ry/c05q3yxs1q98yps6h2_g3gn00000gn/T) It's possoble that the various ways of determining your path do not match:
__dirname => /private/var/folders/ry/c05q3yxs1q98yps6h2_g3gn00000gn/Tprocess.env.PWD => /var/folders/ry/c05q3yxs1q98yps6h2_g3gn00000gn/Tprocess.cwd() => /private/var/folders/ry/c05q3yxs1q98yps6h2_g3gn00000gn/TI've only seen this behavior when working inside a temporary directory, and only on OSX. process.env.PWD seems to be the odd-man out.
process.cwd() is using the realpath here so, since on OS X, /var is a symlink to /private/var, it is technically correct. Although, I'm not sure why __dirname is not resolving the real path
Looking at the example, __dirname and process.cwd() show the same value, which looks correct. process.env.PWD is not going to resolve the symlink, so these values are correct.
The interesting thing here is that the path I CD'd into via bash _only_ agrees with process.env.PWD, and it looks like the node internal commands (__dirname and process.cwd()) agree internally, but disagree with the actual shell.
This is certainly a strange edge case, as the OSX /tmp directory is a confusing mess of symlinks.
@evantahler do you have a reproducible test case that exhibits this behavior? (and preferably does not depend on any external modules)
Hi other Evan, sure thing!
In a console:
node -e "console.log( require('os').tmpDir() )"
You'll get back something like /var/folders/ry/c05q3yxs1q98yps6h2_g3gn00000gn/T
cd /var/folders/ry/c05q3yxs1q98yps6h2_g3gn00000gn/T
In that temp directory run the following file:
// test.js
console.log('__dirname => ' + __dirname);
console.log('process.env.PWD => ' + process.env.PWD);
console.log('process.cwd() =>' + process.cwd());
Your output will indicate that you don't have a match, IE:
> node test.js
__dirname => /private/var/folders/ry/c05q3yxs1q98yps6h2_g3gn00000gn/T
process.env.PWD => /var/folders/ry/c05q3yxs1q98yps6h2_g3gn00000gn/T
process.cwd() =>/private/var/folders/ry/c05q3yxs1q98yps6h2_g3gn00000gn/T
Thanks @evantahler. I could reproduce using bash, but not with fish. It looks like bash isn't resolving the symlink for $PWD. As a workaround, one could simply run process.env.PWD through fs.realpath or fs.realpathSync and you should be in business.
I am going to go ahead and close as it doesn't appear to be an issue in core. Thanks!