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

An error occurred while Lint/NonAtomicFileOperation cop was inspecting #10936

Closed
Zopolis4 opened this issue Aug 19, 2022 · 0 comments · Fixed by #10938
Closed

An error occurred while Lint/NonAtomicFileOperation cop was inspecting #10936

Zopolis4 opened this issue Aug 19, 2022 · 0 comments · Fixed by #10938
Labels

Comments

@Zopolis4
Copy link

An error occurred while Lint/NonAtomicFileOperation cop was inspecting /home/zopolis4/chromebrew/bin/crew:1187:18.
Expected a Parser::Source::Range, Comment or RuboCop::AST::Node, got NilClass
/var/lib/gems/3.0.0/gems/rubocop-1.35.0/lib/rubocop/cop/corrector.rb:91:in `to_range'
/var/lib/gems/3.0.0/gems/rubocop-1.35.0/lib/rubocop/cop/corrector.rb:100:in `check_range_validity'
/var/lib/gems/3.0.0/gems/parser-3.1.2.1/lib/parser/source/tree_rewriter.rb:398:in `combine'
/var/lib/gems/3.0.0/gems/parser-3.1.2.1/lib/parser/source/tree_rewriter.rb:194:in `replace'
/var/lib/gems/3.0.0/gems/parser-3.1.2.1/lib/parser/source/tree_rewriter.rb:218:in `remove'
/var/lib/gems/3.0.0/gems/rubocop-1.35.0/lib/rubocop/cop/lint/non_atomic_file_operation.rb:123:in `autocorrect'
/var/lib/gems/3.0.0/gems/rubocop-1.35.0/lib/rubocop/cop/lint/non_atomic_file_operation.rb:111:in `block in register_offense'
/var/lib/gems/3.0.0/gems/rubocop-1.35.0/lib/rubocop/cop/base.rb:346:in `correct'
/var/lib/gems/3.0.0/gems/rubocop-1.35.0/lib/rubocop/cop/base.rb:127:in `add_offense'
/var/lib/gems/3.0.0/gems/rubocop-1.35.0/lib/rubocop/cop/lint/non_atomic_file_operation.rb:110:in `register_offense'
/var/lib/gems/3.0.0/gems/rubocop-1.35.0/lib/rubocop/cop/lint/non_atomic_file_operation.rb:86:in `on_send'
/var/lib/gems/3.0.0/gems/rubocop-1.35.0/lib/rubocop/cop/commissioner.rb:136:in `public_send'
/var/lib/gems/3.0.0/gems/rubocop-1.35.0/lib/rubocop/cop/commissioner.rb:136:in `block (2 levels) in trigger_restricted_cops'
/var/lib/gems/3.0.0/gems/rubocop-1.35.0/lib/rubocop/cop/commissioner.rb:160:in `with_cop_error_handling'
/var/lib/gems/3.0.0/gems/rubocop-1.35.0/lib/rubocop/cop/commissioner.rb:135:in `block in trigger_restricted_cops'
/var/lib/gems/3.0.0/gems/rubocop-1.35.0/lib/rubocop/cop/commissioner.rb:134:in `each'
/var/lib/gems/3.0.0/gems/rubocop-1.35.0/lib/rubocop/cop/commissioner.rb:134:in `trigger_restricted_cops'
/var/lib/gems/3.0.0/gems/rubocop-1.35.0/lib/rubocop/cop/commissioner.rb:70:in `on_send'
/var/lib/gems/3.0.0/gems/rubocop-ast-1.21.0/lib/rubocop/ast/traversal.rb:154:in `on_if'
/var/lib/gems/3.0.0/gems/rubocop-1.35.0/lib/rubocop/cop/commissioner.rb:71:in `on_if'
/var/lib/gems/3.0.0/gems/rubocop-ast-1.21.0/lib/rubocop/ast/traversal.rb:156:in `on_if'
/var/lib/gems/3.0.0/gems/rubocop-1.35.0/lib/rubocop/cop/commissioner.rb:71:in `on_if'
/var/lib/gems/3.0.0/gems/rubocop-ast-1.21.0/lib/rubocop/ast/traversal.rb:136:in `block in on_dstr'
/var/lib/gems/3.0.0/gems/rubocop-ast-1.21.0/lib/rubocop/ast/traversal.rb:136:in `each'
/var/lib/gems/3.0.0/gems/rubocop-ast-1.21.0/lib/rubocop/ast/traversal.rb:136:in `on_dstr'
/var/lib/gems/3.0.0/gems/rubocop-1.35.0/lib/rubocop/cop/commissioner.rb:71:in `on_begin'
/var/lib/gems/3.0.0/gems/rubocop-ast-1.21.0/lib/rubocop/ast/traversal.rb:154:in `on_if'
/var/lib/gems/3.0.0/gems/rubocop-1.35.0/lib/rubocop/cop/commissioner.rb:71:in `on_if'
/var/lib/gems/3.0.0/gems/rubocop-ast-1.21.0/lib/rubocop/ast/traversal.rb:136:in `block in on_dstr'
/var/lib/gems/3.0.0/gems/rubocop-ast-1.21.0/lib/rubocop/ast/traversal.rb:136:in `each'
/var/lib/gems/3.0.0/gems/rubocop-ast-1.21.0/lib/rubocop/ast/traversal.rb:136:in `on_dstr'
/var/lib/gems/3.0.0/gems/rubocop-1.35.0/lib/rubocop/cop/commissioner.rb:71:in `on_begin'
/var/lib/gems/3.0.0/gems/rubocop-ast-1.21.0/lib/rubocop/ast/traversal.rb:157:in `on_block'
/var/lib/gems/3.0.0/gems/rubocop-1.35.0/lib/rubocop/cop/commissioner.rb:71:in `on_block'
/var/lib/gems/3.0.0/gems/rubocop-ast-1.21.0/lib/rubocop/ast/traversal.rb:157:in `on_block'
/var/lib/gems/3.0.0/gems/rubocop-1.35.0/lib/rubocop/cop/commissioner.rb:71:in `on_block'
/var/lib/gems/3.0.0/gems/rubocop-ast-1.21.0/lib/rubocop/ast/traversal.rb:136:in `block in on_dstr'
/var/lib/gems/3.0.0/gems/rubocop-ast-1.21.0/lib/rubocop/ast/traversal.rb:136:in `each'
/var/lib/gems/3.0.0/gems/rubocop-ast-1.21.0/lib/rubocop/ast/traversal.rb:136:in `on_dstr'
/var/lib/gems/3.0.0/gems/rubocop-1.35.0/lib/rubocop/cop/commissioner.rb:71:in `on_begin'
/var/lib/gems/3.0.0/gems/rubocop-ast-1.21.0/lib/rubocop/ast/traversal.rb:156:in `on_if'
/var/lib/gems/3.0.0/gems/rubocop-1.35.0/lib/rubocop/cop/commissioner.rb:71:in `on_if'
/var/lib/gems/3.0.0/gems/rubocop-ast-1.21.0/lib/rubocop/ast/traversal.rb:136:in `block in on_dstr'
/var/lib/gems/3.0.0/gems/rubocop-ast-1.21.0/lib/rubocop/ast/traversal.rb:136:in `each'
/var/lib/gems/3.0.0/gems/rubocop-ast-1.21.0/lib/rubocop/ast/traversal.rb:136:in `on_dstr'
/var/lib/gems/3.0.0/gems/rubocop-1.35.0/lib/rubocop/cop/commissioner.rb:71:in `on_begin'
/var/lib/gems/3.0.0/gems/rubocop-ast-1.21.0/lib/rubocop/ast/traversal.rb:154:in `on_if'
/var/lib/gems/3.0.0/gems/rubocop-1.35.0/lib/rubocop/cop/commissioner.rb:71:in `on_if'
/var/lib/gems/3.0.0/gems/rubocop-ast-1.21.0/lib/rubocop/ast/traversal.rb:136:in `block in on_dstr'
/var/lib/gems/3.0.0/gems/rubocop-ast-1.21.0/lib/rubocop/ast/traversal.rb:136:in `each'
/var/lib/gems/3.0.0/gems/rubocop-ast-1.21.0/lib/rubocop/ast/traversal.rb:136:in `on_dstr'
/var/lib/gems/3.0.0/gems/rubocop-1.35.0/lib/rubocop/cop/commissioner.rb:71:in `on_begin'
/var/lib/gems/3.0.0/gems/rubocop-ast-1.21.0/lib/rubocop/ast/traversal.rb:157:in `on_block'
/var/lib/gems/3.0.0/gems/rubocop-1.35.0/lib/rubocop/cop/commissioner.rb:71:in `on_block'
/var/lib/gems/3.0.0/gems/rubocop-ast-1.21.0/lib/rubocop/ast/traversal.rb:156:in `on_if'
/var/lib/gems/3.0.0/gems/rubocop-1.35.0/lib/rubocop/cop/commissioner.rb:71:in `on_if'
/var/lib/gems/3.0.0/gems/rubocop-ast-1.21.0/lib/rubocop/ast/traversal.rb:153:in `on_def'
/var/lib/gems/3.0.0/gems/rubocop-1.35.0/lib/rubocop/cop/commissioner.rb:71:in `on_def'
/var/lib/gems/3.0.0/gems/rubocop-ast-1.21.0/lib/rubocop/ast/traversal.rb:136:in `block in on_dstr'
/var/lib/gems/3.0.0/gems/rubocop-ast-1.21.0/lib/rubocop/ast/traversal.rb:136:in `each'
/var/lib/gems/3.0.0/gems/rubocop-ast-1.21.0/lib/rubocop/ast/traversal.rb:136:in `on_dstr'
/var/lib/gems/3.0.0/gems/rubocop-1.35.0/lib/rubocop/cop/commissioner.rb:71:in `on_begin'
/var/lib/gems/3.0.0/gems/rubocop-ast-1.21.0/lib/rubocop/ast/traversal.rb:20:in `walk'
/var/lib/gems/3.0.0/gems/rubocop-1.35.0/lib/rubocop/cop/commissioner.rb:86:in `investigate'
/var/lib/gems/3.0.0/gems/rubocop-1.35.0/lib/rubocop/cop/team.rb:155:in `investigate_partial'
/var/lib/gems/3.0.0/gems/rubocop-1.35.0/lib/rubocop/cop/team.rb:83:in `investigate'
/var/lib/gems/3.0.0/gems/rubocop-1.35.0/lib/rubocop/runner.rb:311:in `inspect_file'
/var/lib/gems/3.0.0/gems/rubocop-1.35.0/lib/rubocop/runner.rb:255:in `block in do_inspection_loop'
/var/lib/gems/3.0.0/gems/rubocop-1.35.0/lib/rubocop/runner.rb:289:in `block in iterate_until_no_changes'
/var/lib/gems/3.0.0/gems/rubocop-1.35.0/lib/rubocop/runner.rb:282:in `loop'
/var/lib/gems/3.0.0/gems/rubocop-1.35.0/lib/rubocop/runner.rb:282:in `iterate_until_no_changes'
/var/lib/gems/3.0.0/gems/rubocop-1.35.0/lib/rubocop/runner.rb:251:in `do_inspection_loop'
/var/lib/gems/3.0.0/gems/rubocop-1.35.0/lib/rubocop/runner.rb:134:in `block in file_offenses'
/var/lib/gems/3.0.0/gems/rubocop-1.35.0/lib/rubocop/runner.rb:159:in `file_offense_cache'
/var/lib/gems/3.0.0/gems/rubocop-1.35.0/lib/rubocop/runner.rb:133:in `file_offenses'
/var/lib/gems/3.0.0/gems/rubocop-1.35.0/lib/rubocop/runner.rb:124:in `process_file'
/var/lib/gems/3.0.0/gems/rubocop-1.35.0/lib/rubocop/runner.rb:105:in `block in each_inspected_file'
/var/lib/gems/3.0.0/gems/rubocop-1.35.0/lib/rubocop/runner.rb:104:in `each'
/var/lib/gems/3.0.0/gems/rubocop-1.35.0/lib/rubocop/runner.rb:104:in `reduce'
/var/lib/gems/3.0.0/gems/rubocop-1.35.0/lib/rubocop/runner.rb:104:in `each_inspected_file'
/var/lib/gems/3.0.0/gems/rubocop-1.35.0/lib/rubocop/runner.rb:90:in `inspect_files'
/var/lib/gems/3.0.0/gems/rubocop-1.35.0/lib/rubocop/runner.rb:47:in `run'
/var/lib/gems/3.0.0/gems/rubocop-1.35.0/lib/rubocop/cli/command/execute_runner.rb:26:in `block in execute_runner'
/var/lib/gems/3.0.0/gems/rubocop-1.35.0/lib/rubocop/cli/command/execute_runner.rb:52:in `with_redirect'
/var/lib/gems/3.0.0/gems/rubocop-1.35.0/lib/rubocop/cli/command/execute_runner.rb:25:in `execute_runner'
/var/lib/gems/3.0.0/gems/rubocop-1.35.0/lib/rubocop/cli/command/execute_runner.rb:17:in `run'
/var/lib/gems/3.0.0/gems/rubocop-1.35.0/lib/rubocop/cli/command.rb:11:in `run'
/var/lib/gems/3.0.0/gems/rubocop-1.35.0/lib/rubocop/cli/environment.rb:18:in `run'
/var/lib/gems/3.0.0/gems/rubocop-1.35.0/lib/rubocop/cli.rb:72:in `run_command'
/var/lib/gems/3.0.0/gems/rubocop-1.35.0/lib/rubocop/cli.rb:79:in `execute_runners'
/var/lib/gems/3.0.0/gems/rubocop-1.35.0/lib/rubocop/cli.rb:48:in `run'
/var/lib/gems/3.0.0/gems/rubocop-1.35.0/exe/rubocop:19:in `block in <top (required)>'
/var/lib/gems/3.0.0/gems/benchmark-0.2.0/lib/benchmark.rb:311:in `realtime'
/var/lib/gems/3.0.0/gems/rubocop-1.35.0/exe/rubocop:19:in `<top (required)>'
/usr/local/bin/rubocop:25:in `load'
/usr/local/bin/rubocop:25:in `<main>'

