title: "Could not find gem …" error with bundler using custom gem source
body: <p>In my Gemfile I require a gem from a custom source with this line:</p>
  
  <pre><code>gem 'very-secret-gem', source:'https://foo.example.com/'
  </code></pre>
  
  <p><code>bundle install</code> completes fine:</p>
  
  <pre><code>$ bundle install
  Fetching source index from https://foo.example.com/
  Fetching source index from https://foo.example.com/
  Fetching gem metadata from https://rubygems.org/........
  …
  Resolving dependencies...
  …
  Installing very-secret-gem 1.5.1
  …
  Bundle complete! 47 Gemfile dependencies, 116 gems now installed.
  Use `bundle show [gemname]` to see where a bundled gem is installed.
  </code></pre>
  
  <p>But running commands that use ruby fail (empty Rakefile here):</p>
  
  <pre><code>$ bundle exec rake -T
  Could not find gem 'very-secret-gem (&gt;= 0) ruby' in rubygems repository https://foo.example.com/.
  Source does not contain any versions of 'very-secret-gem (&gt;= 0) ruby'
  Run `bundle install` to install missing gems.
  </code></pre>
  
  <p>Running <code>bundle install</code> at this point as advised in the error message will not help.</p>
  
  <p><strong>Why is it, and how to fix it?</strong></p>
  
  <p>If I specify the gem in a source block, it fails just the same:</p>
  
  <pre><code>source 'https://foo.example.com/' do
    gem 'very-secret-gem'
  end
  </code></pre>
  
  <p>More interestingly, if I specify the sources at the beginning of the file, not tied to any gems, it works fine:</p>
  
  <pre><code>source 'https://rubygems.org'
  source 'https://foo.example.com/'
  gem 'very-secret-gem'
  </code></pre>
  
  <p>…but bundler advises against it:</p>
  
  <pre><code>Warning: this Gemfile contains multiple primary sources. Using `source`
  more than once without a block is a security risk, and may result in
  installing unexpected gems. To resolve this warning, use a block to
  indicate which gems should come from the secondary source. To upgrade
  this warning to an error, run `bundle config disable_multisource true`.
  </code></pre>
  
  <h3>Versions:</h3>
  
  <pre><code>$ ruby -v    # =&gt; ruby 2.2.0p0 (2014-12-25 revision 49005) [x86_64-darwin14]
  $ gem -v     # =&gt; 2.4.5
  $ bundle -v  # =&gt; Bundler version 1.8.2
  </code></pre>
  
  <h3>Update</h3>
  
  <p>Seems like a bundler bug. The presence of another <code>gem</code> with <code>:path</code> seems to be what triggers it. A test app is here: <a href="https://github.com/kch/bundler-source-bug" rel="noreferrer">https://github.com/kch/bundler-source-bug</a></p>
  
  <p>GH issue for bundler here: <a href="https://github.com/bundler/bundler/issues/3417" rel="noreferrer">https://github.com/bundler/bundler/issues/3417</a></p>