load path

I

Iain Barnett

Hi,

I've discovered that the load path is different if I run sudo, =
specifically it doesn't pick up the value in GEM_HOME, even though `sudo =
echo $GEM_HOME` shows the correct path. Everything else I've checked =
between sudo/not has been identical, so I was wondering where the load =
path is originally set or when it's first picked up?=20

I'm running ruby v1.9.2 on OSX 10.6. All the info below is identical, =
except for the missing path in $: in `sudo irb`

echo $GEM_HOME
/Users/iainuser/.gems

sudo echo $GEM_HOME
/Users/iainuser/.gems

irb
puts $:
 
B

Brian Candler

Iain said:
I've discovered that the load path is different if I run sudo,
specifically it doesn't pick up the value in GEM_HOME, even though `sudo
echo $GEM_HOME` shows the correct path.

sudo clears the environment. When you type

sudo echo $GEM_HOME

then the whole command line is expanded by the shell (including
replacing $GEM_HOME by the contents of the environment variable) before
it is run, and hence sudo echo gets passed the already-expanded string.

Try these:

sh -c 'echo $GEM_HOME'
sudo sh -c 'echo $GEM_HOME'

to get the true picture.

However you can configure sudo to pass through certain environment
variables (see 'man 5 sudoers'), or you can do

sudo env GEM_HOME=$GEM_HOME gem install foo

The same question was asked recently at
http://www.ruby-forum.com/topic/214260
so there may be some more pointers in there.

Regards,

Brian.
 
I

Iain Barnett

Thanks for the responses.


I tried that just before I mailed in and it didn't work :(

Try these:
=20
sh -c 'echo $GEM_HOME'
sudo sh -c 'echo $GEM_HOME'
=20
to get the true picture.

That gave me the same results as before, unfortunately. I'm a bit =
perplexed as the env variables are being passed through but the load =
path isn't updated and is still missing the one line I want.=20


I don't seem to be able to find how the load path is built =
automatically, only stuff on how I can alter it after the fact, which =
won't help me in the long term.

Is there somewhere I can just add the path hardcoded? I don't really =
care about configurability, I'm not moving my gems anywhere else soon.


Regards,
Iain=
 
R

Ryan Davis

Thanks for the responses.
=20
On 2 Sep 2010, at 10:33, Ryan Davis wrote:
=20
=20
I tried that just before I mailed in and it didn't work :(

Works for me:

502 % grep -A1 ryand /etc/sudoers
# Added by ryand:
Defaults env_keep +=3D "GEM_PATH GEM_HOME"

You need to pay attention to where you put that. It HAS to be after the =
reset_env line or it obviously won't work.
Last login: Thu Sep 2 18:34:01 on ttys002
501 % env | grep GEM
GEM_PATH=3D/Library/Ruby/Gems/1.8
502 % sudo bash
501 % env | grep GEM
GEM_PATH=3D/Library/Ruby/Gems/1.8


btw... are you sure you've exported your env vars?
...
=20
Is there somewhere I can just add the path hardcoded? I don't really =
care about configurability, I'm not moving my gems anywhere else soon.

Fix it correctly or don't fix it at all. Anything else is just a =
headache delayed.
 
B

Brian Candler

Iain said:
Is there somewhere I can just add the path hardcoded?

At the top of your app you could do

$LOAD_PATH.unshift ENV['GEM_HOME'] unless $LOAD_PATH.include?
ENV['GEM_HOME']

for a nasty workaround.

As for how $LOAD_PATH is built: well, it's in ruby.c (look for incpush
and push_include), but the bits you're looking at are probably added by
rubygems.
 
I

Iain Barnett

Iain said:
Is there somewhere I can just add the path hardcoded?
=20
At the top of your app you could do
=20
$LOAD_PATH.unshift ENV['GEM_HOME'] unless $LOAD_PATH.include?=20
ENV['GEM_HOME']
=20
for a nasty workaround.

I shudder less at this
=20
As for how $LOAD_PATH is built: well, it's in ruby.c (look for incpush=20=
and push_include), but the bits you're looking at are probably added = by=20
rubygems.

than at the thought of dealing with C :)


=20
502 % grep -A1 ryand /etc/sudoers
# Added by ryand:
Defaults env_keep +=3D "GEM_PATH GEM_HOME"
=20
You need to pay attention to where you put that. It HAS to be after =
the reset_env line or it obviously won't work.

I don't have a reset line, but here's my sudoers.

# sudoers file.
#
# This file MUST be edited with the 'visudo' command as root.
#
# See the sudoers man page for the details on how to write a sudoers =
file.
#

# Host alias specification

# User alias specification

# Cmnd alias specification

# Defaults specification
Defaults env_keep +=3D "GEM_PATH GEM_HOME"

# Runas alias specification

# User privilege specification
root ALL=3D(ALL) ALL
%admin ALL=3D(ALL) ALL
%iainuser ALL=3D(ALL) ALL

# Uncomment to allow people in group wheel to run all commands
# %wheel ALL=3D(ALL) ALL

# Same thing without a password
# %wheel ALL=3D(ALL) NOPASSWD: ALL

# Samples
# %users ALL=3D/sbin/mount /cdrom,/sbin/umount /cdrom
# %users localhost=3D/sbin/shutdown -h now

=20
=20
=20
btw... are you sure you've exported your env vars?

I used the method Brian Candler suggested in the other mail, but I get =
the same with the one you've used here:

sudo zsh
env
(blah blah blah...)
GEM_HOME=3D/Users/iainuser/.gems
GEM_PATH=3D/Users/iainuser/.gems


sh -c 'echo $GEM_HOME'
/Users/iainuser/.gems

sudo sh -c 'echo $GEM_HOME'
/Users/iainuser/.gems

sudo sh -c 'env'
(blah blah blah...)
GEM_HOME=3D/Users/iainuser/.gems
HOME=3D/Users/iainuser
GEM_PATH=3D/Users/iainuser/.gems


Here's a relevant portion of my zprofile, with recent additions =
included:

export RUBY_PLATFORM=3Ddarwin
export PLATFORM=3D"$RUBY_PLATFORM"
export RUBY_VERSION=3D1.9
export GEM_HOME=3D"$HOME/.gems"
export RUBYLIB=3D"$GEM_HOME"
export GEM_PATH=3D"$GEM_HOME"
export RUBYOPT=3Drubygems
care about configurability, I'm not moving my gems anywhere else soon.
=20
Fix it correctly or don't fix it at all. Anything else is just a = headache delayed.
=20

I agree, but sometimes you just want a headache tablet. Or anything that =
comes on prescription, I hear that's what all the top celebrities are =
doing nowadays. Except for Paris Hilton. Not sure if she counts as a top =
celebrity though. If she'd been caught with several prescriptions I =
might change my mind about her...

Regards,
Iain
 
I

Iain Barnett

=20
$LOAD_PATH.unshift ENV['GEM_HOME'] unless $LOAD_PATH.include?=20
ENV['GEM_HOME']

Is there a way I can get this to run every time anything goes through =
ruby, not just things I've written?

Regards,
Iain=
 
R

Ryan Davis

I don't have a reset line, but here's my sudoers.
=20
# sudoers file.

Here is what my /etc/sudoers has. It has only the one documented =
molestation by myself. Everything else is _standard_ to Mac OSX:
# Defaults specification
Defaults env_reset
Defaults env_keep +=3D "BLOCKSIZE"
Defaults env_keep +=3D "COLORFGBG COLORTERM"
Defaults env_keep +=3D "__CF_USER_TEXT_ENCODING"
Defaults env_keep +=3D "CHARSET LANG LANGUAGE LC_ALL LC_COLLATE = LC_CTYPE"
Defaults env_keep +=3D "LC_MESSAGES LC_MONETARY LC_NUMERIC = LC_TIME"
Defaults env_keep +=3D "LINES COLUMNS"
Defaults env_keep +=3D "LSCOLORS"
Defaults env_keep +=3D "SSH_AUTH_SOCK"
Defaults env_keep +=3D "TZ"
Defaults env_keep +=3D "DISPLAY XAUTHORIZATION XAUTHORITY"
Defaults env_keep +=3D "EDITOR VISUAL"
=20
# Added by ryand:
Defaults env_keep +=3D "GEM_PATH GEM_HOME"

So, did you gut yours, or what? what's the output from `which sudo`?
# User privilege specification
root ALL=3D(ALL) ALL
%admin ALL=3D(ALL) ALL
%iainuser ALL=3D(ALL) ALL

you shouldn't need %iainuser either if your user is set up as an admin.
Here's a relevant portion of my zprofile, with recent additions = included:
=20
export RUBY_PLATFORM=3Ddarwin
export PLATFORM=3D"$RUBY_PLATFORM"
export RUBY_VERSION=3D1.9
export GEM_HOME=3D"$HOME/.gems"
export RUBYLIB=3D"$GEM_HOME"
export GEM_PATH=3D"$GEM_HOME"
export RUBYOPT=3Drubygems

Here is what I have:
501 % env | egrep "RUBY|GEM"
GEM_PATH=3D/Library/Ruby/Gems/1.8

Why do you have all that stuff? and why are you trying to force gems =
into ~/.gems if that's supported already?

What are you actually trying to do?
 
B

Brian Candler

Iain said:
$LOAD_PATH.unshift ENV['GEM_HOME'] unless $LOAD_PATH.include?
ENV['GEM_HOME']

Is there a way I can get this to run every time anything goes through
ruby, not just things I've written?

Well, there's

export RUBYOPT=-I/Users/iainuser/.gems
or
export RUBYOPT=-rmyfrig

(and then put your code in myfrig.rb somewhere which is always in
$LOAD_PATH,
like
/Library/Frameworks/Ruby.framework/Versions/1.9.2-p0/lib/ruby/site_ruby/1.9.1
)

As long as RUBYOPT propagates of course.

For a more fundamental question, why do you want /Users/iainuser/.gems
in your $LOAD_PATH anyway? Surely you want the lib dirs of the
individual gems, and you wouldn't/shouldn't put ruby files at the top
level?

B.
 
I

Iain Barnett

Finally got back to this, and for whatever reason, it just works now, so =
something I did before I was away has fixed it.

Many thanks to Brian and Ryan for helping me out with this, I can now =
remove a lot of those hacks I put in until it stops working and find out =
why.


Regards,
Iain=
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,128
Latest member
ElwoodPhil
Top