oh-my-fish / plugin-foreign-env

Run foreign bash scripts and capture exported environment variables
MIT License
221 stars 15 forks source link

~/.bashrc is not properly sourced even when --login is passed #16

Open ghost opened 5 years ago

ghost commented 5 years ago

This was a rather difficult one to track down: some distributions have a special clause in their ~/.bashrc that looks something like this (taken from this gist):

# ~/.bashrc: executed by bash(1) for non-login shells.
# see /usr/share/doc/bash/examples/startup-files (in the package bash-doc)
# for examples

# If not running interactively, don't do anything
case $- in
    *i*) ;;
      *) return;;
esac

# rest of file with some modifications to $PATH ...

This answer on ServerFault explains in detail why that is, and from the looks of it, for the sake of backwards-compatibility, it isn't going to go away very soon. In my case, it means that the command nvm could not be found, even though my .bashrc was set up correctly.

I propose a small change to the documentation to notify people who are having trouble with this. You can do the following and get it working completely:

# ~/.bashrc: executed by bash(1) for non-login shells.
# see /usr/share/doc/bash/examples/startup-files (in the package bash-doc)
# for examples

# If not running interactively, don't do anything
# Adds a special case for when we really do want to modify $PATH
case $- in
    *i*) ;;
      *) [ -z "$FORCE_LOGIN" ] && return;;
esac

Next, wrap the command you would like to run in a function, like so:


function nvm
  begin
    set -lx FORCE_LOAD 1
    fenv nvm $argv
  end
end

Until fish functions are able to accept environment variables, I'm afraid that's the best I can come up with.

ghost commented 5 years ago

By the way: thanks for the plugin! I really did not want to write one myself, and with some slight modifications (see #15) it works like a charm! 👍

ghost commented 5 years ago

Hmm seems that I was too quick ... nvm does not modify my $PATH variable and can't find node while it works in plain bash. Maybe it is too good to be true after all ...