Yarn: "yarn run" script environment is missing environment variables set on command-line

Created on 20 Feb 2018  ยท  6Comments  ยท  Source: yarnpkg/yarn

BUG

What is the current behavior?
When using a yarn "run script" to execute protractor, the environment variables I set on the command-line are not accessible in a test execution by protractor.

In package.json, here is snippet...

....
  "engines": {
    "node": "9.3.0",
    "yarn": ">= 1.0.0"
  },
  "repository": {
    "type": "git",
  },
  "scripts": {
    "foo": "protractor conf.js
  },
devDependencies: {
"protractor": "5.3.0",
}

In conf.js

// An example configuration file.
exports.config = {

  // Capabilities to be passed to the webdriver instance.
  capabilities: {
    'browserName': 'chrome'
  },

  // Framework to use. Jasmine is recommended.
  framework: 'jasmine',

 onPrepare: () => console.log(process.env.FOO) // *** this is undefined when it should say "foo"

  specs: ['*_spec.js'], // whatever pattern your specs are in
};

export FOO=bar;
yarn run foo

Using yarn 1.3.2
protractor 5.3.0
**If the current behavior is a bug, please provide the steps to reproduce.**

Set up conf.js and package.json as mentioned above... then do the following on command line...

export FOO=bar;
yarn run foo
```
You should see "undefined" output when "FOO" should be on the console.

What is the expected behavior?
all environment variable set on command-line should be available in a script executed via "yarn run"
Please mention your node.js, yarn and operating system version.
yarn 1.3.2
node 9.3.0
operating system: Ubuntu 17.10

triaged

Most helpful comment

I found a solution for this... instead of running "export FOO=bar" on command line... the solution was to do it like this on Ubuntu...

FOO=bar yarn run foo

All 6 comments

I found a solution for this... instead of running "export FOO=bar" on command line... the solution was to do it like this on Ubuntu...

FOO=bar yarn run foo

Not sure if this is a Linux issue, but on OSX w/ bash it seems to work as you would expect:

~/Projects/yarn-test ๐Ÿ’   cat package.json
{
  "name": "yarn-test",
  "version": "1.0.0",
  "main": "index.js",
  "license": "MIT",
  "dependencies": {
    "react-native-router-flux": "^4.0.0-beta.24"
  },
  "scripts": {
    "foo": "node conf.js"
  }
}

~/Projects/yarn-test ๐Ÿ’   cat conf.js
console.log('Foo = ', process.env.foo);

~/Projects/yarn-test ๐Ÿ’   export FOO=bar;

~/Projects/yarn-test ๐Ÿ’   yarn run foo
yarn run v1.3.2
$ node conf.js
Foo =  bar
โœจ  Done in 0.14s.

perhaps linux and perhaps unique to protractor. I found a workaround though. Thanks.

While you found a workaround that worked for you, I still think yarn should fix this, so I would vote to reopen it. We got bitten by the fact that e.g. PATH gets reset to the default when calling out to scripts on linux, which can lead to all sorts of weird/bad behavior.

It seems that Linux is ok. So maybe protractor issue? I'm on Ubuntu 18.04 LTS

albert@sela:~/work/python/jupyter-work/yarn-bug$ export FOO=bar
albert@sela:~/work/python/jupyter-work/yarn-bug$ echo $FOO
bar
albert@sela:~/work/python/jupyter-work/yarn-bug$ yarn run foo
yarn run v1.7.0
$ node conf.js
foo =  undefined
FOO =  bar
Done in 0.10s.
albert@sela:~/work/python/jupyter-work/yarn-bug$ cat package.json 
{
  "name": "yarn-bug",
  "version": "1.0.0",
  "description": "reproduce a bug",
  "main": "index.js",
  "license": "MIT",
  "engines": {
    "node": "8.11.2",
    "yarn": ">= 1.0.0"
  },
  "scripts": {
    "foo": "node conf.js"
  }
}
albert@sela:~/work/python/jupyter-work/yarn-bug$ cat conf.js 
console.log('foo = ', process.env.foo);
console.log('FOO = ', process.env.FOO);

Thanks for looking into this. I think my issue has been resolved on my
machine.

On Fri, May 25, 2018, 12:30 PM Jiri Vrany notifications@github.com wrote:

It seems that Linux is ok. So maybe protractor issue? I'm on Ubuntu 18.04
LTS

albert@sela:~/work/python/jupyter-work/yarn-bug$ export FOO=bar
albert@sela:~/work/python/jupyter-work/yarn-bug$ echo $FOO
bar
albert@sela:~/work/python/jupyter-work/yarn-bug$ yarn run foo
yarn run v1.7.0
$ node conf.js
foo = undefined
FOO = bar
Done in 0.10s.
albert@sela:~/work/python/jupyter-work/yarn-bug$ cat package.json
{
"name": "yarn-bug",
"version": "1.0.0",
"description": "reproduce a bug",
"main": "index.js",
"license": "MIT",
"engines": {
"node": "8.11.2",
"yarn": ">= 1.0.0"
},
"scripts": {
"foo": "node conf.js"
}
}
albert@sela:~/work/python/jupyter-work/yarn-bug$ cat conf.js
console.log('foo = ', process.env.foo);
console.log('FOO = ', process.env.FOO);

โ€”
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
https://github.com/yarnpkg/yarn/issues/5381#issuecomment-392111873, or mute
the thread
https://github.com/notifications/unsubscribe-auth/AGDAayK3X7FnfSQfo6iZGF0MSh_6NpA_ks5t2DGbgaJpZM4SMo8T
.

Was this page helpful?
0 / 5 - 0 ratings