{"record":{"id":"ed1bdcb1a706163d","repo":"capistrano/capistrano","slug":"undefined-method-execute-for-main-object","errorCode":null,"errorMessage":"undefined method `execute' for main:Object","messagePattern":"undefined method `execute' for main:Object","errorType":"exception","errorClass":"NoMethodError","httpStatus":null,"severity":"error","filePath":"lib/capistrano/dsl.rb","lineNumber":91,"sourceCode":"    # rubocop:enable Security/MarshalLoad\n\n    def run_locally(&block)\n      SSHKit::Backend::Local.new(&block).run\n    end\n\n    # Catch common beginner mistake and give a helpful error message on stderr\n    def execute(*)\n      file, line, = caller.first.split(\":\")\n      colors = SSHKit::Color.new($stderr)\n      $stderr.puts colors.colorize(\"Warning: `execute' should be wrapped in an `on' scope in #{file}:#{line}.\", :red)\n      $stderr.puts\n      $stderr.puts \"  task :example do\"\n      $stderr.puts colors.colorize(\"    on roles(:app) do\", :yellow)\n      $stderr.puts \"      execute 'whoami'\"\n      $stderr.puts colors.colorize(\"    end\", :yellow)\n      $stderr.puts \"  end\"\n      $stderr.puts\n      raise NoMethodError, \"undefined method `execute' for main:Object\"\n    end\n  end\nend\nextend Capistrano::DSL\n","sourceCodeStart":73,"sourceCodeEnd":96,"githubUrl":"https://github.com/capistrano/capistrano/blob/b54b02fa0ecdf18fad97e7ccbacd4b8d4e342c83/lib/capistrano/dsl.rb#L73-L96","documentation":"`execute` is an SSHKit method that runs a command on remote hosts through the backend established by Capistrano's `on(roles(...)) { ... }` scope. Outside an `on` block there is no backend, so Capistrano defines a top-level `execute` on the DSL purely to catch this beginner mistake: it prints a colored remediation hint to stderr (showing how to wrap the call in `on roles(:app)`) and then raises NoMethodError for main:Object. The error is intentional and the printed example above the exception is the actual fix.","triggerScenarios":"Calling `execute \"whoami\"`, `capture`, or `sudo` (which calls execute) directly inside a `task ... do` block without an enclosing `on` block; calling execute in a helper method invoked outside any `on` scope; running code at file-load time in deploy.rb outside a task/on.","commonSituations":"First Capistrano 3 project (Capistrano 2 allowed top-level `run`); refactoring a task and accidentally moving a line out of the `on` block; defining shared helper methods that call execute and invoking them from the wrong nesting level.","solutions":["Wrap the call in an `on` scope: `on roles(:app) do execute \"whoami\" end` (exactly as the stderr hint shows)","For commands on the deploy machine itself, use `run_locally { execute \"whoami\" }` instead of `on`","If the code lives in a helper, pass the backend explicitly or define the helper inside the `on` block so `execute` resolves against SSHKit::Backend"],"exampleFix":"# before\ntask :check do\n  execute \"whoami\"   # NoMethodError: main:Object\nend\n\n# after\ntask :check do\n  on roles(:app) do\n    execute \"whoami\"\n  end\nend","handlingStrategy":"type-guard","validationCode":"# assert a backend context exists before shelling out\nif defined?(SSHKit::Backend) && SSHKit::Backend.current.is_a?(SSHKit::Backend::ConnectionPool::NilClass)\n  raise \"execute called outside an `on` scope\"\nend","typeGuard":"def inside_sshkit_backend?\n  respond_to?(:execute) && !is_a?(Object) # top-level main:Object stub means we are OUTSIDE `on`\nend\n# practical check: run within `on`/`run_locally` blocks only\nwithin_backend = self.class.ancestors.any? { |a| a.name.to_s.start_with?(\"SSHKit::Backend\") }","tryCatchPattern":"begin\n  execute \"whoami\"\nrescue NoMethodError => e\n  raise unless e.message.include?(\"execute' for main:Object\")\n  run_locally { execute \"whoami\" } # or wrap in on roles(:app)\nend","preventionTips":["Indentation discipline: any execute/capture/upload! line must sit inside an `on ... do` or `run_locally do` block","Define shared remote helpers as methods that take the backend as self (define them inside the on block) or check `respond_to?(:backend)`","Run `cap --dry-run` style smoke loads after refactors to hit the top-level stub early"],"tags":["capistrano","sshkit","execute","no-method-error","scope","beginner-mistake"],"backgroundTag":"api-called-outside-required-scope","analyzedSha":"b54b02fa0ecdf18fad97e7ccbacd4b8d4e342c83","analyzedAt":"2026-08-21T15:52:32.020Z","schemaVersion":2},"datasetVersion":"2026-08-21T18:17:14.833Z"}