All jobs

Fix NoMethodError: Undefined Method exists? for File:Class on Ruby 3.2 and Later

An agent renames every File.exists? and Dir.exists? call to exist?, then proves the entry point starts on Ruby 3.2, which the existing tests may never show.

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

What the agent does

File.exists? was a second name for File.exist?. Ruby marked it deprecated in 2.1, warned about it for years when run with -w, and removed it in 3.2 along with Dir.exists? and FileTest.exists?. Code that still uses the plural name now stops with the NoMethodError in the title; Ruby 3.4 words the same failure as undefined method 'exists?' for class File. The edit is a rename with identical behaviour. The work worth handing to an agent is finding out which commands were broken, whether any test would have told you, and what else from the same release is waiting behind the first error.

hughbien/thymerb, a Pomodoro timer for the terminal, has seven calls: one in the Rakefile, four in lib/thyme/timer.rb, one in lib/thyme/console.rb and one in lib/thyme/tmux.rb. The agent starts from the executable. bin/thyme builds its option parser and calls thyme.load(o) before o.parse!, and Console#load opens with return if block.nil? && !File.exists?(Config::CONFIG_FILE). The executable passes no block, so that line runs on every invocation. On Ruby 3.2 the command fails before it reads a single flag, and that includes thyme -h.

Then it checks what the tests cover. The Rakefile's test task requires each test/*_test.rb: config, console, format and hooks plugin. test_load in test/console_test.rb calls @console.load(opt) with a block, so block.nil? is false and Ruby never evaluates the right side of the &&. No test file constructs a Thyme::Timer or a Thyme::Tmux, where five of the seven calls live. From the code as written, the suite can pass on 3.2 while the installed command is unusable. The agent reports that gap and closes it with two tests: one that calls load with no block against a temporary HOME, and one that runs bin/thyme -h as a subprocess and asserts exit status 0.

It makes the seven renames, installs Ruby 3.2 and the newest stable Ruby in its sandbox, and runs rake test on both. The Gemfile also lists five gems used only to build the project's website (stasis, homeostasis, haml, bluecloth, compass); the tests need ruby-progressbar and nothing else from it, so the agent installs what the tests load and says which gems it skipped and why. Finally it greps for the other names that left in 3.2, such as Fixnum, Bignum, untaint and =~ called on arbitrary objects, and lists any hit without rewriting it in the same pull request.

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.2 or later with a NoMethodError because File.exists? no longer exists. Find every call to File.exists?, Dir.exists? and FileTest.exists? in lib, bin, the Rakefile, the gemspec, tests and any scripts, and rename each to exist?. Change nothing else on those lines. Then work out which user-facing commands were broken: for each executable in bin, trace the path from startup to the first renamed call and say whether it runs on every invocation or only on some. Check whether the existing tests reach any of the renamed calls; if a call is on a startup path that no test reaches, add a test that reaches it, including one that runs the executable with its help flag as a subprocess and asserts it exits 0. Use a temporary HOME for anything that reads or writes dotfiles. Install Ruby 3.2 and the newest stable Ruby, install only the gems the tests load, and run the test task on both; include the commands and their full output. Search for other things Ruby 3.2 removed (Fixnum, Bignum, taint and untaint, Object#=~, Struct::Passwd) and list each hit with file and line, but do not change them in this pull request. If the gemspec has no required_ruby_version, propose one. Open a pull request with the renames, the new tests and the report.",
  "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 with one commit of renames and one of tests. For thymerb the first is seven changed lines across four files, each differing from the old line by one letter. The description has a table of the renamed calls with a column that says when each one runs: at startup of thyme on every invocation, only when a timer is already running, only when cleaning up after one, or only under rake test TEST=.... That column is how a reviewer learns how bad the breakage was for users, which a list of renames does not say.

The description also carries the rake test output from both Ruby versions, the output of bin/thyme -h before and after the change on 3.2, and the list of other removed names with zero or more hits. A reviewer checks that the new subprocess test fails on the commit before the renames, which the agent shows by running it there first. The proposed required_ruby_version is left as a suggestion in the description, because raising or adding it changes who can install the gem.

Run it on every failed CI run

This error usually arrives the week a CI image or a ruby-version matrix moves to 3.2. In the console, create an automation on the Project that has the repository, put the prompt above in it and give it a github trigger on workflow_run.failure. When a workflow fails, Rush starts a run with the saved prompt and adds the event after it, so the agent sees which workflow and commit failed. Add one sentence to the saved prompt telling the agent to stop with a one-line answer when the failing job's log has no NoMethodError for exists?, since the trigger fires for every failed workflow.

The trigger only fires for work by an owner, member or collaborator of the repository, and never for runs from forks. Set the automation's max runs per hour to a low number so a red main branch with several workflows does not start one run per workflow. For a set of gems you maintain, a script on your side can instead send the request once per repository, each with its own request_id, a UUID, so a retried call returns the run it already started.

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

Why run it on Rush?

Proving this fix means running the same code on two Ruby versions and showing the difference. A laptop usually has one Ruby active and a version manager that needs a build toolchain to add another. The agent builds or installs both in its own sandbox, runs the test task and the executable on each, and every command with its output is kept in the session's transcript. The claim in the pull request that thyme -h fails before and passes after is backed by the two outputs.

The timer writes ~/.thyme-pid and ~/.thyme-tmux and sends Unix signals to the process named in the pid file. Exercising those paths on a machine where you already run the tool risks signalling a real timer or deleting its files. In the sandbox there is no other timer, HOME can point anywhere, and nothing the tests create outlives the run.

Frequently asked questions

Is File.exist? exactly the same as File.exists? was?

Yes. They were two names for one method, and exist? has long been the documented one. The same holds for Dir.exist? and FileTest.exist?. The rename works on every Ruby version you are likely to support, so no version check is needed around it.

The error comes from a gem, not from my code. What does the agent do?

It reads the backtrace to name the gem and the version in your Gemfile.lock, then checks whether a newer release uses exist?. If one does and your tests pass with it, the pull request raises that one gem. It does not edit files inside installed gems, and it does not add a monkey patch that defines File.exists? again.

Why not define File.exists? in an initializer and move on?

It works, and it hides every other place the old name is used, including in gems that will drop it at different times. The rename is seven lines in the example above and removes the problem. If you need a stopgap for a dependency you cannot upgrade yet, the agent can add the shim behind a comment that names the gem and the issue, and say so in the description.

My tests pass on Ruby 3.2 but the command crashes. How is that possible?

The removed method only raises when its line runs. In thymerb the one test of load passes a block, which skips the File.exists? check, and nothing tests the timer. The agent traces each executable's startup path for this reason, and adds a test that runs the executable itself.

More Ruby jobs

More jobs like this: Fix a bug from an alert

Keep reading