Mocha: Unable to set DYLD_LIBRARY_PATH on MacOS

Created on 24 May 2018  路  6Comments  路  Source: mochajs/mocha

Description

Ability to set environment variables.
Mocha spawns a new child process and on MacOS environment variables such as DYLD_LIBRARY_PATH are not carry forward to mocha. This is not desirable because if the project has dependencies, it cannot be satisfied.

Steps to Reproduce

  1. Set DYLD_LIBRARY_PATH on MacOS
  2. Run Mocha on Mac.

Expected behavior: [What you expect to happen]
Expect the child process have the DYLD_LIBRARY_PATH variable set as well.

Actual behavior: [What actually happens]
DYLD_LIBRARY_PATH is not set

Reproduces how often: [What percentage of the time does it reproduce?]
100%

Versions

5.0.5

question

All 6 comments

Please feel free to elaborate/discuss your point of view, but here are my thoughts:

This is more of a feature rather than a bug for a testing framework. For testing a product, generally you do not want system specific environment vars to leak inside. However, if your solution really needs system variables for some reason, you can explicitly set the env variables in the setup, or use something like .env if needed as well.

Running mocha programmatically solves this issue, as well. This is the LOC that spawns mocha in a separate environment

const proc = spawn(process.execPath, args, {
  stdio: 'inherit'
});

The replacement below is basically what you are proposing, but i am currently against such solution, i'd rather have our current encapsulation.

const proc = spawn(process.execPath, args, {
  stdio: 'inherit',
  env: Object.create(process.env),
});

@Bamieh - The main reason we would need this is because of a change Apple made:

Spawning children processes of processes restricted by System Integrity Protection, such as by launching a helper process in a bundle with NSTask or calling the exec(2) command, resets the Mach special ports of that child process. Any dynamic linker (dyld) environment variables, such as DYLD_LIBRARY_PATH, are purged when launching protected processes.
See more: https://forums.developer.apple.com/thread/9233

This is a sample change (on a fork I did to test) I would like to see:

https://github.com/jalantechnologies/mocha/commit/fa04f61ddda2ba3eaf0bd999ebb359cee30f1242

Though there might be better way to achieve the goal.

Here are the default options for spawn():

const defaults = {
  cwd: undefined,
  env: process.env
};

Not having checked, are you saying that our _explicit_ third parameter is overriding the _implicit_ defaults normally provided? If so, we should definitely also pass the existing environment on to the child. @Bamieh, while I see your point to some degree, it also breaks basic OS-level assumptions (e.g., PATH, LD_LIBRARY_PATH, etc. will contain pre-populated values) required for tests.

const spawnOpts = {
  cwd: undefined,
  env: process.env,
  stdio: 'inherit'
};
const proc = spawn(process.execPath, args, spawnOpts);

@plroebuck Line 499 at https://github.com/nodejs/node/blob/master/lib/child_process.js suggest that env should be inherited. The problem might be somewhere else. I guess I need to do a little bit more investigation.

@jjalan @plroebuck my bad, you are right, the env is inherited. i believe the issue is not in mocha's scope. It would be awesome if you provide a follow up answer once you figure it out for future people facing this.

I think its safe to close it for now.

Thanks for the help.

Was this page helpful?
0 / 5 - 0 ratings