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

Abierto Apto para principiantes
#10,082 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Los mantenedores suelen responder en 1 día

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
2/5
Tiempo estimado
1-3 horas
Aptitud para principiantes
78/100
Tipo de issue
Error
Claridad
Bien especificado
Estado de actividad
Activo
Stack tecnológico
ruby
Área
backend, cli

Línea de trabajo

Lee lib/bundler/injector.rb, especialmente Bundler::Injector#inject, build_gem_lines(false) y las llamadas a append_to y @definition.lock descritas en el issue. Sigue la ruta de bundle add con versión y compárala con la ruta que resuelve la dependencia antes de modificar el Gemfile. Reproduce el problema con la fuente local de gems proporcionada; el trabajo está terminado cuando una resolución fallida deja el Gemfile sin cambios, con o sin --version.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

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/

Lenguaje dominante
Ruby
Estrellas
4k
Forks
2k
Merge medio
1 d 20 h
PR fusionados (30 d)
75

Preparar el entorno

Abrir en Codespaces

Inicia el contenedor de desarrollo del proyecto en tu navegador, con tu propia cuenta de GitHub.

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de ruby/rubygems

Todos los issues de ruby/rubygems

Issues similares

Más issues de Ruby

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.