Rename Opencode::Rails::VERSION -> Opencode::RAILS_VERSION

Critical fix: defining Opencode::Rails as a module shadowed ::Rails
under Ruby's constant lookup whenever host code referenced top-level
Rails.something from inside the Opencode:: namespace.

Caught when host code in lib/opencode/containers/container.rb failed
to boot:

  /workspaces/app/lib/opencode/containers/container.rb:433:
    undefined method 'root' for module Opencode::Rails (NoMethodError)
      "notes.md" => Rails.root.join(...)

Ruby resolved 'Rails' to 'Opencode::Rails' first (the gem's version
namespace) before falling back to ::Rails. The fix removes the
intermediate module entirely:

  module Opencode
    RAILS_VERSION = "0.0.1.alpha1"
  end

Added a regression test (test_no_opencode_rails_module) so the
shadowing never sneaks back in. opencode-ruby uses Opencode::VERSION
for its own gem version; we can't double-up on that constant, so this
gem uses RAILS_VERSION as a sibling on the same Opencode module.
This commit is contained in:
2026-05-20 05:22:23 -07:00
parent 65ee701cae
commit 8ccfe0f7cf
3 changed files with 23 additions and 5 deletions

View File

@@ -1,7 +1,15 @@
# frozen_string_literal: true # frozen_string_literal: true
# NOTE: we deliberately do NOT define `Opencode::Rails` as a module —
# host applications often have files inside the `Opencode::` namespace
# that reference top-level `::Rails.something`. Defining
# `Opencode::Rails` would shadow `::Rails` under Ruby's constant
# lookup rules (`Rails.root` would resolve to `Opencode::Rails.root`
# and raise NoMethodError).
#
# The opencode-ruby gem uses `Opencode::VERSION` for its own version.
# We can't reuse the same constant from a second gem, so we use a
# distinct, non-namespaced constant.
module Opencode module Opencode
module Rails RAILS_VERSION = "0.0.1.alpha1"
VERSION = "0.0.1.alpha1"
end
end end

View File

@@ -4,7 +4,7 @@ require_relative "lib/opencode/rails/version"
Gem::Specification.new do |spec| Gem::Specification.new do |spec|
spec.name = "opencode-rails" spec.name = "opencode-rails"
spec.version = Opencode::Rails::VERSION spec.version = Opencode::RAILS_VERSION
spec.authors = ["Ajay Krishnan"] spec.authors = ["Ajay Krishnan"]
spec.email = ["ajay@krishnan.ca"] spec.email = ["ajay@krishnan.ca"]

View File

@@ -49,6 +49,16 @@ class Opencode::LoadingTest < Minitest::Test
end end
def test_version_constant def test_version_constant
assert_match(/\A\d+\.\d+\.\d+/, Opencode::Rails::VERSION) assert_match(/\A\d+\.\d+\.\d+/, Opencode::RAILS_VERSION)
end
def test_no_opencode_rails_module
# Defining Opencode::Rails as a module would shadow ::Rails for any
# host code that references top-level Rails.* from inside the
# Opencode:: namespace (e.g. lib/opencode/containers/container.rb).
# Verify the namespace stays clean — version lives at
# Opencode::RAILS_VERSION, not Opencode::Rails::VERSION.
refute Opencode.const_defined?(:Rails),
"Opencode::Rails must not be defined — it would shadow ::Rails inside the Opencode namespace"
end end
end end