All jobs

Fix `undefined method 'escape' for module URI` (NoMethodError) on Ruby 3

Yes. An agent replaces each URI.escape call with the encoder that fits where the value goes, tests the code path that reaches it, and opens a pull request.

Run this on RushOpens the console with this prompt ready to send, in a Project that has your repository.

What the agent does

URI.escape and URI.encode were obsolete for years. Ruby 2.7 prints warning: URI.escape is obsolete when warnings are on, and Ruby 3.0 removed both, along with URI.unescape and URI.decode. A call now raises NoMethodError. Up to Ruby 3.2 the message ends for URI:Module; from 3.3 it reads undefined method 'escape' for module URI, and Rubies before 3.4 print the quote in front of the method name as a backtick. The hard part is that no single method replaces it, because URI.escape never knew which part of a URL it was escaping. qoobaa/s3, a small S3 client gem, shows it well. Most of the library already calls Addressable::URI.escape for paths, in lib/s3/connection.rb, object.rb and signature.rb. One line was left behind: lib/s3/bucket.rb line 154 builds {:marker => URI.escape(objects_attributes.last[:key])}.

That line sits inside while parse_is_truncated(response.body). It runs only when a bucket listing comes back truncated and the gem asks for the next page, which the comment above it puts at more than 1000 objects. The agent reads the tests to see whether anything gets there. Five test files hold 72 tests, and every listing they stub says <IsTruncated>false</IsTruncated>. On Ruby 3.3 the suite runs and 71 pass; the one failure, test "objects" in test/bucket_test.rb, is a stubbing problem unrelated to URIs. So the gem looks healthy on Ruby 3 until someone lists a large bucket. The agent writes the missing test with two stubbed responses, the first truncated and ending on a key that contains a space and an ampersand, and it fails with the NoMethodError at bucket.rb:154.

Next it follows the value to see what encoding it needs. The marker goes to Connection.parse_params in lib/s3/connection.rb, which builds marker=<value> by plain string interpolation, so the call on line 154 is the only encoding the marker ever gets. On Ruby 2.7, URI.escape turns a space into %20 and leaves &, =, ? and / alone. A key such as photos/a b&c.jpg becomes marker=photos/a%20b&c.jpg, which a server reads as a shorter marker followed by a stray parameter. URI::DEFAULT_PARSER.escape reproduces that output exactly, old bug included. A query value needs URI.encode_www_form_component, which yields photos%2Fa+b%26c.jpg, and the agent's test asserts the full query string.

It states the one thing it cannot settle from the code: encode_www_form_component writes a space as +, the form convention, and whether the service on the other end reads + as a space in a query string has to come from that service's documentation. Where the percent form is required, URI.encode_uri_component writes %20, though it exists only on newer Rubies, so the agent checks the gemspec's supported versions before choosing it. It also greps for URI.encode, URI.unescape and URI.decode, and for each hit decides between a path, a query value and a whole URL before touching it.

Start it with one API call

POST https://api.phoenix.so/o/{org}/sessions with this body:

{
  "agent": "claude",
  "input": "This project fails on Ruby 3 with `NoMethodError: undefined method 'escape' for module URI` (Rubies before 3.3 end the message `for URI:Module`). Fix it. Find every call to URI.escape, URI.encode, URI.unescape and URI.decode. For each call, trace where the value ends up: a path segment, a whole path, a query-string value, a form body, or a complete URL. Check whether any test reaches the call; if none does, write one that does and show it failing with the NoMethodError on the newest Ruby you can install. Replace each call with the encoder that fits its destination: URI.encode_www_form_component or URI.encode_uri_component for a single query value or path segment, the project's existing path encoder (for example Addressable) for a whole path if it already uses one, and URI::DEFAULT_PARSER.escape only when the old output must be preserved byte for byte. For each replacement, show the old and new output for a value containing a space, an ampersand, a slash and a non-ASCII character, and say whether the old output was already wrong. Check the gemspec and CI configuration for the oldest supported Ruby before using a method that only newer Rubies have. Do not add a new dependency. Run the full suite and report failures that have another cause separately. Open a pull request.",
  "resources": [
    {
      "url": "https://github.com/your-org/your-app"
    }
  ]
}
  1. Get an API key. In the console, open Settings, then API keys, and create a key for your org. Send it in the Authorization header as a Bearer token.
  2. Send the request. POST the body in request.json to https://api.phoenix.so/o/{org}/sessions, with your repository as the resources URL. The run starts in its own cloud sandbox.
  3. Read what came back. Follow the run on its live stream or wait for the run.completed webhook, then read the result and its trajectory.

What comes back

A pull request that changes one expression per call site and adds a test for each path that had none. For a library like s3 that is a single line in lib/s3/bucket.rb and one new test that walks a truncated listing. The description shows the new test failing with the NoMethodError before the change and passing after it, with the query string the test asserts.

For every replaced call the description has a small table: the input, what URI.escape used to return, and what the new call returns. Where the two differ, it says which is correct for that position in the URL. In the s3 case the difference is the point, since a key containing & was sent wrongly before Ruby 3 ever removed the method, and the reviewer sees that as a behaviour fix they are agreeing to, not a silent side effect.

The suite's result is reported as found. An existing failure with another cause is named with its file and test and left alone. What the reviewer verifies outside the sandbox is the open question the description lists, for example how the remote service decodes a +, with a real request against a real bucket.

Run it on every failed CI run

When CI moves to Ruby 3, every job that reaches a URI.escape call goes red with the same message. Create an automation in the Project with a github trigger on workflow_run.failure and this prompt saved in it. The trigger puts the failed run's event after the prompt, and the agent starts from the failing workflow instead of from a guess.

The trigger fills in nothing in the prompt, so keep the prompt general, as this one is. Runs from forks never fire it. A red main branch can fail many workflows in a few minutes, so set the automation's max runs per hour low; an event past the cap waits as a queued run and starts when the hour has room. An automation's run is not tied to an API key, so read its result on the session or in the pull request it opens.

The use case this job follows, with its automation and trigger: Fix a bug from an alert.

Why run it on Rush?

The call that breaks is often one no test reaches, and proving a fix means building the situation that reaches it: a second page of results, a redirect, a filename with a space. That takes a few experiments on more than one Ruby. In its own sandbox the agent installs the old and the new interpreter, prints what each encoder returns for the awkward inputs, and keeps those outputs in the session. A reviewer reads the comparison instead of working out percent-encoding rules from memory.

The alternative most teams reach for is a global search and replace with whichever method the first search result recommends, which removes the exception and can change what gets sent over the wire. A run that has to show old and new output for each call makes that difference visible before merge. Because the agent works on a branch in its own machine, a wrong first attempt costs nothing, and the pull request carries only the version that passed.

Frequently asked questions

What replaces URI.escape?

It depends on what is being escaped. For one query value or form field, URI.encode_www_form_component. For one path segment where a space must be %20, URI.encode_uri_component on Rubies that have it. URI::DEFAULT_PARSER.escape gives the old output unchanged, which is right only if the old output was right.

Why was URI.escape removed?

It escaped a string without knowing whether it was a path, a query value or a whole URL, so it left characters such as & and = untouched even where they change the meaning. That made it wrong for most of the uses people put it to. Ruby marked it obsolete and then removed it in 3.0.

Can I use CGI.escape?

For a query value, yes. It gives the same result as URI.encode_www_form_component for ordinary input, including + for a space. It is not a drop-in for escaping a whole path or URL, where slashes must survive.

The error is raised inside a gem. Will the agent patch the gem?

No. It reads the backtrace, finds the gem and version, and looks for a release that no longer calls the removed method. The pull request then bumps that dependency and shows the failing path working. If no fixed release exists, the result says so and names the line upstream.

Will the new encoding change URLs my service already generates?

It can, and the pull request shows where. For each call the description lists old and new output for inputs with spaces and reserved characters. If stored or signed URLs depend on the old form, the agent keeps the old output with URI::DEFAULT_PARSER.escape for that call and says why.

More jobs like this: Fix a bug from an alert

Keep reading