ruby/ruby · error · Bundler::Plugin::PluginGemfileError

29

29

Error message

Undefined local variable or method `#{name}' for Gemfile

What it means

Plugin::Dsl pre-parses the Gemfile to collect source plugins while ignoring regular `gem` calls; its method_missing accepts a name only if Bundler::Dsl defines that method (method_defined? check), otherwise it raises PluginGemfileError 'Undefined local variable or method `<name>' for Gemfile'. Only standard Gemfile DSL verbs survive the plugins pre-parse.

Source

Thrown at lib/bundler/plugin/dsl.rb:34

      #
      # When we encounter :type attribute with a source block, we add a plugin
      # by name bundler-source-<type> to list of plugins to be installed.
      #
      # These plugins are optional and are not installed when there is conflict
      # with any other plugin.
      attr_reader :inferred_plugins

      def initialize
        super
        @inferred_plugins = [] # The source plugins inferred from :type
      end

      def gem(*args)
        # Ignore regular dependencies when doing the plugins-only pre-parse
      end

      def method_missing(name, *args)
        raise PluginGemfileError, "Undefined local variable or method `#{name}' for Gemfile" unless Bundler::Dsl.method_defined? name
      end

      def source(source, *args, &blk)
        options = args.last.is_a?(Hash) ? args.pop.dup : {}
        options = normalize_hash(options)
        return super unless options.key?("type")

        plugin_name = "bundler-source-#{options["type"]}"

        return if @dependencies.any? {|d| d.name == plugin_name }

        plugin(plugin_name)
        @inferred_plugins << plugin_name
      end
    end
  end
end

View on GitHub (pinned to 0e5b888e1c)

Solutions

  1. Rewrite the offending call with standard DSL: `gem`, `source`, `group`, `platforms`, `env` only if defined by Dsl, etc.
  2. Define helper methods inside the Gemfile itself (before first use) so they resolve as instance methods during evaluation.
  3. Run `bundle install` - the normal (non-plugin) Dsl error message will point at the same line with better context.
  4. If the construct came from a plugin that provided DSL extensions, ensure that plugin is actually installed/enabled before its syntax is used.

Example fix

# Gemfile
# before - helper the plugin pre-parse doesn't know
env :production do
  gem "newrelic_rpm"
end

# after - standard DSL
group :production do
  gem "newrelic_rpm"
end
Defensive patterns

Strategy: type-guard

Validate before calling

# scan the Gemfile for calls the plugin pre-parse will reject
source = File.read("Gemfile")
calls = source.scan(/^\s*([a-z_][a-zA-Z0-9_?!]*)[\s(]/).flatten.uniq
standard = %w[source gem group platforms plugin git path env_if ruby install_if
gemspec eval_gemfile path! git! source!] 
unknown = calls - standard - Bundler::Dsl.instance_methods.map(&:to_s)
abort "non-DSL calls in Gemfile: #{unknown.join(', ')}" unless unknown.empty?

Type guard

# guard used by the plugin Dsl itself - reuse it before evaluating a Gemfile
known_to_dsl = ->(name) { Bundler::Dsl.method_defined?(name) }

%w[github env custom_helper].each do |candidate|
  warn "#{candidate} will raise PluginGemfileError" unless known_to_dsl[candidate.to_sym]
end

Try / catch

begin
  Bundler::Plugin.eval_gemfile(gemfile_path)
rescue Bundler::Plugin::PluginGemfileError => e
  name = e.message[/method `(\S+)' for Gemfile/, 1]
  abort "replace #{name} with standard Gemfile DSL (gem/source/group/platforms)"
end

Prevention

When it happens

Trigger: The Gemfile calls a helper or extension that Bundler::Dsl does not define - e.g. a leftover `env :production do`, a custom `github "..."` helper macro, `system "..."`, or a typo'd keyword - and plugin evaluation runs over the file.

Common situations: Gemfiles that rely on application-level helpers or metaprogramming defined outside the file; copying Gemfile snippets from older setups with DSL extensions; enabling the plugins feature (BUNDLE_PLUGINS / `bundle plugin`) makes the strict pre-parse run where it previously didn't.

Related errors


AI-assisted analysis of ruby/ruby@0e5b888e1c (2026-08-21). Data as JSON: /api/errors/9355cbd8f6664a04. Report an issue: GitHub.