Project

General

Profile

Feature #12695

File.expand_path should resolve ~/ using /etc/passwd when HOME is not set

Added by fursten (MÃ¥rten Svantesson) over 2 years ago. Updated about 2 years ago.

Status:
Closed
Priority:
Normal
Assignee:
-
Target version:
-
[ruby-core:77019]

Description

I would expect a call like File.expand_path('~/.ssh/known_hosts') to work even though HOME is not set.

A reason this is important to fix is that libraries (in my use case Net::SSH) do calls like File.expand_path('~/.ssh/known_hosts'), but this will often fail when the code is run in a unix service, since HOME often isn't set then.
I considered reporting this as a bug, but the current behaviour is actually documented.

Associated revisions

Revision 6b88dd26
Added by nobu (Nobuyoshi Nakada) over 2 years ago

file.c: home directory from system

  • file.c (rb_default_home_dir): resolve home directory from the system database when HOME is not set. [Feature #12695]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@56902 b2dd03c8-39d4-4d8f-98ff-823fe69b080e

Revision 56902
Added by nobu (Nobuyoshi Nakada) over 2 years ago

file.c: home directory from system

  • file.c (rb_default_home_dir): resolve home directory from the system database when HOME is not set. [Feature #12695]

Revision 56902
Added by nobu (Nobuyoshi Nakada) over 2 years ago

file.c: home directory from system

  • file.c (rb_default_home_dir): resolve home directory from the system database when HOME is not set. [Feature #12695]

Revision 56902
Added by nobu (Nobuyoshi Nakada) over 2 years ago

file.c: home directory from system

  • file.c (rb_default_home_dir): resolve home directory from the system database when HOME is not set. [Feature #12695]

History

Updated by shyouhei (Shyouhei Urabe) over 2 years ago

+1 it makes sense to me.

Updated by nobu (Nobuyoshi Nakada) over 2 years ago

Why doesn't your service launcher set HOME?

Updated by akr (Akira Tanaka) over 2 years ago

I think its good idea.

Although POSIX doesn't specify it for shell, we can define it in Ruby.

Updated by knu (Akinori MUSHA) over 2 years ago

Makes sense. Ruby knows how to resolve ~user, so you may well expect it to resolve ~ as well given that the login name is identified.

Updated by knu (Akinori MUSHA) over 2 years ago

Bash seems to do that:

% bash -c 'unset HOME; echo ~'
/home/knu

As far as I tested most other known shells like tcsh, zsh, ash, dash and pdksh do not, but we could follow the best.

Through my testing, I found fish unique in that it automatically defines a missing HOME on startup and then expands ~ simply to $HOME. While it would keep things simple and efficient, Ruby is not a shell after all, and altering the environment would clearly be something unasked for.

I think following Bash should be the way to go.

#8

Updated by nobu (Nobuyoshi Nakada) over 2 years ago

  • Status changed from Open to Closed

Applied in changeset r56902.


file.c: home directory from system

  • file.c (rb_default_home_dir): resolve home directory from the system database when HOME is not set. [Feature #12695]

Updated by pabs (Paul Wise) about 2 years ago

Does anyone know if this also fixes the Dir.home value? Currently (in 2.3) that doesn't work when HOME is unset either.

pabs@chianamo ~ $ unset HOME
pabs@chianamo /home/pabs $ ruby -e 'print Dir.home'
-e:1:in `home': couldn't find HOME environment -- expanding `~' (ArgumentError)
        from -e:1:in `<main>'

Updated by nobu (Nobuyoshi Nakada) about 2 years ago

This is a new feature of 2.4.

Also available in: Atom PDF