{"record":{"id":"1955fa07c0059edb","repo":"hashicorp/vagrant","slug":"vagrantfile-already-exists-in-this-directory-re","errorCode":null,"errorMessage":"`Vagrantfile` already exists in this directory. Remove it before running `vagrant init`.","messagePattern":"`Vagrantfile` already exists in this directory\\. Remove it before running `vagrant init`\\.","errorType":"exception","errorClass":"Vagrant::Errors::VagrantfileExistsError","httpStatus":null,"severity":"error","filePath":"plugins/commands/init/command.rb","lineNumber":59,"sourceCode":"          o.on(\"--output FILE\", String,\n               \"Output path for the box. '-' for stdout\") do |output|\n            options[:output] = output\n          end\n\n          o.on(\"--template FILE\", String, \"Path to custom Vagrantfile template\") do |template|\n            options[:template] = template\n          end\n        end\n\n        # Parse the options\n        argv = parse_options(opts)\n        return if !argv\n\n        save_path = nil\n        if options[:output] != \"-\"\n          save_path = Pathname.new(options[:output]).expand_path(@env.cwd)\n          save_path.delete if save_path.exist? && options[:force]\n          raise Vagrant::Errors::VagrantfileExistsError if save_path.exist?\n        end\n\n        # Determine the template and template root to use\n        template_root = \"\"\n        if options[:template].nil?\n          options[:template] = \"Vagrantfile\"\n\n          if options[:minimal]\n            options[:template] = \"Vagrantfile.min\"\n          end\n\n          template_root = ::Vagrant.source_root.join(\"templates/commands/init\")\n        end\n\n        # Strip the .erb extension off the template if the user passes it in\n        options[:template] = options[:template].chomp(\".erb\")\n\n        # Make sure the template actually exists","sourceCodeStart":41,"sourceCodeEnd":77,"githubUrl":"https://github.com/hashicorp/vagrant/blob/35f3160f4ad6edc3a9f3aa9570adfc1a4d73aaa5/plugins/commands/init/command.rb#L41-L77","documentation":"`vagrant init` refuses to clobber an existing Vagrantfile. It computes the output path (default ./Vagrantfile, overridable with --output), deletes it only when --force was given, then raises Vagrant::Errors::VagrantfileExistsError if the path still exists. It is a safety guard, not a corruption problem.","triggerScenarios":"Running `vagrant init` a second time in a directory that already contains a Vagrantfile; passing `--output somefile` that already exists without `--force`; leftover Vagrantfile from a previous or aborted init run.","commonSituations":"Re-initializing a project to switch the base box; scaffolding tools or repo templates that pre-create a Vagrantfile; CI jobs that re-run init in a dirty workspace without cleaning.","solutions":["Keep the existing file: open and edit it instead of re-initializing.","Overwrite intentionally: `vagrant init --force <box>` (or `vagrant init -f <box>`).","Write elsewhere: `vagrant init --output Vagrantfile.new <box>` and merge manually."],"exampleFix":"# before\nvagrant init ubuntu/jammy64   # Vagrantfile already present\n# after\nvagrant init --force ubuntu/jammy64","handlingStrategy":"validation","validationCode":"path = 'Vagrantfile'\nif File.exist?(path)\n  warn \"#{path} exists; pass --force to overwrite or edit it instead\"\n  exit 1\nend\nsystem('vagrant', 'init', 'ubuntu/jammy64')","typeGuard":null,"tryCatchPattern":"begin\n  Vagrant::Environment.new.cli(['init', 'ubuntu/jammy64'])\nrescue Vagrant::Errors::VagrantfileExistsError => e\n  warn e.message # decide: --force, --output, or edit the existing file\n  exit 1\nend","preventionTips":["Check for ./Vagrantfile before scripting vagrant init.","Use --force only in disposable environments (CI containers, temp dirs).","Keep the Vagrantfile in version control so any overwrite is recoverable."],"tags":["vagrant","init","vagrantfile","file-exists"],"backgroundTag":"file-already-exists","analyzedSha":"35f3160f4ad6edc3a9f3aa9570adfc1a4d73aaa5","analyzedAt":"2026-08-21T13:34:32.514Z","schemaVersion":2},"datasetVersion":"2026-08-21T18:17:14.833Z"}