Skip to content

Encapsulate location encoding in response builders#4089

Draft
vinistock wants to merge 1 commit intorubydex_adoption_feature_branchfrom
vs_encapsulate_location_encoding_in_response_builders
Draft

Encapsulate location encoding in response builders#4089
vinistock wants to merge 1 commit intorubydex_adoption_feature_branchfrom
vs_encapsulate_location_encoding_in_response_builders

Conversation

@vinistock
Copy link
Copy Markdown
Member

Motivation

While working on the Rubydex migration, I realized that we weren't actually returning the expected code units for the different encodings in a bunch of requests.

The primary reason for that is the fact that listeners don't have access to the parse result or the encoding to access the code units cache and compute the expected response. This also means add-ons can't provide correct code unit positions.

This PR changes our base response builder to be instantiated with the code units cache for the specific encoding. All listeners (including the ones coming from add-ons) have access to the response builder to make contributions to the final response, so this makes it straightforward for all listeners to use the correct code unit positions.

Note: this also uncovered a bug in Prism, which calculates UTF-8 code units incorrectly. The fix is merged, but we can only ship this PR once there's a Prism release (and we will need to bump our requirement to be strictly the latest).

Implementation

The idea is simple: the base response builder class now enforces that all of them are instantiated with the encoding and parse result for the document being served, so that we can get the right code units cache.

Then we expose a public API that any listener can use to produce new locations.

After all listeners are migrated to this new API and have their responses fixed for all encodings, we need to remove the old helpers from Support::Common as those produce incorrect code unit positions.

Automated Tests

Added a test to verify that we are creating locations with the expected code unit values for a multibyte source.

@vinistock vinistock self-assigned this Apr 30, 2026
@vinistock vinistock added bugfix This PR will fix an existing bug server This pull request should be included in the server gem's release notes labels Apr 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bugfix This PR will fix an existing bug server This pull request should be included in the server gem's release notes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant