Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Absolute Path vs Relative Path on deprecations #70

Open
sobrinho opened this issue Jul 7, 2022 · 6 comments
Open

Absolute Path vs Relative Path on deprecations #70

sobrinho opened this issue Jul 7, 2022 · 6 comments

Comments

@sobrinho
Copy link

sobrinho commented Jul 7, 2022

Hi there!

When recording deprecations as suggested on https://about.gitlab.com/blog/2021/02/03/how-we-automatically-fixed-hundreds-of-ruby-2-7-deprecation-warnings/ we are seeing recordings like:

-  DEPRECATION WARNING: /Users/sobrinho/Developer/my-app/models/concerns/logable.rb:36: warning: Using the last argument as keyword parameters is deprecated; maybe ** should be added to the call

But when running on CI, we have:

+  DEPRECATION WARNING: /app/app/models/concerns/logable.rb:36: warning: Using the last argument as keyword parameters is deprecated; maybe ** should be added to the call

Which leads to a DeprecationToolkit::Behaviors::DeprecationMismatch failure.

I'm currently fixing those with a dirty sed:

find spec/deprecation -type f | xargs gsed -i 's/.Users.sobrinho.Developer.my-app./\/app\//g'

Does it worth to have a way of replacing paths/values from deprecations or at least mention how on README?

@sobrinho
Copy link
Author

sobrinho commented Jul 7, 2022

Another way:

class DeprecationToolkit::Collector
  def initialize(deprecations)
    # This will remove the absolute paths from the deprecations since it varies
    # from environment to environment.
    #
    # See https://github.com/Shopify/deprecation_toolkit/issues/70
    self.deprecations = deprecations.map do |deprecation|
      deprecation
        .gsub(Gem.dir.to_s, "[GEM_DIR]")
        .gsub(Rails.root.to_s, "[RAILS_ROOT]")
    end
  end
end

@rafaelfranca
Copy link
Member

What is adding paths to the deprecation message? Active Support deprecation doesn't do it

@rafaelfranca
Copy link
Member

rafaelfranca commented Jul 7, 2022

Ah, it is the message Ruby generates.

We ignore stacktraces when writing and comparing deprecations (for deprecation using ActiveSupport::Deprecation). It seems the fact that the location of the deprecation is on the Ruby warning message being recorded is just a mistake from our part when we implemented that.

But, if we remove it from the recorded deprecation, like we do with ActiveSupport::Deprecation, the approach used in that blog post doesn't work anymore.

Tough position to be since I want to allow automation, but I also want to match the same behavior as ActiveSupport::Deprecation which doesn't allow automation right now.

I think we should really think on how to allow automation in both contexts in order to fix this issue properly.

@sobrinho
Copy link
Author

sobrinho commented Jul 8, 2022

The code snippet is working fine, probably allowing to change the collector (to avoid the monkey patch) or either allowing to scrub data from the deprecation messages would work (if do not want to couple to Rails, for instance, even though we could loose couple with a if defined? Rails or something).

@henrahmagix
Copy link

Would this be solved by #60, if that were in a releasable state?

@brkn
Copy link

brkn commented Nov 17, 2023

I had to add 3rd gsub to handle psych deprecation warnings

# At spec/support/deprecation_toolkit.rb

# frozen_string_literal: true

require 'deprecation_toolkit'
require 'deprecation_toolkit/rspec'

# Monkeypatch the DeprecationToolkit
# This will remove the absolute paths from the deprecations since it varies from environment to environment.
# See https://github.com/Shopify/deprecation_toolkit/issues/70
module DeprecationToolkit
  class Collector
    def initialize(deprecations)
      self.deprecations = deprecations.map do |deprecation|
        deprecation
          .gsub(Gem.dir.to_s, '[GEM_DIR]')
          .gsub(Rails.root.to_s, '[RAILS_ROOT]')
          .gsub(RbConfig::CONFIG['rubylibdir'].to_s, '[RUBY_LIBS_DIR]') # For default ruby gems such as psych
      end
    end
  end
end

DeprecationToolkit::Configuration.test_runner = :rspec
DeprecationToolkit::Configuration.deprecation_path = 'spec/fixtures/deprecations'

# Empty regex matches every warning to be treated as a deprecation error
DeprecationToolkit::Configuration.warnings_treated_as_deprecation = [//]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants