ruby/ruby · error · GemfileError

an empty gem name is not valid

Error message

an empty gem name is not valid

What it means

The third name check in normalize_options (lib/bundler/dsl.rb:470): a zero-length name is rejected immediately. Empty names almost never come from literal `gem ""` but from interpolation that produced nothing, so the message is the fastest way to discover that a variable feeding the Gemfile is nil or blank.

Source

Thrown at lib/bundler/dsl.rb:470

    def normalize_hash(opts)
      opts.keys.each do |k|
        opts[k.to_s] = opts.delete(k) unless k.is_a?(String)
      end
      opts
    end

    def valid_keys
      @valid_keys ||= VALID_KEYS
    end

    def normalize_options(name, version, opts)
      if name.is_a?(Symbol)
        raise GemfileError, %(You need to specify gem names as Strings. Use 'gem "#{name}"' instead)
      end
      if /\s/.match?(name)
        raise GemfileError, %('#{name}' is not a valid gem name because it contains whitespace)
      end
      raise GemfileError, %(an empty gem name is not valid) if name.empty?

      normalize_hash(opts)

      git_names = @git_sources.keys.map(&:to_s)
      validate_keys("gem '#{name}'", opts, valid_keys + git_names)

      groups = @groups.dup
      opts["group"] = opts.delete("groups") || opts["group"]
      groups.concat Array(opts.delete("group"))
      groups = [:default] if groups.empty?

      install_if = @install_conditionals.dup
      install_if.concat Array(opts.delete("install_if"))
      install_if = install_if.reduce(true) do |memo, val|
        memo && (val.respond_to?(:call) ? val.call : val)
      end

      platforms = @platforms.dup

View on GitHub (pinned to 0e5b888e1c)

Solutions

  1. Find the `gem` line whose interpolated variable is blank — log or print the inputs feeding the Gemfile
  2. Default or validate the value before use: `gem gem_name.to_s.strip` after raising on blank, or `ENV.fetch("GEM_NAME")` to fail with a clearer error
  3. For generated Gemfiles, add a blank-name assertion in the generator so the error points at your data, not Bundler

Example fix

# before
GEMS.each { |name, constraints| gem name, constraints } # name is "" for one row

# after
GEMS.each do |name, constraints|
  raise ArgumentError, "blank gem name in manifest row" if name.to_s.strip.empty?
  gem name, constraints
end
Defensive patterns

Strategy: validation

Validate before calling

# Fail with your own diagnostic when generated names are blank
GEMS.each do |name, req|
  raise ArgumentError, "blank gem name for requirements #{req.inspect}" if name.to_s.strip.empty?
  gem name, req
end

Prevention

When it happens

Trigger: `gem "#{gem_name}"` where gem_name is nil ("#{nil}" would give "nil" — but an unset hash key or empty string gives ""), loops like `GEMS.each { |g| gem g[:name] }` with a missing key, or ERB templates rendering an empty attribute.

Common situations: Templated/ERB-generated Gemfiles where a config value is missing in one environment; scripts reading a manifest that has blank lines mapped to empty strings; refactors that renamed a variable but not the symbol it reads from.

Related errors


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