Updated - Vagrant on Windows path limit fix

Photo by Jono on Unsplash.

I previously shared my experience using PuPHPet to host a Linux VM on my Windows computer for MEAN stack development. Since then I've made some improvements to the startup script I use to overcome Windows's 260 character path limit, which npm and bower easily exceed.

Here's the latest script (and another nod to the GitHub discussion that started it):

#!/bin/bash

BASE_FOLDER="/var/tmp/windows-path-limit-fix/"

create()
{
    # Generate a unique but predictable directory by hashing the folder mount point
    # From http://stackoverflow.com/a/1602389
    name=`echo $1 | md5sum | cut -f1 -d" "`
    destination="$BASE_FOLDER$name"

    # Skip setup if the folder already exists
    if [ ! -d "$destination" ]; then

        echo "Creating mount destination: $destination"
        eval "mkdir -p $destination"
        eval "chown -R www-data:www-data $destination"
        eval "chmod 775 $destination"

    fi

    if [ -d "$1" ]; then

        echo "Transferring files from $1 to $destination"
        rsync -Lr --inplace $1/ $destination/

    else

        echo "Creating mount target: $1"
        eval "mkdir -p $1"

    fi

    echo "mounting $destination at $1"
    eval "mount -o bind $destination $1"

}

if [ ! "$1" -o -z "$1" ]; then
    echo "You must specify a folder mount point (recommend an absolute path)"
else
    create $1
fi

And the updated method for calling it:

config.vm.provision :shell, :path => 'puphpet/shell/install-ruby.sh'
config.vm.provision :shell, :path => 'puphpet/shell/install-puppet.sh'
config.vm.provision :shell, run: 'always' do |s|  s.path = 'puphpet/shell/windows-path-limit-fix.sh'  s.args = '/vagrant/node_modules'endconfig.vm.provision :shell, run: 'always' do |s|  s.path = 'puphpet/shell/windows-path-limit-fix.sh'  s.args = '/vagrant/.bower-tmp'end
config.vm.provision :puppet do |puppet|
  puppet.facter = {
    'ssh_username'     => "#{ssh_username}",
    'provisioner_type' => ENV['VAGRANT_DEFAULT_PROVIDER'],
    'vm_target_key'    => 'vagrantfile-local',
  }

While the old script got the job done, it had many inconveniences. The new version makes several improvements:

  • Temporary files are stored in /var/tmp instead of /tmp, meaning they persist after the VM is restarted.
  • Building on that, the temporary directory name is now a hash of the mount point instead of choosing a new name every time. This allows us to detect when the directory is already present then remount it.
  • The updated provisioner syntax in the Vagrantfile causes the script to run during vagrant reload in addition to the usual vagrant up.

These updates avoid repeated setup of the mounted directories. No more npm install after every reload!

Drew

Drew

Hi! I'm Drew, the Wimpy Programmer. I'm a software developer and formerly a Windows server administrator. I use this blog to share my mistakes and ideas.