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

Add new Lint/NonAtomicFileOperation cop #10696

Merged
merged 1 commit into from Jun 27, 2022

Commits on Jun 26, 2022

  1. Add new Lint/NonAtomicFileOperation cop

    Checks for non-atomic file operation.
    And then replace it with a nearly equivalent and atomic method.
    
    These can cause problems that are difficult to reproduce,
    especially in cases of frequent file operations in parallel,
    such as test runs with parallel_rspec.
    
    For examples: creating a directory if there is none, has the following problems
    
    An exception occurs when the directory didn't exist at the time of `exist?`,
    but someone else created it before `mkdir` was executed.
    
    Subsequent processes are executed without the directory that should be there
    when the directory existed at the time of `exist?`,
    but someone else deleted it shortly afterwards.
    
    ## @safety
    This cop is unsafe, because autocorrection change to atomic processing.
    The atomic processing of the replacement destination is not guaranteed
    to be strictly equivalent to that before the replacement.
    
    ## @example
    ```ruby
    # bad
    unless FileTest.exist?(path)
      FileUtils.makedirs(path)
    end
    
    if FileTest.exist?(path)
      FileUtils.remove(path)
    end
    
    # good
    FileUtils.mkdir_p(path)
    
    FileUtils.rm_rf(path)
    ```
    ydah committed Jun 26, 2022
    Copy the full SHA
    857709b View commit details
    Browse the repository at this point in the history