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

False positive of NonAtomicFileOperation for FileUtils.rm_r #10813

Closed
AlexWayfer opened this issue Jul 13, 2022 · 4 comments · Fixed by #10818
Closed

False positive of NonAtomicFileOperation for FileUtils.rm_r #10813

AlexWayfer opened this issue Jul 13, 2022 · 4 comments · Fixed by #10818

Comments

@AlexWayfer
Copy link
Contributor

From this discussion: #10760 (comment)

Expected behavior

No offenses or no exceptions for FileUtils.rm_r.

Actual behavior

If I have the code:

FileUtils.rm_r gem_name if Dir.exist? gem_name

it causes offenses by NonAtomicFileOperation cop.

If I remove the condition — it causes the exception:

     Failure/Error: FileUtils.rm_r gem_name
     
     Errno::ENOENT:
       No such file or directory @ apply2files - foo_bar

Even with require 'fileutils'.

Steps to reproduce the problem

The code is above.

RuboCop version

$ [bundle exec] rubocop -V
1.31.2 (using Parser 3.1.2.0, rubocop-ast 1.19.1, running on ruby 3.1.2 x86_64-linux)
  - rubocop-performance 1.14.2
  - rubocop-rspec 2.12.1
@ydah
Copy link
Member

ydah commented Jul 14, 2022

The offense message is confusing, so we improved the message at #10781

For the following code:

# frozen_string_literal: true

FileUtils.rm_r gem_name if Dir.exist? gem_name

The following message is output:

:
test.rb:3:1: W: Lint/NonAtomicFileOperation: Use atomic file operation method FileUtils.rm_rf.
FileUtils.rm_r gem_name if Dir.exist? gem_name
^^^^^^^^^^^^^^^^^^^^^^^
test.rb:3:25: W: [Correctable] Lint/NonAtomicFileOperation: Remove unnecessary existence check Dir.exist?.
FileUtils.rm_r gem_name if Dir.exist? gem_name
                        ^^^^^^^^^^^^^^^^^^^^^^

Expected changes:

# frozen_string_literal: true

- FileUtils.rm_r gem_name if Dir.exist? gem_name
+ FileUtils.rm_rf gem_name

@AlexWayfer
Copy link
Contributor Author

@ydah thank you, it's a good change. But… --force option is not only for cases where files (or dirs) don't exist. It'll also delete all protected files, probably something else. It's not an equal I beleive.

@ydah
Copy link
Member

ydah commented Jul 14, 2022

@AlexWayfer Yes, I also certainly don't think they are strictly equivalent. The reason for adding this cop is that these can cause problems that are difficult to reproduce, especially when frequent file operations are performed in parallel, as in test runs using parallel_rspec.
However, it might be nice to be able to options for cases allowed where atomic file deletion cannot be safely.

@AlexWayfer
Copy link
Contributor Author

@AlexWayfer Yes, I also certainly don't think they are strictly equivalent. The reason for adding this cop is that these can cause problems that are difficult to reproduce, especially when frequent file operations are performed in parallel, as in test runs using parallel_rspec. However, it might be nice to be able to options for cases allowed where atomic file deletion cannot be safely.

Maybe I'd even preffer a cop which will offense if I have rm_r (without f) without check for existance, to avoid the error.

There are just different approaches. And the current default behavior I consider as very agressive and inaccurate.

ydah added a commit to ydah/rubocop that referenced this issue Jul 20, 2022
…nAtomicFileOperation`

Fixes: rubocop#10813

This PR is suppress offense in case of
recursive deletion in `Lint/NonAtomicFileOperation`.
bbatsov pushed a commit that referenced this issue Jul 21, 2022
…FileOperation`

Fixes: #10813

This PR is suppress offense in case of
recursive deletion in `Lint/NonAtomicFileOperation`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants