Skip to content

Keep generated Ruby constants under the binding namespace - #1

Closed
pboling wants to merge 1 commit into
mainfrom
fix/magnus-namespaced-constants
Closed

Keep generated Ruby constants under the binding namespace#1
pboling wants to merge 1 commit into
mainfrom
fix/magnus-namespaced-constants

Conversation

@pboling

@pboling pboling commented Aug 4, 2026

Copy link
Copy Markdown

Summary

Remove the Magnus Ruby wrapper's automatic export of generated constants into Object.

Generated Ruby types should remain under the public binding namespace. Publishing them globally is unsafe because generic names can collide with unrelated Ruby libraries. For example, TSLP's generated native TreeSitterLanguagePack::Parser was exported as ::Parser; requiring the parser gem afterward raised:

TypeError: Parser is not a module

The parser gem is not a dependency of TSLP. This is a generator namespace-pollution bug.

Changes

  • Remove the global Object.const_set re-export loop from the Magnus Ruby wrapper template.
  • Keep generated constants available through the binding module.
  • Add a generator regression test asserting that generated Ruby wrappers do not export constants globally.

Issues are disabled for this repository, so this PR is also the bug report and design record.

Verification

cargo test --lib backends::magnus::gen_bindings::tests::ruby_public_wrapper_keeps_generated_constants_namespaced

Passes.

Copilot AI lite review requested due to automatic review settings August 4, 2026 07:50

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes a namespace-pollution bug in the Magnus-generated Ruby wrapper by stopping the generator from re-exporting generated type constants into Object, keeping all generated Ruby types under the binding module namespace to avoid collisions with unrelated gems.

Changes:

  • Removed the Object.const_set re-export loop from the Ruby wrapper template so generated constants stay under the binding module.
  • Added a generator regression test asserting the generated main Ruby wrapper does not contain the global export code.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
src/backends/magnus/templates/main_rb_wrapper.rb.jinja Removes global constant re-exporting; leaves generated types namespaced under the binding module.
src/backends/magnus/gen_bindings/tests.rs Adds a regression test to prevent reintroducing global constant exports in generated Ruby wrappers.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +162 to +165
assert!(
!main_file.content.contains("::Object.const_set"),
"the generated Ruby wrapper must not export constants globally"
);
@pboling

pboling commented Aug 4, 2026

Copy link
Copy Markdown
Author

This PR was opened against the staging fork by mistake. The actual upstream PR is xberg-io#214.

@pboling pboling closed this Aug 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants