Skip to content

Commit

Permalink
[Fix #1177] Partially fix Rails/FilePath autocorrect for root filepat…
Browse files Browse the repository at this point in the history
…hs by marking cop as unsafe for autocorrection

We correctly add a @safety annotation for Rails/FilePath since it can produce invalid code like this:

# pass any string argument beginning with '/' to Rails.root.join and it will omit the Rails path

(dev)> Rails.root.join '/'
=> #<Pathname:/>
(dev)> Rails.root.join '//'
=> #<Pathname://>
(dev)> Rails.root.join '/abc'
=> #<Pathname:/abc>
(dev)> Rails.root.join '/abc/'
=> #<Pathname:/abc/>
  • Loading branch information
corsonknowles committed Dec 23, 2024
1 parent fecead8 commit c559f7a
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 0 deletions.
1 change: 1 addition & 0 deletions config/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,7 @@ Rails/FilePath:
VersionAdded: '0.47'
VersionChanged: '2.4'
EnforcedStyle: slashes
SafeAutoCorrect: false
SupportedStyles:
- slashes
- arguments
Expand Down
3 changes: 3 additions & 0 deletions lib/rubocop/cop/rails/file_path.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ module Rails
# Identifies usages of file path joining process to use `Rails.root.join` clause.
# It is used to add uniformity when joining paths.
#
# @safety
# This cop's autocorrection is unsafe because it will replace the root path with an invalid path.
#
# @example EnforcedStyle: slashes (default)
# # bad
# Rails.root.join('app', 'models', 'goober')
Expand Down

0 comments on commit c559f7a

Please sign in to comment.