`bundle add <gem> --version ...` modifies `Gemfile` before failing when resolution fails

Open Beginner friendly
#10,082 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
78/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
ruby
Domain
backend, cli

Research direction

Read lib/bundler/injector.rb, especially Bundler::Injector#inject, build_gem_lines(false), and the append_to and @definition.lock calls described in the issue. Trace the versioned bundle add path and compare it with the path that resolves before modifying the Gemfile. Reproduce with the provided local gem source; done means a failed resolution leaves the Gemfile unchanged with or without --version.

Written by the indexing model from the issue text.

Description

Describe the problem as clearly as you can

When bundle add <gem> fails because the requested gem cannot be found (exit code 7), Gemfile is left unchanged. However, when --version is passed (bundle add <gem> --version "~> 1.0"), Bundler appends the broken gem line to Gemfile before raising Bundler::GemNotFound, leaving Gemfile in an unresolvable state for subsequent bundle commands.

In Bundler::Injector#inject (lib/bundler/injector.rb), @definition.remotely! only configures remote sources lazily, and build_gem_lines(false) (used when --version is given) does not access @definition.specs, so append_to writes to Gemfile before @definition.lock runs resolution.

Did you try upgrading rubygems & bundler?

Yes. Reproduced on RubyGems / Bundler 4.1.0 (Ruby 4.0.9) and on ruby/rubygems master (5879ffe34b, 4.2.0.dev).

Post steps to reproduce the problem
mkdir repro-bundle-add && cd repro-bundle-add
mkdir -p repo/gems repo/quick/Marshal.4.8 app

ruby -rzlib -e '
  File.binwrite("repo/specs.4.8.gz", Zlib.gzip(Marshal.dump([])))
  File.binwrite("repo/prerelease_specs.4.8.gz", Zlib.gzip(Marshal.dump([])))
'

cat > app/Gemfile <<GEMFILE
source "file://$PWD/repo"
GEMFILE

cd app
BUNDLE_PATH=vendor/bundle bundle add nonexistent || echo "exit=$?"
cat Gemfile

BUNDLE_PATH=vendor/bundle bundle add nonexistent --version "~> 1.0" || echo "exit=$?"
cat Gemfile
Which command did you run?

bundle add nonexistent --version "~> 1.0"

What were you expecting to happen?

When bundle add fails resolution with exit code 7, Gemfile should remain unchanged regardless of whether --version was passed, matching bundle add nonexistent without --version and the comment in Bundler::Injector#inject ("resolve to see if the new deps broke anything" before "since nothing broke, we can add those gems to the gemfile").

What happened instead?
$ BUNDLE_PATH=vendor/bundle bundle add nonexistent || echo "exit=$?"
Fetching source index from file:///tmp/repro-bundle-add/repo/
Could not find gem 'nonexistent' in rubygems repository
file:///tmp/repro-bundle-add/repo/ or installed locally.
exit=7

$ cat Gemfile
source "file:///tmp/repro-bundle-add/repo"

$ BUNDLE_PATH=vendor/bundle bundle add nonexistent --version "~> 1.0" || echo "exit=$?"
Fetching source index from file:///tmp/repro-bundle-add/repo/
Could not find gem 'nonexistent (~> 1.0)' in rubygems repository
file:///tmp/repro-bundle-add/repo/ or installed locally.
exit=7

$ cat Gemfile
source "file:///tmp/repro-bundle-add/repo"

gem "nonexistent", "~> 1.0"
If not included with the output of your command, run bundle env and paste the output below
## Environment

```
Bundler       4.1.0
  Platforms   ruby, x86_64-linux
Ruby          4.0.9p0 (2026-10-23 revision 3e93b23afcec3433f6aecc34b5d832799d754238) [x86_64-linux]
  Full Path   /opt/ruby-4.0.9/bin/ruby
  Config Dir  /opt/ruby-4.0.9/etc
RubyGems      4.1.0
  Gem Home    /opt/ruby-4.0.9/lib/ruby/gems/4.0.0
  Gem Path    /home/user/.local/share/gem/ruby/4.0.0:/opt/ruby-4.0.9/lib/ruby/gems/4.0.0
  User Home   /home/user
  User Path   /home/user/.local/share/gem/ruby/4.0.0
  Bin Dir     /opt/ruby-4.0.9/bin
Git           2.39.5
```

## Bundler Build Metadata

```
Timestamp  2026-11-16
Git SHA    9af9267
```

## Bundler settings

```
path
  Set via BUNDLE_PATH: "/tmp/repro-bundle-add/app/vendor/bundle"
```

## Gemfile

### Gemfile

```ruby
source "file:///tmp/repro-bundle-add/repo"

gem "nonexistent", "~> 1.0"
```

### Gemfile.lock

```
<No /tmp/repro-bundle-add/app/Gemfile.lock found>
```

Credit: https://ucsbplse.github.io/

Dominant language
Ruby
Stars
4k
Forks
2k
Avg merge
1d 20h
Merged PRs (30d)
75

Getting set up

Open in Codespaces

Starts the project's dev container in your browser, under your own GitHub account.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from ruby/rubygems

All issues in ruby/rubygems

Similar issues

More Ruby issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.