jharding / grunt-exec

Grunt plugin for executing shell commands.
https://npmjs.org/package/grunt-exec
Other
248 stars 47 forks source link

Can't use 'source' to run a bash script #36

Closed blakewest closed 8 years ago

blakewest commented 11 years ago

Hi, I'm trying to use this to automate the running of a bash script that sets environment variables in my project. If I just personally run the command 'source ./my_script.sh' then it works just fine. But if I put that same command into grunt-exec, it says that it runs without errors, but the vars aren't actually set.

Any thoughts on what the issue is?

Thanks! - Blake

jharding commented 10 years ago

Can you provide me with an example so I can reproduce the issue?

blakewest commented 10 years ago

Hey, yeah sorry for that before. Here's an example. I made a test project, with a grunt file that looks like this.

module.exports = function(grunt){

  grunt.initConfig({
    pkg: grunt.file.readJSON('package.json'),
    exec: {
      testing: {
        command: 'source ./test_grunt_exec.sh',
      },
    }
  });

  grunt.loadNpmTasks('grunt-exec');
  grunt.registerTask('default', 'exec');
};

Then the test_grunt_exec.sh looks like this...

echo 'Working'

export TESTING="AlsoWorking"

Finally, here's sample shell output when testing this out:

Last login: Wed Dec 25 16:48:39 on ttys006
Macintosh-001ec21cda79:test bwest87$ printenv | grep TESTING
Macintosh-001ec21cda79:test bwest87$ grunt
Running "exec:testing" (exec) task
Working

Done, without errors.
Macintosh-001ec21cda79:test bwest87$ printenv | grep TESTING
Macintosh-001ec21cda79:test bwest87$ source ./test_grunt_exec.sh 
Working
Macintosh-001ec21cda79:test bwest87$ printenv | grep TESTING
TESTING=AlsoWorking
Macintosh-001ec21cda79:test bwest87$ 

As you can see... actually running the command from the terminal correctly sets the environment variables. But if I tried to do it only from the grunt task, the file runs, but the environment vars aren't set. The use case here is that I wanted certain variables set as part of the one start_up grunt task for a project I'm working on. I can tell people to separately run the bash command, but it would be cleaner to just tuck it in as part of grunt.

Any thoughts?

yibuyisheng commented 10 years ago

A new child process will be produced when execute a shell command. So the environment variables you set are not in the main process but the child process which will be killed after executed.

d48 commented 9 years ago

I have a similar use case in trying to set environment variables via bash script before more items are executed. source allows for any variables set to remain in the current context and available for any subprocesses spawned from that script. Having source support would be great

kevinSuttle commented 9 years ago

I think this is an issue with Node in general, isn't it?

gwicksted commented 8 years ago

Yes, this is not a grunt-exec issue nor a Grunt issue nor a NodeJS issue. It is a limitation of processes/subprocesses and env var handling.

Options: 1) echo each environment variable (with delimiters) to stdout and parse them with the callback method.

2) Or output a file that you can parse from within grunt.

3) Define the env vars before calling grunt.

4) Spawn a second grunt process immediately following your existing command (in the same grunt-exec call) and specify only the task(s) that require those env vars to be set. You can access them within grunt via process.env.ENV_VARIABLE or pass them into grunt as args via --argname=value and retrieving them via grunt.option('argname')