ruby/rubygems · error · GemfileNotFound

#{gemfile} not found

Error message

#{gemfile} not found

What it means

Bundler::Definition.build is the entry point that turns a Gemfile plus optional lockfile into a Definition via Dsl.evaluate. It expands the gemfile path and requires an existing regular file (gemfile.file?), raising Bundler::GemfileNotFound with the expanded path otherwise (lib/bundler/definition.rb:39). The check runs before the Gemfile is parsed, so it is purely a path-existence failure: wrong path, dangling symlink, a directory, or a BUNDLE_GEMFILE pointing nowhere.

Source

Thrown at lib/bundler/definition.rb:39

      :platforms,
      :ruby_version,
      :lockfile,
      :gemfiles,
      :sources
    )

    # Given a gemfile and lockfile creates a Bundler definition
    #
    # @param gemfile [Pathname] Path to Gemfile
    # @param lockfile [Pathname,nil] Path to Gemfile.lock
    # @param unlock [Hash, Boolean, nil] Gems that have been requested
    #   to be updated or true if all gems should be updated
    # @return [Bundler::Definition]
    def self.build(gemfile, lockfile, unlock)
      unlock ||= {}
      gemfile = Pathname.new(gemfile).expand_path

      raise GemfileNotFound, "#{gemfile} not found" unless gemfile.file?

      Plugin.hook(Plugin::Events::GEM_BEFORE_EVAL, gemfile, lockfile)
      Dsl.evaluate(gemfile, lockfile, unlock).tap do |definition|
        Plugin.hook(Plugin::Events::GEM_AFTER_EVAL, definition)
      end
    end

    #
    # How does the new system work?
    #
    # * Load information from Gemfile and Lockfile
    # * Invalidate stale locked specs
    #  * All specs from stale source are stale
    #  * All specs that are reachable only through a stale
    #    dependency are stale.
    # * If all fresh dependencies are satisfied by the locked
    #  specs, then we can try to resolve locally.
    #

View on GitHub (pinned to 86cbb817a3)

Solutions

  1. Verify the path exists: `ls -l "$BUNDLE_GEMFILE"` or check ./Gemfile in the current directory
  2. Fix or unset BUNDLE_GEMFILE so it points at the real file: `export BUNDLE_GEMFILE=$(pwd)/Gemfile`
  3. In Docker, COPY the Gemfile (and lockfile) into the image before any RUN bundle step
  4. When calling Definition.build programmatically, guard with File.file?(gemfile) and pass an absolute path

Example fix

# before
ENV["BUNDLE_GEMFILE"] = "/app/Gemfile.production" # does not exist
Bundler::Definition.build(ENV["BUNDLE_GEMFILE"], nil, {})
# => /app/Gemfile.production not found

# after
ENV["BUNDLE_GEMFILE"] = "/app/Gemfile"
Bundler::Definition.build(ENV["BUNDLE_GEMFILE"], nil, {})
Defensive patterns

Strategy: validation

Validate before calling

gemfile = Pathname.new(ENV.fetch("BUNDLE_GEMFILE", Dir.pwd + "/Gemfile")).expand_path
unless gemfile.file?
  abort "Gemfile not found at #{gemfile}; fix BUNDLE_GEMFILE or cwd"
end
definition = Bundler::Definition.build(gemfile, gemfile.sub_ext(".lock"), {})

Type guard

# Predicate narrowing before calling the API
def loadable_gemfile?(path)
  p = Pathname.new(path).expand_path
  p.file? && p.readable?
end

definition = Bundler::Definition.build(gf, lf, {}) if loadable_gemfile?(gf)

Try / catch

begin
  definition = Bundler::Definition.build(gemfile, lockfile, unlock)
rescue Bundler::GemfileNotFound => e
  abort "bad bundle path: #{e.message}"
end

Prevention

When it happens

Trigger: BUNDLE_GEMFILE set to a nonexistent file (for example /app/Gemfile.production when only /app/Gemfile exists); calling Bundler::Definition.build(gemfile, lockfile, unlock) from Ruby with a bad path; deploy containers where the Gemfile was never COPYied to the location the env var names.

Common situations: Docker or CI images missing the Gemfile at the expected path; deploy configs referencing a templated Gemfile name that was never created; scripts run from a different working directory with stale absolute paths; repo restructuring breaking hardcoded paths.

Related errors


AI-assisted analysis of ruby/rubygems@86cbb817a3 (2026-08-23). Data as JSON: /api/errors/a9a0eacf92ccce80. Report an issue: GitHub.