Vagrant: If share_folder doesn't exist, create it

Created on 21 Dec 2011  路  4Comments  路  Source: hashicorp/vagrant

When I specify a share_folder in my vagrantfile like so

config.vm.share_folder("www", "/vagrant/www", "./www", :owner => "www-data", :group => "www-data")

vagrant will error out:

dking@desktop78 ~/projects/vagrant-librarian $ vagrant up
There was a problem with the configuration of Vagrant. The error message(s)
are printed below:

vm:
* Shared folder host path for 'www' doesn't exist: ./www

I would like to see vagrant just create the missing host path. Committing an empty folder with a placeholder file into my git repo isn't the answer. In the mean time, this is how I'm working around the limitation in my vagrantfile:

if not FileTest::directory?("./www")
  Dir::mkdir("./www")
end
config.vm.share_folder("www", "/vagrant/www", "./www", :owner => "www-data", :group => "www-data")

Most helpful comment

Okay. I didn't want to make creating a shared folder the default because a simple typo could cause unexpected results (a folder would be created and it would look empty on the VM, which would not be expected).

Instead, I opted to get shared folders another option, the :create flag. If this flag is set to true (false by default), then it will attempt to create the folder if it does not exist. For example:

config.vm.share_folder "www", "/vagrant/www", "./www", :create => true

Simple enough?

Pushed, will be part of next release.

All 4 comments

Okay. I didn't want to make creating a shared folder the default because a simple typo could cause unexpected results (a folder would be created and it would look empty on the VM, which would not be expected).

Instead, I opted to get shared folders another option, the :create flag. If this flag is set to true (false by default), then it will attempt to create the folder if it does not exist. For example:

config.vm.share_folder "www", "/vagrant/www", "./www", :create => true

Simple enough?

Pushed, will be part of next release.

That'll do. Thanks Mitchell!

What about?

vm:
* The following settings don't exist: share_folder

@haydenk You're probably using V2 config syntax, share_folder is from V1: http://docs.vagrantup.com/v2/vagrantfile/version.html

Was this page helpful?
0 / 5 - 0 ratings

Related issues

RobertSwirsky picture RobertSwirsky  路  3Comments

dorinlazar picture dorinlazar  路  3Comments

rhencke picture rhencke  路  3Comments

jsirex picture jsirex  路  3Comments

barkingfoodog picture barkingfoodog  路  3Comments