Shopify/packwerk

[Bug Report] Uninitialized constant (FileUtils)

jaydorsey opened this issue ยท 4 comments

Description

When I run packwerk init or bundle exec packwerk init I receive an uninitialized constant error:

        29: from /Users/jaydorsey/.asdf/installs/ruby/2.6.6/bin/bundle:23:in `<main>'
        28: from /Users/jaydorsey/.asdf/installs/ruby/2.6.6/bin/bundle:23:in `load'
        27: from /Users/jaydorsey/.asdf/installs/ruby/2.6.6/lib/ruby/gems/2.6.0/gems/bundler-2.2.6/exe/bundle:37:in `<top (required)>'
        26: from /Users/jaydorsey/.asdf/installs/ruby/2.6.6/lib/ruby/site_ruby/2.6.0/bundler/friendly_errors.rb:130:in `with_friendly_errors'
        25: from /Users/jaydorsey/.asdf/installs/ruby/2.6.6/lib/ruby/gems/2.6.0/gems/bundler-2.2.6/exe/bundle:49:in `block in <top (required)>'
        24: from /Users/jaydorsey/.asdf/installs/ruby/2.6.6/lib/ruby/site_ruby/2.6.0/bundler/cli.rb:24:in `start'
        23: from /Users/jaydorsey/.asdf/installs/ruby/2.6.6/lib/ruby/site_ruby/2.6.0/bundler/vendor/thor/lib/thor/base.rb:485:in `start'
        22: from /Users/jaydorsey/.asdf/installs/ruby/2.6.6/lib/ruby/site_ruby/2.6.0/bundler/cli.rb:30:in `dispatch'
        21: from /Users/jaydorsey/.asdf/installs/ruby/2.6.6/lib/ruby/site_ruby/2.6.0/bundler/vendor/thor/lib/thor.rb:392:in `dispatch'
        20: from /Users/jaydorsey/.asdf/installs/ruby/2.6.6/lib/ruby/site_ruby/2.6.0/bundler/vendor/thor/lib/thor/invocation.rb:127:in `invoke_command'
        19: from /Users/jaydorsey/.asdf/installs/ruby/2.6.6/lib/ruby/site_ruby/2.6.0/bundler/vendor/thor/lib/thor/command.rb:27:in `run'
        18: from /Users/jaydorsey/.asdf/installs/ruby/2.6.6/lib/ruby/site_ruby/2.6.0/bundler/cli.rb:494:in `exec'
        17: from /Users/jaydorsey/.asdf/installs/ruby/2.6.6/lib/ruby/site_ruby/2.6.0/bundler/cli/exec.rb:28:in `run'
        16: from /Users/jaydorsey/.asdf/installs/ruby/2.6.6/lib/ruby/site_ruby/2.6.0/bundler/cli/exec.rb:63:in `kernel_load'
        15: from /Users/jaydorsey/.asdf/installs/ruby/2.6.6/lib/ruby/site_ruby/2.6.0/bundler/cli/exec.rb:63:in `load'
        14: from /Users/jaydorsey/.asdf/installs/ruby/2.6.6/bin/packwerk:23:in `<top (required)>'
        13: from /Users/jaydorsey/.asdf/installs/ruby/2.6.6/bin/packwerk:23:in `load'
        12: from /Users/jaydorsey/.asdf/installs/ruby/2.6.6/lib/ruby/gems/2.6.0/gems/packwerk-1.1.2/exe/packwerk:6:in `<top (required)>'
        11: from /Users/jaydorsey/.asdf/installs/ruby/2.6.6/lib/ruby/gems/2.6.0/gems/sorbet-runtime-0.5.6295/lib/types/private/methods/_methods.rb:222:in `block in _on_method_added'
        10: from /Users/jaydorsey/.asdf/installs/ruby/2.6.6/lib/ruby/gems/2.6.0/gems/sorbet-runtime-0.5.6295/lib/types/private/methods/call_validation.rb:126:in `validate_call'
         9: from /Users/jaydorsey/.asdf/installs/ruby/2.6.6/lib/ruby/gems/2.6.0/gems/sorbet-runtime-0.5.6295/lib/types/private/methods/call_validation.rb:126:in `call'
         8: from /Users/jaydorsey/.asdf/installs/ruby/2.6.6/lib/ruby/gems/2.6.0/gems/packwerk-1.1.2/lib/packwerk/cli.rb:50:in `run'
         7: from /Users/jaydorsey/.asdf/installs/ruby/2.6.6/lib/ruby/gems/2.6.0/gems/sorbet-runtime-0.5.6295/lib/types/private/methods/_methods.rb:222:in `block in _on_method_added'
         6: from /Users/jaydorsey/.asdf/installs/ruby/2.6.6/lib/ruby/gems/2.6.0/gems/sorbet-runtime-0.5.6295/lib/types/private/methods/call_validation.rb:126:in `validate_call'
         5: from /Users/jaydorsey/.asdf/installs/ruby/2.6.6/lib/ruby/gems/2.6.0/gems/sorbet-runtime-0.5.6295/lib/types/private/methods/call_validation.rb:126:in `call'
         4: from /Users/jaydorsey/.asdf/installs/ruby/2.6.6/lib/ruby/gems/2.6.0/gems/packwerk-1.1.2/lib/packwerk/cli.rb:59:in `execute_command'
         3: from /Users/jaydorsey/.asdf/installs/ruby/2.6.6/lib/ruby/gems/2.6.0/gems/packwerk-1.1.2/lib/packwerk/cli.rb:96:in `init'
         2: from /Users/jaydorsey/.asdf/installs/ruby/2.6.6/lib/ruby/gems/2.6.0/gems/packwerk-1.1.2/lib/packwerk/generators/application_validation.rb:9:in `generate'
         1: from /Users/jaydorsey/.asdf/installs/ruby/2.6.6/lib/ruby/gems/2.6.0/gems/packwerk-1.1.2/lib/packwerk/generators/application_validation.rb:21:in `generate'
/Users/jaydorsey/.asdf/installs/ruby/2.6.6/lib/ruby/gems/2.6.0/gems/packwerk-1.1.2/lib/packwerk/generators/application_validation.rb:31:in `generate_packwerk_validate_script': uninitialized constant Packwerk::Generators::ApplicationValidation::FileUtils (NameError)

