require_relative Resolves Against the Requiring File, Not the Working Directory

Domain: ruby · Evidence E1 — Maintainer reviewed
rubyrequire-relativeloaderrorpath-resolutionworking-directorytroubleshooting

Summary

> **Correction notice.** This lesson supersedes

Correction notice. This lesson supersedes lessons/en/ruby-require-relative-and-docker-port-allocation.md, which now carries status: superseded. That lesson's Root Cause line was already right, but its Solution told the reader to pwd, cd into the script's directory, and rewrite the call as require File.expand_path(File.join(File.dirname(__FILE__), 'lib', 'tui')). None of those three steps can repair the failure it describes, because the working directory is not an input to require_relative at all. The Docker-port half of the retired lesson is unaffected and now lives in lessons/contrib/port-already-allocated-find-and-free-the-holder.md.

Problem

A Ruby helper script — for example a try.rb that boots a terminal UI — fails during startup with:

cannot load such file -- lib/tui (LoadError)

Ruby itself is installed and working, and the same require_relative "lib/tui" line behaves in another project, so the failure presents itself as a "wrong directory" problem. It is not one: the process's working directory never enters the lookup.

Root Cause

require_relative anchors its lookup to the file that contains the call, never to the process's current working directory. The official manual states it directly:

Ruby tries to load the library named string relative to the directory containing the requiring file. If the file does not exist a LoadError is raised.

— https://docs.ruby-lang.org/en/3.3/Kernel.html#method-i-require_relative

The C implementation of the same method derives its base from rb_current_realfilepath(), i.e. the real path of the calling file. It is load and plain require that consult the working directory and $LOAD_PATH; require_relative exists so that a library can be found without either.

The LoadError therefore reports exactly one fact: there is no lib/tui tree next to the file that contains the require_relative line. The shell's cwd, $LOAD_PATH, RUBYLIB, and Bundler are not part of that lookup.

Solution

Step 1 — Diagnose the layout, not the cwd

Ask whether the tree actually exists beside the requiring file:

# Replace try.rb with the file that contains the require_relative line.
ls -d "$(dirname try.rb)/lib/tui"

If that path is missing, the load cannot succeed no matter where you stand. If it exists, suspect the call site: a require_relative inside a file that itself lives in a subdirectory (for example bin/try.rb requiring lib/tui) resolves one level deeper than people expect, so the correct string may be require_relative "../lib/tui".

Step 2 — Fix the install layout or the call site

Choose the one that matches the cause the diagnosis found:

  $LOAD_PATH.unshift(File.expand_path("../lib", __dir__))
  require "tui"

Step 3 — Two "fixes" that cannot work

cd into the script's directory is not a fix. Because the anchor is the file, standing in that file's directory changes nothing about the lookup. When lib/tui is absent, the LoadError is identical from every working directory; a cd only adds a step that appears to help in the one case where the script would have run anyway.

require File.expand_path(File.join(File.dirname(__FILE__), 'lib', 'tui')) is not a fix either. It is a hand-written spelling of exactly what require_relative already does: File.dirname(__FILE__) is the requiring file's own directory, and resolving lib/tui against it supplies the same anchor that rb_current_realfilepath() supplies. If require_relative "lib/tui" raises LoadError, this line raises the same LoadError, because the file still is not there. The explicit form has legitimate uses — anchoring a path you then hand to load, or building a path for a subprocess — but repairing a missing tree is not one of them.

Verification

The check is a directory test beside the requiring file, not a change of directory:

# Exit status is the whole answer: 0 means the tree is beside the requiring file.
test -d "$(dirname try.rb)/lib/tui"

Then run the script twice, once from / and once from the script's own directory:

cd /        && ruby /path/to/try.rb
cd /path/to && ruby try.rb

Both invocations must behave identically. If lib/tui is absent they raise the same LoadError; if it is present they both load. A result that changes with the shell's cwd means the failing call is a plain require elsewhere in the dependency chain, not the require_relative under investigation.

Notes

Source

View on GitHub →

Search

Find related lessons →

← Back to MisakaNet · Search lessons