Skip to content

Circular require #173

Open
Open
@rwstauner

Description

I noticed an issue if that occurs under certain circumstances.
If you require "protoboeuf" it defines an autoload for CodeGen.
If you then require codegen directly it requires codegen_type_helper before establishing the CodeGen module
and the autoload is triggered for a file that is currently being loaded:

💥 ruby -Ilib -ve 'require "protoboeuf"; require "protoboeuf/codegen"'
ruby 3.3.4 (2024-07-09 revision be1089c8ec) [arm64-darwin24]
<internal:/Users/rwstauner/.rubies/3.3.4/lib/ruby/3.3.0/rubygems/core_ext/kernel_require.rb>:136: warning: <internal:/Users/rwstauner/.rubies/3.3.4/lib/ruby/3.3.0/rubygems/core_ext/kernel_require.rb>:136: warning: loading in progress, circular require considered harmful - /Users/rwstauner/src/github.com/Shopify/protoboeuf/lib/protoboeuf/codegen.rb
        from -e:1:in  `<main>'
        from <internal:/Users/rwstauner/.rubies/3.3.4/lib/ruby/3.3.0/rubygems/core_ext/kernel_require.rb>:136:in  `require'
        from <internal:/Users/rwstauner/.rubies/3.3.4/lib/ruby/3.3.0/rubygems/core_ext/kernel_require.rb>:136:in  `require'
        from /Users/rwstauner/src/github.com/Shopify/protoboeuf/lib/protoboeuf/codegen.rb:5:in  `<top (required)>'
        from /Users/rwstauner/src/github.com/Shopify/protoboeuf/lib/protoboeuf/codegen.rb:5:in  `require_relative'
        from /Users/rwstauner/src/github.com/Shopify/protoboeuf/lib/protoboeuf/codegen_type_helper.rb:3:in  `<top (required)>'
        from /Users/rwstauner/src/github.com/Shopify/protoboeuf/lib/protoboeuf/codegen_type_helper.rb:4:in  `<module:ProtoBoeuf>'
        from <internal:/Users/rwstauner/.rubies/3.3.4/lib/ruby/3.3.0/rubygems/core_ext/kernel_require.rb>:136:in  `require'
        from <internal:/Users/rwstauner/.rubies/3.3.4/lib/ruby/3.3.0/rubygems/core_ext/kernel_require.rb>:136:in  `require'

/Users/rwstauner/src/github.com/Shopify/protoboeuf/lib/protoboeuf/codegen_type_helper.rb:4: warning: Expected protoboeuf/codegen to define ProtoBoeuf::CodeGen but it didn't

One option is to move the require:

diff --git a/lib/protoboeuf/codegen.rb b/lib/protoboeuf/codegen.rb
index 9f6492e..7bb353e 100644
--- a/lib/protoboeuf/codegen.rb
+++ b/lib/protoboeuf/codegen.rb
@@ -2,13 +2,14 @@
 
 require "erb"
 require "syntax_tree"
-require_relative "codegen_type_helper"
 
 module ProtoBoeuf
   class CodeGen
     class EnumCompiler
       attr_reader :generate_types
 
+      require_relative "codegen_type_helper"
+
       include TypeHelper
 
       class << self

we could also change the namespace, etc.

Activity

linked a pull request that will close this issue on Jan 30, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Participants

@rwstauner@davebenvenuti

Issue actions

    Circular require · Issue #173 · Shopify/protoboeuf