lostisland / faraday

Simple, but flexible HTTP client library, with support for multiple backends.
https://lostisland.github.io/faraday
MIT License
5.71k stars 972 forks source link

Multiple "already initialized constant" warnings #1495

Closed richcorbs-ncino closed 1 year ago

richcorbs-ncino commented 1 year ago

Basic Info

Issue description

When there are multiple gems in our project that depend on Faraday we get multiples of these kinds of warnings when starting the server:

/Users/me/.rbenv/versions/3.0.4/lib/ruby/gems/3.0.0/gems/faraday-multipart-1.0.4/lib/faraday/multipart.rb:15: warning: already initialized constant Faraday::FilePart
/Users/me/.rbenv/versions/3.0.4/lib/ruby/gems/3.0.0/gems/faraday-1.8.0/lib/faraday/file_part.rb:53: warning: previous definition of FilePart was here

I found that if I change all the assignments in multipart.rb to ||= the warnings go away. Is that a valid change to consider making a PR for?

multipart.rb

# frozen_string_literal: true

require_relative 'multipart/version'
require_relative 'multipart/file_part'
require_relative 'multipart/param_part'
require_relative 'multipart/middleware'

module Faraday
  # Main Faraday::Multipart module.
  module Multipart
    Faraday::Request.register_middleware(multipart: Faraday::Multipart::Middleware)
  end

  # Aliases for Faraday v1, these are all deprecated and will be removed in v2 of this middleware
  FilePart ||= Multipart::FilePart
  ParamPart ||= Multipart::ParamPart
  Parts ||= Multipart::Parts
  CompositeReadIO ||= Multipart::CompositeReadIO
  # multipart-post v2.2.0 introduces a new class hierarchy for classes like Parts and UploadIO
  # For backwards compatibility, detect the gem version and use the right class
  UploadIO ||= if ::Gem::Requirement.new('>= 2.2.0').satisfied_by?(Multipart.multipart_post_version)
               ::Multipart::Post::UploadIO
             else
               ::UploadIO
             end
end

Steps to reproduce

Our app has twilio, sentry, and plaid as dependencies, all of which rely on faraday. When we start our local development server the terminal is filled with these kinds of warnings.

With the change I suggested to multipart.rb the warnings go away.

iMacTia commented 1 year ago

Hi @richcorbs-ncino, this issue should go away if you update Faraday to v1.9.0 or higher. Not sure why you're using 1.8.0 and if you're under any external (dependency) constraint, but that would be the easiest solution and the upgrade should be completely painless for you.

If you can, I'd also suggest trying the latest 1.x version which is currently 1.10.3: it comes with an out-of-the-box JSON middleware (meaning you could remove your dependency on faraday_middleware if that's the only one you use) and could prepare you to move over to v2.x 🙌.

Please do let me know if you can upgrade and if that helps

richcorbs-ncino commented 1 year ago

Thanks so much! Updated to 1.9.0 and the warnings indeed went away!