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

Recommend File.expand_path(__dir__) over File.expand_path('..', __FILE__) #4008

Closed
backus opened this issue Feb 2, 2017 · 4 comments
Closed

Comments

@backus
Copy link
Contributor

backus commented Feb 2, 2017

Since Ruby 2.0 we've had __dir__ as well as __FILE__. Still, I think a very standard practice is write

File.expand_path('..', __FILE__)

even though now you can do

File.expand_path(__dir__)

Other corrections we could make like this:

File.expand_path('../..', __FILE__) == File.expand_path('..', __dir__)         # => true
Pathname.new(__FILE__).parent.expand_path == Pathname.new(__dir__).expand_path # =>true
@bbatsov
Copy link
Collaborator

bbatsov commented Feb 3, 2017

I'm fine with the idea. I think I planned to implement this myself years ago, but I never got to doing so.

@dabroz
Copy link
Contributor

dabroz commented Feb 5, 2017

@backus Dir.pwd is the current directory, which can be changed with Dir.chdir. __dir__ is always constant for a specific file.

@backus
Copy link
Contributor Author

backus commented Feb 5, 2017

@dabroz good point I'll adjust that

koic added a commit to koic/rubocop that referenced this issue Feb 7, 2018
Fixes rubocop#4008.

## Summary

This cop checks for use of the `File.expand_path` arguments.

```ruby
# bad
File.expand_path('..', __FILE__)

# good
File.expand_path(__dir__)
```

Likewise, it also checks for the `Pathname.new` argument.

```ruby
# bad
Pathname.new(__FILE__).parent.expand_path

# good
Pathname.new(__dir__).expand_path
```

Basically, replace `__FILE__` with `__dir_`, but also warn about
arguments that specify unnecessary current directory as follows.

```ruby
# bad
File.expand_path('.', __FILE__)

# good
File.expand_path(__FILE__)
```

This cop has autocorrect for these.
This commit autocorrected some offenses that had occurred in
RuboCop itself using this `Style/ExpandPathArguments` cop.

## Other Information

If other than `str_type` is used as an argument to `expand_path`,
it will not be checked because the path cannot be determined.

For example, in the following cases.

```ruby
File.expand_path(path, __FILE__)
File.expand_path("#{path_to_file}.png", __FILE__)
```
bbatsov pushed a commit that referenced this issue Feb 10, 2018
Fixes #4008.

## Summary

This cop checks for use of the `File.expand_path` arguments.

```ruby
# bad
File.expand_path('..', __FILE__)

# good
File.expand_path(__dir__)
```

Likewise, it also checks for the `Pathname.new` argument.

```ruby
# bad
Pathname.new(__FILE__).parent.expand_path

# good
Pathname.new(__dir__).expand_path
```

Basically, replace `__FILE__` with `__dir_`, but also warn about
arguments that specify unnecessary current directory as follows.

```ruby
# bad
File.expand_path('.', __FILE__)

# good
File.expand_path(__FILE__)
```

This cop has autocorrect for these.
This commit autocorrected some offenses that had occurred in
RuboCop itself using this `Style/ExpandPathArguments` cop.

## Other Information

If other than `str_type` is used as an argument to `expand_path`,
it will not be checked because the path cannot be determined.

For example, in the following cases.

```ruby
File.expand_path(path, __FILE__)
File.expand_path("#{path_to_file}.png", __FILE__)
```
@reitzig
Copy link

reitzig commented Feb 25, 2018

The message is Style/Dir: Use dir to get an absolute path to the current file's directory. [Style/Dir] but __dir__ alone gives us a relative path. The autofix from File.expand_path(File.dirname(__FILE__)) to __dir__ is therefore wrong.

This is with Ruby 2.3.1 and RuboCop 0.52.1.

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

No branches or pull requests

4 participants