The relevant line of code is here. I noticed there wasn't a require 'fileutils' anywhere in the codebase, so adding one to line 4 of exe/packwerk resolved this for me. I feel like there should be a require somewhere inside the gem, but I don't know why this doesn't break inside Docker w/ the same ruby version.

Things I tried as workarounds that reproduced/exhibited same behavior:

  • I tried with a brand new rails app (same version), macos, was able to reproduce
  • I tried with both gem install packwerk and adding it to top level of Gemfile and using bundle exec
  • I made sure to spring stop in all instances (and DISABLE_SPRING=1)
  • Generate a bin/packwerk binstub
  • Upgrading to Rails 6.1.3; Ruby 2.7.1

Things look fine/not able to reproduce with:

  • I tried this inside a ruby 2.6.6 docker container, same rails version, not able to reproduce the error
  • I tried this inside an alpine linux container w/ asdf-installed ruby 2.6.6; not able to reproduce

To Reproduce

Ruby 2.6.6 (p146)/macos (intel)/packwerk 1.1.2/Rails 6.0.3.5:

rails new foo_bar && cd foo_bar && packwerk init

Expected Behaviour

Init should complete successfully

Screenshots

N/A

Version Information

  • Packwerk: 1.1.2
  • Ruby 2.6.6
  • Rails 6.0.3.5
  • Macos
  • asdf version manager

Additional Context

I do have a workaround for this, so not urgent. Behavior is strange so appreciate any confirmations of this behavior. Would normally do a quick PR for this but the fact that I can't reproduce it except on my machine has me wanting to confirm the behavior first. I feel like I'm missing something...

I seem to have the same error on a fresh Rails 6.1.3 --api-only application with Ruby 3.0.0:

โฏ bundle exec packwerk init
๐Ÿ“ฆ Initializing Packwerk...
๐Ÿ“ฆ Generating application validator...
bundler: failed to load command: packwerk (/Users/holgerfrohloff/.rbenv/versions/3.0.0/bin/packwerk)
/Users/holgerfrohloff/.rbenv/versions/3.0.0/lib/ruby/gems/3.0.0/gems/packwerk-1.1.2/lib/packwerk/generators/application_validation.rb:31:in `generate_packwerk_validate_script': uninitialized constant Packwerk::Generators::ApplicationValidation::FileUtils (NameError)
Did you mean?  FileTest
        from /Users/holgerfrohloff/.rbenv/versions/3.0.0/lib/ruby/gems/3.0.0/gems/packwerk-1.1.2/lib/packwerk/generators/application_validation.rb:21:in `generate'
        from /Users/holgerfrohloff/.rbenv/versions/3.0.0/lib/ruby/gems/3.0.0/gems/packwerk-1.1.2/lib/packwerk/generators/application_validation.rb:9:in `generate'
        from /Users/holgerfrohloff/.rbenv/versions/3.0.0/lib/ruby/gems/3.0.0/gems/packwerk-1.1.2/lib/packwerk/cli.rb:96:in `init'
        from /Users/holgerfrohloff/.rbenv/versions/3.0.0/lib/ruby/gems/3.0.0/gems/packwerk-1.1.2/lib/packwerk/cli.rb:59:in `execute_command'
        from /Users/holgerfrohloff/.rbenv/versions/3.0.0/lib/ruby/gems/3.0.0/gems/sorbet-runtime-0.5.6307/lib/types/private/methods/call_validation.rb:949:in `call'

Same with rails 6.1.1 and ruby 3.0.0

Interesting. If I just start up a blank irb I get FileUtils preloaded. ๐Ÿค”

Turns out irb just requires fileutils ๐Ÿ˜„

I'd approve a PR adding the missing require to lib/packwerk.rb.