Skip to content

Conversation

ghost
Copy link

@ghost ghost commented Jan 1, 2016

I just rebased david’s patch and applied your comments.

Let me know if there are any other changes that are needed to accept this.

Thank you!

@ljharb
Copy link
Member

ljharb commented Jan 1, 2016

Linking to #833

# But $PROFILE should override
_PROFILE="$(PROFILE=test_profile nvm_detect_profile)"
_PROFILE=$(nvm_detect_profile)
[ "_$_PROFILE" = "_$PROFILE" ] || ( echo "_\$_PROFILE: _$_PROFILE" &&
Copy link
Member

Choose a reason for hiding this comment

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

this reads pretty confusing to me - could we rename _PROFILE to DETECTED_PROFILE or something?

@ljharb ljharb added needs followup We need some info or action from whoever filed this issue/PR. installing nvm: profile detection Issues with nvm's own install script, related to figuring out the right profile file. labels Jan 1, 2016
#

# .bashrc should be detected for bash
NVM_DETECT_PROFILE="$(declare SHELL="/bin/bash"; unset PROFILE; nvm_detect_profile)"
Copy link
Member

Choose a reason for hiding this comment

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

just curious, why declare instead of SHELL=/bin/bash PROFILE= nvm_detect_profile?

Copy link
Author

Choose a reason for hiding this comment

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

I was trying to figure out how to set variables inside a command substitution and for some reason that was the way I found. I'm not totally sure of the implications, using declare seems ok. Retrying it now, I think I could have just added semicolons and didn't have to use declare as the following shows.

$ TEST="123"; echo $(TEST="ABC" echo $TEST); echo $TEST
123
123
$ TEST="123"; echo $(TEST="ABC"; echo $TEST); echo $TEST
ABC
123

Copy link
Member

Choose a reason for hiding this comment

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

It'd be ideal to simplify where possible. generally, you can set env vars for the duration of a command like TEST="ABC" echo $TEST without having to use separate statements. does that work here?

@ghost
Copy link
Author

ghost commented Jan 2, 2016

I tried to keep the same logic and rewrote the tests. The [ condition ] || die parts seemed to never trigger urchin when they failed. Let me know if my style is ok, I'm not really a bash programmer.

@ljharb
Copy link
Member

ljharb commented Jan 2, 2016

The "if" approach is fine with me, altho the || approach should be identical assuming the conditions are the same.

@ghost
Copy link
Author

ghost commented Jan 2, 2016

I removed the unnecessary usage of declare. Everything else is ok?

fi

# $PROFILE should override .bashrc profile detection
NVM_DETECT_PROFILE="$(SHELL="/bin/bash"; PROFILE="test_profile"; nvm_detect_profile)"
Copy link
Member

Choose a reason for hiding this comment

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

do these not work without the semicolons? I'd expect just SHELL="/bin/bash" PROFILE="test_profile" nvm_detect_profile to work.

@ghost
Copy link
Author

ghost commented Jan 2, 2016

Some of the tests will not return an error even if the semicolon is left out due to side-effects. Though, to obtain the intended logic the semicolons are needed.

# $PROFILE is not a valid file
# In this case no profile is set and nvm_detect_profile returns an empty value,
# still resulting in the test passing
rm "test_profile"
NVM_DETECT_PROFILE="$(PROFILE="test_profile" nvm_detect_profile)"
if [ "$NVM_DETECT_PROFILE" = "test_profile" ]; then
  die "nvm_detect_profile picked \$PROFILE when it was an invalid file"
fi

Play around with the following code in your shell. The command substitution runs in a subshell containing its parent's variables, though, its parent doesn't have access to the child processes variables. As utilized in the tests, command substitutions are replaced by their stdout.

$ TEST="123"; echo $(TEST="ABC" echo $TEST); echo $TEST
123
123
$ TEST="123"; echo $(TEST="ABC"; echo $TEST); echo $TEST
ABC
123

Also, you can try to make the tests fail to confirm they are not passing due to a side-effect. That is what I did.

I'm still trying to learn more about shell and bash programming, there may still be some minor issues, though, I think less than before. I found this article covers a number of potential gotchas.

@ljharb ljharb removed the needs followup We need some info or action from whoever filed this issue/PR. label Jan 2, 2016
ljharb added a commit that referenced this pull request Jan 2, 2016
[Fix] `install.sh`: honor PROFILE var.

Closes #888. Fixes #830.
@ljharb ljharb merged commit 1ba8a96 into nvm-sh:master Jan 2, 2016
@ljharb
Copy link
Member

ljharb commented Jan 2, 2016

Thanks!

@ghost ghost mentioned this pull request Jan 18, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
installing nvm: profile detection Issues with nvm's own install script, related to figuring out the right profile file.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants