core icon indicating copy to clipboard operation
core copied to clipboard

fix: ignore unset environment variables

Open amcaplan opened this issue 3 years ago • 0 comments

When an env is set for a boolean flag, if the user doesn't actually specify the value in their ENV, it ends up being treated as though they'd specified false. I don't think this is the correct behavior; instead, it should be considered unspecified.

This is especially relevant in 2 cases:

  1. Boolean flags with default: true. As soon as you add an env config, they now effectively default to false.
  2. Boolean flags with mutual exclusivity. If you specify one, oclif thinks you've specified both, and the command fails. You can reproduce easily:
import {Command, Flags} from '@oclif/core'

export default class Repro extends Command {
  static description = 'Reproduce exclusive flags bug'

  static flags = {
    exclusive1: Flags.string({
      required: false,
      env: 'SOME_ENV_VAR_1',
      exclusive: ['exclusive2'],
    }),
    exclusive2: Flags.boolean({
      required: false,
      env: 'SOME_ENV_VAR_2',
      exclusive: ['exclusive1'],
    }),
  }

  static args = []

  async run(): Promise<void> {
    const {flags} = await this.parse(Repro)
  }
}
$ bin/dev repro
# everything works fine

$ bin/dev repro --exclusive1
Error: The following error occurred:
  --exclusive1=true cannot also be provided when using --exclusive2
# ... stack trace ...

$ bin/dev repro --exclusive2
Error: The following error occurred:
  --exclusive2=true cannot also be provided when using --exclusive1
# ... stack trace ...

amcaplan avatar Oct 23 '22 18:10 amcaplan