Skip to content

trellis up fails with an "undefined method exists? for class File (NoMethodError)" error on new projects #1523

Closed
@1dkfa

Description

@1dkfa

Version

1.22.1

What did you expect to happen?

I expected for the provisioning of a new Trellis project to succeed.

What actually happens?

The provisioning fails right after running the playbook during the check of an already existing ssh configuration with a following error message:

/tmp/example.com/trellis/lib/trellis/vagrant.rb:108:in `update_ssh_config': undefined method `exists?' for class File (NoMethodError)

  if File.exists?(config_file)
         ^^^^^^^^
Did you mean?  exist?

The error happens on line 108 in the following method:

def update_ssh_config(main_hostname)
regexp = /(Host #{Regexp.quote(main_hostname)}(?:(?!^Host).)*)/m
config_file = File.expand_path('~/.ssh/config')
vagrant_ssh_config = `vagrant ssh-config --host #{main_hostname}`.chomp
if File.exists?(config_file)
FileUtils.cp(config_file, "#{config_file}.trellis_backup")
ssh_config = File.read(config_file)
content = if ssh_config =~ regexp
ssh_config.gsub(regexp, vagrant_ssh_config)
else
ssh_config << "\n#{vagrant_ssh_config}"
end
File.write(config_file, content)
else
FileUtils.mkdir_p(File.dirname(config_file), mode: 0700)
File.write(config_file, vagrant_ssh_config)
end
end

I don't know much about Ruby, but according to the following changelog, the File.exists? method was removed from Ruby in version 3.2.0 and to my knowledge, at least on openSUSE Tumbleweed the minimum available version from the official repositories is 3.3.

The provisioning finishes just fine after replacing the failing File.exists? method with the interpreter-suggested File.exist? method.

if File.exists?(config_file)

Steps to reproduce

trellis new example.com
cd example.com
trellis up

System info

openSUSE Tumbleweed 20240607 x86_64
trellis-cli 1.11.1
ruby 3.3.2 (2024-05-30 revision e5a195edf6) [x86_64-linux-gnu]
Vagrant 2.3.7

Log output

==> default: Running action triggers after up ...
==> default: Running trigger...
==> default: Adding vagrant ssh-config for example.test to ~/.ssh/config
/tmp/example.com/trellis/lib/trellis/vagrant.rb:108:in `update_ssh_config': undefined method `exists?' for class File (NoMethodError)

  if File.exists?(config_file)
         ^^^^^^^^
Did you mean?  exist?
        from /tmp/example.com/trellis/Vagrantfile:163:in `block (4 levels) in <top (required)>'
        from /usr/share/vagrant/gems/gems/vagrant-2.3.7/lib/vagrant/plugin/v2/trigger.rb:359:in `execute_ruby'
        from /usr/share/vagrant/gems/gems/vagrant-2.3.7/lib/vagrant/plugin/v2/trigger.rb:212:in `block in execute'
        from /usr/share/vagrant/gems/gems/vagrant-2.3.7/lib/vagrant/plugin/v2/trigger.rb:181:in `each'
        from /usr/share/vagrant/gems/gems/vagrant-2.3.7/lib/vagrant/plugin/v2/trigger.rb:181:in `execute'
        from /usr/share/vagrant/gems/gems/vagrant-2.3.7/lib/vagrant/plugin/v2/trigger.rb:58:in `fire'
        from /usr/share/vagrant/gems/gems/vagrant-2.3.7/lib/vagrant/action/builtin/trigger.rb:30:in `call'
        from /usr/share/vagrant/gems/gems/vagrant-2.3.7/lib/vagrant/action/warden.rb:48:in `call'
        from /usr/share/vagrant/gems/gems/vagrant-2.3.7/lib/vagrant/action/builder.rb:180:in `call'
        from /usr/share/vagrant/gems/gems/vagrant-2.3.7/lib/vagrant/action/builtin/delayed.rb:21:in `call'
        from /usr/share/vagrant/gems/gems/vagrant-2.3.7/lib/vagrant/action/warden.rb:48:in `call'
        from /usr/share/vagrant/gems/gems/vagrant-2.3.7/lib/vagrant/action/builder.rb:180:in `call'
        from /usr/share/vagrant/gems/gems/vagrant-2.3.7/lib/vagrant/action/runner.rb:101:in `block in run'
        from /usr/share/vagrant/gems/gems/vagrant-2.3.7/lib/vagrant/util/busy.rb:19:in `busy'
        from /usr/share/vagrant/gems/gems/vagrant-2.3.7/lib/vagrant/action/runner.rb:101:in `run'
        from /usr/share/vagrant/gems/gems/vagrant-2.3.7/lib/vagrant/machine.rb:248:in `action_raw'
        from /usr/share/vagrant/gems/gems/vagrant-2.3.7/lib/vagrant/machine.rb:217:in `block in action'
        from /usr/share/vagrant/gems/gems/vagrant-2.3.7/lib/vagrant/environment.rb:631:in `lock'
        from /usr/share/vagrant/gems/gems/vagrant-2.3.7/lib/vagrant/machine.rb:203:in `call'
        from /usr/share/vagrant/gems/gems/vagrant-2.3.7/lib/vagrant/machine.rb:203:in `action'
        from /usr/share/vagrant/gems/gems/vagrant-2.3.7/lib/vagrant/batch_action.rb:86:in `block (2 levels) in run'
exit status 1

Please confirm this isn't a support request.

Yes

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions