-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Support NODE_PATH in forked process #531
Conversation
By analyzing the blame information on this pull request, we identified @novemberborn, @ariporad and @sotojuan to be potential reviewers |
Rather than modifying |
@@ -86,6 +86,9 @@ var oldNodeModulesPaths = module.constructor._nodeModulePaths; | |||
module.constructor._nodeModulePaths = function () { | |||
var ret = oldNodeModulesPaths.apply(this, arguments); | |||
ret.push(nodeModulesDir); | |||
if (process.env.NODE_PATH) { | |||
ret.push(process.env.NODE_PATH); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
NODE_PATH
can contain multiple values. Not sure if the env var needs to be split and concatenated with ret
here. See https://nodejs.org/api/modules.html#modules_loading_from_the_global_folders for more.
Thanks for your inputs @novemberborn , I made the following changes:
|
@@ -13,7 +13,8 @@ module.exports = function (file, opts) { | |||
tty: process.stdout.isTTY ? { | |||
columns: process.stdout.columns, | |||
rows: process.stdout.rows | |||
} : false | |||
} : false, | |||
projectRoot: process.cwd() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This isn't necessarily the project root. Perhaps parentWorkingDirectory
? Bit verbose though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about parentCwd
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure
Perhaps resolve the additional, absolute You should add a test, probably in |
@@ -13,7 +13,8 @@ module.exports = function (file, opts) { | |||
tty: process.stdout.isTTY ? { | |||
columns: process.stdout.columns, | |||
rows: process.stdout.rows | |||
} : false | |||
} : false, | |||
additionalPaths: opts.additionalPaths |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Object.assign()
already takes care of mixing in the opts
.
I added the test and it seems like it's green now, but I surely forgot something else, let me know if something is missing! |
@@ -93,11 +94,22 @@ if (cli.flags.init) { | |||
return; | |||
} | |||
|
|||
var nodePaths; | |||
if (process.env.NODE_PATH) { | |||
var osSplitChar = process.platform === 'win32' ? ';' : ':'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead you can use path.delimiter
. https://nodejs.org/api/path.html#path_path_delimiter
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cool! So I can get rid of all the env check stuff!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I should have commented it earlier :)
@ingro I think what @sindresorhus is saying is that you can add an |
Yup, and keep in mind that the whole existing |
Well I think that's cleaner, I will look at it as soon as I have time! |
The commit has some conflicts, what's the procedure in situation like this? |
@ingro You resolve the merge conflict. (No need to open a new PR) |
It seems we are back to green! |
.map(function (p) { | ||
return path.resolve(p); | ||
}) | ||
.join(path.delimiter); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could compute this outside of the function so it can be reused for subsequent forks.
|
||
env = objectAssign({ | ||
NODE_PATH: nodePath | ||
}, env); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ugh, discovered this too late. This makes no sense. Here you overwrite the modified NODE_PATH
with the original one. This means the tests are also invalid. Tests still pass when the .map
is commented out.
Welp sorry, I blindly copied from one of your comment above. Anyway tests still fails, will look into that! |
I think I found the problem with the test, it was: var nodePaths = 'node-paths/modules' + path.delimiter + 'node-paths/deep/nested'; while it should have been: var nodePaths = 'fixture/node-paths/modules' + path.delimiter + 'fixture/node-paths/deep/nested'; That's because I've fixed that in my fork, I should create another pull request? |
Yep! 👯 |
Created here #559 |
@sindresorhus I think this regressed with commit 1f10a7c. |
@hildjj that was intentional. |
Is this the correct place to discuss whether that was the correct decision? I don't see an issue or pull request associated with that commit, so I'll assume yes, unless you'd like me to open a new issue. I have an (admittedly crappy) use case where being able to manipulate NODE_PATH is very handy. It involves using the same test suite to test two different builds of the same software that have subtle differences in their dependencies. I can figure out another way around this if I have to, but it was convenient. |
You could use an |
I'm arguing that it's not specialized handling. ava is now modifying the environment being passed to my tests in a surprising way. At the very least I would have expected something in the release notes and something in the docs for a change that breaks existing functionality built in to node. The docs currently say: "environmentVariables: specifies environment variables to be made available to the tests. The environment variables defined here override the ones from process.env" which is at least confusing at the moment, since it's not clear that the
|
No, AVA 3 no longer modifies anything. But let's continue this in a Discussion. |
#2682 for linkage |
This PR tries to fix #515 in a non-destructive way.
Basically it checks for the presence of
NODE_PATH
env variable before forking the process and creates an absolute path from it, so it could be used intest-worker.js
as additional node module path.That won't work there because
process.cwd()
points to the forked process test file.