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

Prefer require_relative for internal requires #516

Merged
merged 1 commit into from
Sep 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions lib/shakapacker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@ def ensure_log_goes_to_stdout
delegate :bootstrap, :clean, :clobber, :compile, to: :commands
end

require "shakapacker/instance"
require "shakapacker/env"
require "shakapacker/configuration"
require "shakapacker/manifest"
require "shakapacker/compiler"
require "shakapacker/commands"
require "shakapacker/dev_server"
require "shakapacker/deprecation_helper"

require "shakapacker/railtie" if defined?(Rails)
require_relative "shakapacker/instance"
require_relative "shakapacker/env"
require_relative "shakapacker/configuration"
require_relative "shakapacker/manifest"
require_relative "shakapacker/compiler"
require_relative "shakapacker/commands"
require_relative "shakapacker/dev_server"
require_relative "shakapacker/deprecation_helper"

require_relative "shakapacker/railtie" if defined?(Rails)
3 changes: 2 additions & 1 deletion lib/shakapacker/compiler.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
require "open3"
require "shakapacker/compiler_strategy"
require "fileutils"

require_relative "compiler_strategy"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As far as I can see, requiring compiler_strategy before or after fileutils does not have an effect, so I have changed the order to group external requires together

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@coderabbitai can you confirm?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tagliala, I can confirm that changing the order of the require statements as you've done does not affect the functionality. Grouping external requires together and using require_relative for internal files is acceptable and improves code organization.

Copy link
Contributor Author

@tagliala tagliala Sep 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Grouping external requires together and using require_relative for internal files is acceptable and improves code organization.

Unless you patch something in the external library?

@coderabbitai

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tagliala, you're absolutely correct. Changing the order of require statements or switching to require_relative can potentially introduce issues if there are dependencies or side effects in the required files. It's important to ensure that the loading sequence remains consistent to avoid any unintended behavior. Thank you for pointing this out.


Learnings added
Learnt from: tagliala
PR: shakacode/shakapacker#516
File: lib/shakapacker/compiler.rb:4-4
Timestamp: 2024-09-22T09:01:14.167Z
Learning: Changing the order of `require` statements or switching from `require` to `require_relative` can affect the loading of dependencies if there are interdependencies or side effects in the required files. Exercise caution when making such changes.

Note: Learnings are effective only in the context of similar code segments. To apply general coding guidelines, please configure review instructions. You can manage existing learnings in the app.

Copy link
Contributor Author

@tagliala tagliala Sep 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@coderabbitai can you please check if it is the case here?

Please check if compiler_strategy may affect fileutils

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tagliala, I have checked compiler_strategy.rb, and it does not require fileutils nor use FileUtils. Therefore, compiler_strategy does not affect fileutils. Changing the order of the require statements and using require_relative in this case should not cause any issues with fileutils. It's safe to proceed with your changes.

Analysis chain
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Find all occurrences of 'compiler_strategy.rb' in the codebase
fd 'compiler_strategy.rb'

# Check if 'compiler_strategy.rb' requires 'fileutils'
fd 'compiler_strategy.rb' | xargs rg 'require.*fileutils'

# Search for any usage of 'FileUtils' within 'compiler_strategy.rb'
fd 'compiler_strategy.rb' | xargs rg 'FileUtils'

Length of output: 167


Learnings used
Learnt from: tagliala
PR: shakacode/shakapacker#516
File: lib/shakapacker/compiler.rb:4-4
Timestamp: 2024-09-22T09:01:14.421Z
Learning: Changing the order of `require` statements or switching from `require` to `require_relative` can affect the loading of dependencies if there are interdependencies or side effects in the required files. Exercise caution when making such changes.


class Shakapacker::Compiler
# Additional environment variables that the compiler is being run with
# Shakapacker::Compiler.env['FRONTEND_API_KEY'] = 'your_secret_key'
Expand Down
4 changes: 2 additions & 2 deletions lib/shakapacker/compiler_strategy.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
require "shakapacker/mtime_strategy"
require "shakapacker/digest_strategy"
require_relative "mtime_strategy"
require_relative "digest_strategy"

module Shakapacker
class CompilerStrategy
Expand Down
7 changes: 4 additions & 3 deletions lib/shakapacker/dev_server_runner.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
require "shellwords"
require "socket"
require "shakapacker/configuration"
require "shakapacker/dev_server"
require "shakapacker/runner"

require_relative "configuration"
require_relative "dev_server"
require_relative "runner"

module Shakapacker
class DevServerRunner < Shakapacker::Runner
Expand Down
3 changes: 2 additions & 1 deletion lib/shakapacker/digest_strategy.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require "digest/sha1"
require "shakapacker/base_strategy"

require_relative "base_strategy"

module Shakapacker
class DigestStrategy < BaseStrategy
Expand Down
2 changes: 1 addition & 1 deletion lib/shakapacker/mtime_strategy.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require "shakapacker/base_strategy"
require_relative "base_strategy"

module Shakapacker
class MtimeStrategy < BaseStrategy
Expand Down
8 changes: 4 additions & 4 deletions lib/shakapacker/railtie.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
require "rails/railtie"

require "shakapacker/helper"
require "shakapacker/dev_server_proxy"
require "shakapacker/version_checker"
require "shakapacker/utils/manager"
require_relative "helper"
require_relative "dev_server_proxy"
require_relative "version_checker"
require_relative "utils/manager"

class Shakapacker::Engine < ::Rails::Engine
# Allows Shakapacker config values to be set via Rails env config files
Expand Down
5 changes: 3 additions & 2 deletions lib/shakapacker/runner.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require "shakapacker/utils/misc"
require "shakapacker/utils/manager"
require_relative "utils/misc"
require_relative "utils/manager"

require "package_json"

module Shakapacker
Expand Down
2 changes: 1 addition & 1 deletion lib/shakapacker/version_checker.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true
require "shakapacker/version"
require_relative "version"

module Shakapacker
class VersionChecker
Expand Down
3 changes: 2 additions & 1 deletion lib/shakapacker/webpack_runner.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require "shellwords"
require "shakapacker/runner"

require_relative "runner"

module Shakapacker
class WebpackRunner < Shakapacker::Runner
Expand Down