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
Respect original PATH definitions on install #2589
Conversation
@mcornella, @robbyrussell can I get a review on this, I think it solves a few longstanding issues: |
Hi @avit, first of all, I'm not an "official" project mantainer, but it's an honor to be put on the same list :) My 2 cents for now:
More particularly about the PR:
I'll post more when I have that tested. |
Good points, thanks. I also agree with you on this, but #1359 was closed for similar reasons so I'm trying to offer a solution. If we're going to set PATH for new users, this approach would be less broken than the current setup. I agree it can be improved to look for ( |
Surprisingly, on my Debian system (testing), |
if [ "$ORIG_PATHS" ] | ||
then | ||
echo "\033[0;34mCopying your current PATH and adding it to the end of ~/.zshrc for you.\033[0m" | ||
ed tools/zshrc_test <<__EOT__ |
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 outputs tools/zshrc_test: file not found
. You probably meant ~/.zshrc
?
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.
Yes, thanks. That's an oversight from testing.
Wow, it's surprising that a system wouldn't have |
ed tools/zshrc_test <<__EOT__ | ||
g/export PATH=/ | ||
c | ||
$OLD_PATHS |
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 need to be $ORIG_PATHS
Cool! With those changes and |
|
http://wiki.bash-hackers.org/howto/edit-ed @mpapis Not arguing, I'll change it, but ed has been around forever. Just about every system has it in /bin/ed (except debian now has it "optional" as we've learned). ed is basically vi commands without the visual part, it's easier and clearer to understand for many things. sed is... different. |
I had another look at this today but I'm not too sure of the right way to do this in sed or awk. If we capture # captured output with newlines:
ORIG_PATHS='export PATH="$PATH:/foo/bar/baz"
export PATH="$PATH:/qux"
'
# attempted replacement, using '|' instead of '/' for delimiters due to slashes in paths:
sed -e 's|^export PATH=.*|'"$ORIG_PATHS"'|' zshrc
# sed: 1: "s|^export PATH=|export ...": unescaped newline inside substitute pattern Can anyone help with the quoting/escaping please? I also tried escaping using |
try: sed -e '/^export PATH=.*$/ c '"$ORIG_PATHS"'
' zshrc there is supposed to be new line ... but it might work without it too |
Hm. That doesn't quite work either. sed -e '/^export PATH/ c\
'"$ORIG_PATHS" zshrc But then the newlines in $ORIG_PATHS break it so it still needs more escaping. Now I have this double-sed which seems to work: sed -e '/^export PATH=/ c\
'"$(echo $ORIG_PATHS | sed -e 's/$/\\/')" zshrc But is that a sane way to do it? |
sed -e '/^export PATH=/ c\
'"${ORIG_PATHS//\n/\\\\n}" zshrc use shell variable replacement, not sure about the escapes in the second part - 3 or 4 should do it. |
@mpapis Thanks for your help, but I still can't seem to make it replace the newlines. This works with normal characters: echo "${ORIG_PATHS//x/AAA}" But does not catch the newlines: echo "${ORIG_PATHS//\n/AAA}" |
ah yes, this will do it: echo "${ORIG_PATHS//$'\n'/\\\\n}" |
but the last one will not work with dash (works fine with bash/zsh) so use it only if the script is run from zsh - you could add on top: #!/usr/bin/env zsh |
@mpapis I found a solution for replacing newlines that works across all the shells by putting the newline into a variable. I couldn't find a better way unless you have any other suggestions. # works in bash, sh but not in zsh
`"${ORIG_PATHS//$'\n'/\\$'\n'}"` |
ah yes I use the variable trick in rvm => https://github.com/wayneeseguin/rvm/blob/master/scripts/functions/support#L402-L403 |
@robbyrussell This should be good to merge, can you give a review? |
Not tested but looks good to me. However, can you please squash your commits to keep the history clean and atomic? |
Hey @avit, just tested it again and it fails when there are no export statements or I'm talking about line 28, and I was using bash. Grep returns a non-zero value when it doesn't find something, we can solve it adding |
Thanks, I'll have another look before we merge this then. |
I just wasted about an hour trying to figure out why my rbenv wasn't being loaded correctly before I realized Oh My Zsh was overwriting the |
@avit Hi there, have you had a chance to update this? |
@robbyrussell The maintainer lives! Yeah, I haven't yet but I'll look at it again this week. |
it only required other tool to find the same bug as reported 2 years ago |
@robbyrussell I rebased this on master & fixed to handle cases for missing files or missing PATH declarations. |
Hi @avit, whenever I have the time I'll test this again (I don't remember what tests I tried yet) |
To add to the sed portability discussion, I'd invite y'all to checkout my pull request to see how to accomplish your goals while still being compatible with operating systems that run standard sed, like OpenBSD or NetBSD. #3257 TL;DR - Standard sed doesn't have the "-i" option. |
The exporting of PATH as a fixed string would clobber any system-wide paths added to env after installation. Instead, we now read any existing PATH exports from previous config files and copy those from the user's bash config, or else default to a basic prepend-only example that respects the system environment.
Thanks for that @mitzip, I've updated my branch for this PR to include that. |
No problem @avit, though, @mpapis pointed out a potential problem in my PR. If the install file gets over 4096 bytes (it's 1900 or so now), it would overflow sed's buffer and cause the tee output to be corrupt. The solution is using a temp file, which is what GNU sed does when passed the "-i" option. I've updated my PR, I suggest you yours as well. :-) You were just too quick on the draw, :-D |
Ha, ok, that's good to know. I was wondering why not output redirection ( |
It seems a little wrong to copy the user's path to the Instead, why not append the additional directories to the existing path, like this?
Or am I missing something? Sorry if this is an ignorant comment. 😃 |
@LandonSchropp this is response to rejected #1359 - a middle ground, preserve |
So what's the status here? Is it ready for merge? |
I think it is but, to be honest, I haven't tested the newest versions of the patch. Will do this weekend hopefully |
Appreciate all of your work on getting us this far on this. Once we get a chance to do some tests, we'll get this merged in. |
@avit can we please get a rebase here ? |
Hmm. I don't think this use of This is a variation on what @mitzip said. When you do a pipeline like This needs to be replaced with temp files.
|
I'm not sure this change makes the $PATH situation better than before. On the one hand, it will preserve keeping But I think it's more likely to cause immediate breakage by overlooking path elements. In rc files, the path can be modified by several Consider this simplified example. Here are the config files before OMZ installation.
I end up with this in the
I appreciate the problem this is trying to solve, but I don't know that it's the right approach. It makes the path-determination-on-installation behavior more complicated than before, and also introduces a couple failure points that will cause immediate breakage upon installation for some users. This might be a step backward. IMHO the way to get full correctness with path definitions is probably to just preserve the user's entire And failing that, just slap an explicit dynamic |
I'm not sure if this is the best issue to add myself to but... After installing OMZ, rum was complaining: Here is the offending line from my ~/.zshrc
to fix, I simply prepended with $PATH; All Fixed :)
|
This is a solution for #1359.