Search code examples
rubypathenvironment-variablesrake

Rake not exporting PATH


I would like a Rake task to export the path to my command line tool to $PATH, and so I have the following line in my Rakefile:

task :make_path do
  sh 'export PATH=$HOME/Desktop/folder:$PATH'
end

When putting that line export PATH=$HOME/Desktop/folder:$PATH into the terminal directly, the path is prepended to my $PATH variable. However if I run rake make_path nothing changes. Why?

The following task correctly adds execute permission to my command line tool file:

task :chmod do
  sh 'chmod +x my_file'
end

Solution

  • When using sh from within a Rake task, it spawns a shell and executes the command.

    The lifetime of environment variables, PATH being one of them, is until the shell exits.

    So the :make_path task spawns a shell that exports this variable and then exits. Next time a shell is spawned, it has no knowledge of the modifications made to PATH.

    For workarounds you might want to look at this.