Skip to content

New: nodepath option for the path#534

Closed
gyandeeps wants to merge 1 commit intoshelljs:masterfrom
gyandeeps:issue469
Closed

New: nodepath option for the path#534
gyandeeps wants to merge 1 commit intoshelljs:masterfrom
gyandeeps:issue469

Conversation

@gyandeeps
Copy link
Copy Markdown
Contributor

@gyandeeps gyandeeps commented Oct 20, 2016

This what i am thinking for nodepath config option. This works as i have tried it.
Its getting difficult to test since when you run npm test to run the unit test. NPM already adds the ./node_modules/.bin to the $PATH.

I will work on the tests but I wanted to know whether I am heading in the right direction.

var shellMethods = Object.create(shell);
var PATH = 'PATH';

// windows calls it's path 'Path' usually, but this is not guaranteed.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe we can rely on process.env to have case-insensitive keys, so process.env.PATH should always work.

process.env isn't a real JS object. See atom/atom#11302 as an example of this.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I saw npm doing this that why I did it to make sure we are cross platform

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmmm could you link me to the line in npm? I know that process.env is case-insensitive on my Windows machine. Perhaps it's not the case for everyone?

I would imagine things would get very broken in NodeJS if process.env were case-sensitive on Windows. Perhaps that's the case for older versions of Node?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Contributor Author

@gyandeeps gyandeeps Oct 20, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thumbsup means keep the change or remove it? because this change is tied with the below function which we are talking abt also.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I posted a follow up comment. It looks to me like they're making a PR to add this to the documentation (I didn't read the PR yet, so I could be mistaken). This seems to indicate that this is the intended behavior on Node on Windows.

My guess is that process.env has always had case-insensitive keys on Windows, but it was just never documented. I think it's ok to rely on that (unless we hear otherwise).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nfischer let me know whatever you thoughts are on this and I will react accordingly. Thanks

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gyandeeps let's go for the simpler approach, process.env.PATH. Thanks

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apparently this is not true. I use windows 10 and when I use windows default cmd then its called Path but if I run using git bash then its called PATH.
Thats why I see test fail on windows.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gyandeeps if this is not the case, then it would be good to chime in on nodejs/node#9157 and the corresponding pull request before Node 7 gets incorrect documentation.

As for this PR, let's go with your initial approach. Sorry for the confusion.

src/common.js Outdated
globOptions: {},
maxdepth: 255
maxdepth: 255,
nodepath: false
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a trailing comma? We don't enforce it via linting, but it's something we're trying to move toward to keep diffs simple.

src/common.js Outdated
var env = objectAssign({}, process.env);

if (config.nodepath) {
env[PATH] += (process.platform === 'win32' ? ';' : ':') + path.join(process.cwd(), 'node_modules', '.bin') + (process.platform === 'win32' ? ';' : ':');
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you change this to path.delimiter?

Also, I don't think we always want a trailing delimiter, so you can get rid of the second (process.platform === 'win32' ? ';' : ':'). Please correct me if Windows differs from Unix on this point.

src/common.js Outdated
return env;
};

exports.getPathFromEnv = function (env) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need this, since we can just use process.env.PATH to retrieve the value.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At some places in the code base we have used process.env.path || process.env.Path || process.env.PATH; to get the correct path. I just made a function to keep it at one spot.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I saw that. I think that wasn't actually necessary for us to do. We should probably convert everything to process.env.PATH for the sake of consistency.

Plus, we would run into issues if someone on unix ever did export path=foo (totally valid for a user to do, but it would provide incorrect results for which()).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am hearing get rid of this change and the above logic and just make it use process.env.PATH, correct?
Will make the change then.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah. Let's try that. If things break on Appveyor, then we can revert back to this.

@nfischer
Copy link
Copy Markdown
Member

Let's add a test in exec() and in which()

@gyandeeps
Copy link
Copy Markdown
Contributor Author

gyandeeps commented Oct 25, 2016

@nfischer All the work has been done. Some of things to note:

  1. Apparently PATH is not same across. I use windows 10 and when I use windows default cmd then its called Path but if I run using git bash then its called PATH. Thats why I see test fail on windows.
  2. I have added sinon so that we can fake process.env.PATH for testing purposes.
  3. I think we should soon move to using some kind of testing framework since unit test are not well managed currently.

@nfischer
Copy link
Copy Markdown
Member

@gyandeeps

  1. See my comment in-line
  2. sinon is fine
  3. Please see test: switch to AVA #512. I'll try to rebase off master sometime this week. There's still quite a bit of work to do, however. If you're interested in helping, feel free to make a PR against that branch.

@gyandeeps
Copy link
Copy Markdown
Contributor Author

Thanks @nfischer . I think PR is ready. Let me know if you have any suggestions.

@nfischer
Copy link
Copy Markdown
Member

@gyandeeps I'll take a look later today. If you can, please chime in on nodejs/node#9157, since that just added got merged into official node documentation, and it disagrees with your observations on process.env case-sensitivity. It would be bad if the documentation was incorrect but got merged anyway.

@nfischer
Copy link
Copy Markdown
Member

nfischer commented Nov 3, 2016

@gyandeeps Sorry for the late reply. I got a bit distracted with #512

Here's my thoughts on this PR:

shell.env (links to process.env) is already part of our API, so I'm concerned about moving away from that to a custom function. It feels a little clunky to use, especially with common.PATH_IDENTIFIER. I wanted to get your feedback on a slightly different approach.

Instead of having code to get process.env.PATH (regardless of how PATH is capitalized), what if we just use process.env.PATH = process.env.PATH || process.env.Path || process.env.path? Then we don't need a function or PATH_IDENTIFIER, we can just use process.env.PATH.

We can write a setter for config.nodepath to add (or remove) node_modules/.bin to the PATH.

Let me know what your thoughts are for this approach. The downside is that this modifies a global, but the option is likely not going to be used often, and I can't think of a use for this option where modifying the PATH that way would be dangerous.

Although admittedly, that approach wouldn't be anything the users wouldn't be able to do themselves, so perhaps it's best to leave the code as-is.

@nfischer nfischer self-assigned this Nov 3, 2016

// windows calls it's path 'Path' usually, but this is not guaranteed.
if (process.platform === 'win32') {
PATH_IDENTIFIER = 'Path';
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this line necessary? It'll get overridden in the loop below.

@gyandeeps
Copy link
Copy Markdown
Contributor Author

My thoughts:

  • We can definitely change the PATH approach and do what ever you said.
  • Why I create a copy of path is because I dont want to change the path inside the whole nodejs process. Since its single process, me changing the path would/might impact other things in the environment. That was my thought process when designing this.

@nfischer
Copy link
Copy Markdown
Member

@gyandeeps sorry for taking so long to get back. Let's hold off on this, since I'm not completely sure what the best approach is, or if this is something we should delegate to ShellJS users instead.

I'm marking this as something to look into once we get closer to v0.8.

@freitagbr feel free to chime in with your thoughts on this feature.

@nfischer nfischer added this to the v0.8.0 milestone Nov 12, 2016
@nfischer
Copy link
Copy Markdown
Member

After some thought, this is probably the wrong approach to solve this problem. I think it's better to leave this to users, since it can easily be solved outside of ShellJS.

@nfischer nfischer closed this Feb 26, 2017
@gyandeeps gyandeeps deleted the issue469 branch February 26, 2017 15:28
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

Successfully merging this pull request may close these issues.

2 participants