The aformentioned line of code:

def shrink_dir(dir)
  unless CREW_NOT_SHRINK_ARCHIVE
    Dir.chdir dir do
      if File.exist?("#{CREW_PREFIX}/bin/rdfind")
        puts 'Using rdfind to convert duplicate files to hard links.'
        system "#{CREW_PREFIX}/bin/rdfind -removeidentinode true -makesymlinks false -makehardlinks true -makeresultsfile false ."
      end
      # Issues with non-x86_64 in compressing libraries, so just compress
      # non-libraries. Also note that one needs to use "upx -d" on a
      # compressed file to use ldd.
      # sommelier also isn't happy when sommelier and xwayland are compressed
      # so don't compress those packages.
      if File.exist?("#{CREW_PREFIX}/bin/upx")
        # 1. Find executable binaries but also check for hard linked
        # files by making sure we have a unique set of
        # inodes for the binaries found.
        # 2. Copy to a temp file.
        # 3. Compress using upx. (Uncompressble files are ignored.)
        # 4. Check compression by expanding the compressed file with
        # upx.
        # 5. If the expansion doesn't error out then it is ok to copy
        # over the original. (This also lets us only avoid compressing
        # hard linked files multiple times.)
        # Also disable for sommelier.elf and Xwayland.elf since upx
        # breaks those binaries.
        @execfiles = `find . -executable -type f ! \\( -name \"*.so*\" -o -name \"*.a\" -o -name \"Xwayland.elf\" -o -name \"sommelier.elf\" \\) -exec head -c4 {} 2>/dev/null \\; -printf ' %i %p\\n'| grep ^.ELF | sort -u -n -s -k2,2 | awk '{print $3}'`.chomp
        unless @execfiles.to_s.empty?
          puts 'Using upx to shrink binaries.'
          # Copying in the ThreadPoolExecutor loop fails non-deterministically
          @execfiles.each_line do |execfilecp|
            execfilecp.slice! '.'
            execfilecp = dir + execfilecp.chomp
            FileUtils.cp execfilecp.to_s, "#{execfilecp}-crewupxtmp"
          end
          begin
            gem 'concurrent-ruby'
          rescue Gem::LoadError
            puts ' -> install gem concurrent-ruby'
            Gem.install('concurrent-ruby')
            gem 'concurrent-ruby'
          end
          require 'concurrent'
          pool = Concurrent::ThreadPoolExecutor.new(
            min_threads: 1,
            max_threads: CREW_NPROC,
            max_queue: 0, # unbounded work queue
            fallback_policy: :caller_runs
          )
          @execfiles.each_line do |execfile|
            pool.post do
              execfile.slice! '.'
              execfile = dir + execfile.chomp
              puts "Attempting to compress #{execfile} ...".orange
              # Make tmp file for compression
              unless system "upx --lzma #{execfile}-crewupxtmp"
                puts "Compression of #{execfile} failed...".orange if @opt_verbose
                FileUtils.rm_f "#{execfile}-crewupxtmp"
              end
              if File.exist?("#{execfile}-crewupxtmp")
                puts "Testing compressed #{execfile}...".lightblue if @opt_verbose
                if system "upx -t #{execfile}-crewupxtmp && cp #{execfile}-crewupxtmp #{execfile}"
                  puts "#{execfile} successfully compressed...".lightgreen
                elsif File.exist?("#{execfile}-crewupxtmp")
                  FileUtils.rm_f "#{execfile}-crewupxtmp" # line 1187 in original file
                end
              end
              FileUtils.rm_f "#{execfile}-crewupxtmp" 
            end
          end
          pool.shutdown
          pool.wait_for_termination
          # Make sure temporary compression copies are deleted.
          system 'find . -executable -type f -name "*-crewupxtmp" -delete'
        end
      end
    end
  end
end

RuboCop version

1.35.0 (using Parser 3.1.2.1, rubocop-ast 1.21.0, running on ruby 3.0.4 x86_64-linux-gnu)
@koic koic added the bug label Aug 19, 2022
koic added a commit to koic/rubocop that referenced this issue Aug 19, 2022
Fixes rubocop#10936.

This PR fixes an error for `Lint/NonAtomicFileOperation`
when using `FileTest.exist?` as a condition for `elsif`.
Complex cases such as `if`/`elsif`/`else` don't autocorrect
and respect user changes.
bbatsov pushed a commit that referenced this issue Aug 19, 2022
Fixes #10936.

This PR fixes an error for `Lint/NonAtomicFileOperation`
when using `FileTest.exist?` as a condition for `elsif`.
Complex cases such as `if`/`elsif`/`else` don't autocorrect
and respect user changes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants