Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion workspaces/config/lib/set-envs.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ const setEnvs = (config) => {
const cliSet = new Set(Object.keys(cliConf))
const envSet = new Set(Object.keys(envConf))
for (const key in cliConf) {
const { deprecated, envExport = true } = definitions[key] || {}
const { deprecated, envExport = true, exclusive } = definitions[key] || {}
if (deprecated || envExport === false) {
continue
}
Expand All @@ -85,6 +85,16 @@ const setEnvs = (config) => {
if (!(envSet.has(key) && !cliSet.has(key))) {
setEnv(env, key, cliConf[key])
}
// when setting an exclusive param, also clear its siblings in env
// so child processes that re-read from env don't see conflicts
if (exclusive) {
for (const exclusiveKey of exclusive) {
const exclDef = definitions[exclusiveKey]
if (exclDef && exclDef.envExport !== false && !exclDef.deprecated) {
setEnv(env, exclusiveKey, defaults[exclusiveKey])
}
}
}
}
}

Expand Down
95 changes: 95 additions & 0 deletions workspaces/config/test/set-envs.js
Original file line number Diff line number Diff line change
Expand Up @@ -241,3 +241,98 @@ t.test('dont set configs marked as envExport:false', t => {
t.strictSame(env, { ...extras }, 'not exported, because envExport=false')
t.end()
})

t.test('exclusive params: clear siblings when setting one', t => {
const { definitions, defaults } = mockDefinitions(t)
const envConf = Object.create(defaults)
const cliConf = Object.create(envConf)

// env has save-dev from user's shell; cli sets save-prod
const env = { npm_config_save_dev: 'true' }
cliConf['save-prod'] = true
const config = {
list: [cliConf, envConf],
env,
defaults,
definitions,
execPath,
globalPrefix,
localPrefix,
npmPath,
npmBin,
}
setEnvs(config)
t.equal(env.npm_config_save_prod, 'true', 'set save-prod from cli')
t.equal(env.npm_config_save_dev, '', 'cleared save-dev to default (exclusive sibling)')
t.end()
})

t.test('exclusive params: skip clearing siblings with envExport:false', t => {
const { definitions, defaults } = mockDefinitions(t)
const envConf = Object.create(defaults)
const cliConf = Object.create(envConf)

// Create modified definitions where save-optional has envExport: false
const modifiedDefinitions = {
...definitions,
'save-optional': {
...definitions['save-optional'],
envExport: false,
},
}

// env has save-optional (with envExport: false); cli sets save-prod
const env = { npm_config_save_optional: 'true' }
cliConf['save-prod'] = true
const config = {
list: [cliConf, envConf],
env,
defaults,
definitions: modifiedDefinitions,
execPath,
globalPrefix,
localPrefix,
npmPath,
npmBin,
}
setEnvs(config)
t.equal(env.npm_config_save_prod, 'true', 'set save-prod from cli')
// save-optional should NOT be cleared because envExport is false
t.equal(env.npm_config_save_optional, 'true', 'did not clear save-optional (has envExport:false)')
t.end()
})

t.test('exclusive params: skip clearing deprecated siblings', t => {
const { definitions, defaults } = mockDefinitions(t)
const envConf = Object.create(defaults)
const cliConf = Object.create(envConf)

// Create modified definitions where save-optional is deprecated
const modifiedDefinitions = {
...definitions,
'save-optional': {
...definitions['save-optional'],
deprecated: 'This config is deprecated',
},
}

// env has save-optional (deprecated); cli sets save-prod
const env = { npm_config_save_optional: 'true' }
cliConf['save-prod'] = true
const config = {
list: [cliConf, envConf],
env,
defaults,
definitions: modifiedDefinitions,
execPath,
globalPrefix,
localPrefix,
npmPath,
npmBin,
}
setEnvs(config)
t.equal(env.npm_config_save_prod, 'true', 'set save-prod from cli')
// save-optional should NOT be cleared because it's deprecated
t.equal(env.npm_config_save_optional, 'true', 'did not clear save-optional (deprecated)')
t.end()
})
Loading