Skip to content
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

Error when expanding path with ~ and no HOME #5661

headius opened this Issue Mar 21, 2019 · 1 comment


None yet
1 participant
Copy link

headius commented Mar 21, 2019

When expanding a path starting with a tilde, like ~/foo, if the HOME environment variable has been removed from ENV we raise an error. MRI does not, and must be using some fallback logic to expand HOME properly when it has been cleared (or else capturing it at startup?).

[] ~/projects/jruby $ rvm ruby-2.5.3 do ruby -e 'ENV.delete("HOME"); p ENV["HOME"]; p File.expand_path "~/foo.rb"'

[] ~/projects/jruby $ ruby -e 'ENV.delete("HOME"); p ENV["HOME"]; p File.expand_path "~/foo.rb"'
ArgumentError: couldn't find HOME environment -- expanding `~'
  expand_path at org/jruby/
       <main> at -e:1

This manifests on current master and has probably been this way for a long time.

Note that we have logic for expanding the home path in various places that should be checked for this behavior:

  • RubyFile.expand_path and similar that eventually attempt to expand a tilde to a HOME path.
  • RubyDir.getHomeDirectoryPath used by some of the above, which has several fallbacks to try to find a HOME but seems to not have quite enough?
  • Load logic within LoadService and LibrarySearcher. I'm currently working on a refactor of these, so the code is in flux. The logic I am now calling lives near the logic in RubyDir but simple returns null if the HOME environment variable cannot be resolved, with no fallbacks.

This comment has been minimized.

Copy link
Member Author

headius commented Mar 21, 2019

The logic for at least some of the File class methods appears to be in file.c, in rb_home_dir_of.

  • Use any chars after the ~ to as username to look for user-specific homes.
  • If that name matches getlogin()...
    • We are using current user's home. Retrieve HOME using getenv().
    • If that is not null, return it.
    • Use getpwnam() to get a pwPtr for username. If that is non-null...
      • Use pwPtr->pw_dir for HOME.
  • If none of this produces a HOME dir, raise an error.

The logic we have in RubyDir, used by RubyFile, currently also checks the user.dir Java property and the LOGDIR environment variable (strange?) so we'll have to negotiate whether those are appropriate in all cases.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.