Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Environment Variables are lost when merging config files #57

Open
mncharlton opened this issue Aug 20, 2021 · 4 comments
Open

Environment Variables are lost when merging config files #57

mncharlton opened this issue Aug 20, 2021 · 4 comments

Comments

@mncharlton
Copy link

mncharlton commented Aug 20, 2021

When using the plugin my environment variables are lost, I think because they aren't part of the actual config file, but are added to the config, they aren't included in the merged config file (which is purely the two config files, not anything else).

To get this to work for me, I wrote my own function, based on the plugin, that looks like this:

const mergeConfig = (config, filename) => {
  const configJson = require(filename);
  if (configJson.extends) {
    let baseConfigFilename;
    if (configJson.extends.startsWith('.')) {
      baseConfigFilename = path.join(path.dirname(filename), configJson.extends);
    } else {
      baseConfigFilename = require.resolve(configJson.extends);
    }
    const baseConfig = require(baseConfigFilename);
    return deepmerge.all([config, baseConfig, configJson]);
    // eslint-disable-next-line no-else-return
  } else {
    return configJson;
  }
};

So it merges the config, the baseConfig, and the config file rather than just merging the two json files - this appears to work for me.

Happy to try and make a PR to update the plugin if this looks like something that would work for all cases/I'm not missing anything?

@dwightjack
Copy link

@mncharlton @bahmutov I confirm I encountered the same issue.

@mncharlton One question about your fix: if config is the resolved config and baseConfig the configuration JSON file, I'd expect that env variables defined via --env and included in config.env would be overwritten by variables already defined in baseConfig.env. Shouldn't it be the opposite?

An example of the expected behavior:

// base.json
{
  "env": {
    "myVar": "one"
  }
} 

// extended.json
{
  "extends": "./base.json",
  "env": {
    "myVar": "two"
  }
} 

cypress run --config-file extended.json

// myVar === "two"

cypress run --config-file extended.json --env myVar="three"

// myVar === "tree"

@mncharlton
Copy link
Author

if config is the resolved config and baseConfig the configuration JSON file, I'd expect that env variables defined via --env and included in config.env would be overwritten by variables already defined in baseConfig.env. Shouldn't it be the opposite?

I don't know what the correct answer is to be honest 😅
In my 'fix' it works that anything in the resolved config will be overwritten by anything in the baseConfig, and then in turn by anything in the specified config file. This works for my cases, but we don't use the --env flag anywhere, in a case where you do use that this wouldn't work as you say.

@antonyfuentes
Copy link

I had issues with this issue as well. In my particular case what was happening is that on my CI/CD settings I had a scheduled job that was passing a custom value for CYPRESS_baseUrl.

However, that value was getting overwritten by the merge plugin, and as well as other environment variables getting lost as mentioned in the comments above.

In the end I resolved it by doing the following:

const path = require('path');
const merge = require('deepmerge');

function extendConfig(config) {
  // Parsing the config file to determine if the "extends" key is present
  const configFile = require(config.configFile)
  if (configFile.extends) {
    // If the extends key is present, then parse parse it as an object
    const extendedConfig = require(path.join(path.dirname(config.configFile), configFile.extends));
    console.log('\nMerging config from %s with %s \n', config.configFile, configFile.extends)

    // Merging the current config loaded by Cypress with the extended config
    return merge(config, extendedConfig)
  } else {
    console.log('Could not find any extended config, hence returning the original config parsed by Cypress.');
    return config
  }
}

module.exports = (on, config) => {
  // Extending config file if applicable
  return extendConfig(config);
};

@conclavia
Copy link

If you only need to fix this issue for env values and not for configuration used by Cypress itself (which was enough for our use case) then you can just merge the original env back over the result of the plugin:

import cypressExtends from '@bahmutov/cypress-extends';
import merge from 'deepmerge';
 ...
module.exports = ((on, config) => {
  const extended = cypressExtends(config.configFile);
  return { ...extended, env: merge(extended.env, config.env) };
}) as Cypress.PluginConfig;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants