Skip to content

CLI parser breaks when values for global flags are the same as command-specific flag names #4966

@yhakbar

Description

@yhakbar

Describe the bug

When a value is used for a global flag that is named the same as a command-specific CLI flag, the Terragrunt CLI parser will prevent the command-specific CLI flag from being parsed correctly.

Steps To Reproduce

$ mkdir -p {queue-include-dir, not-queue-include-dir}/{foo, bar, baz}
$ touch {queue-include-dir, not-queue-include-dir}/{foo, bar, baz}/{terragrunt.hcl, main.tf}
$ terragrunt run --all --working-dir queue-include-dir --queue-include-dir foo -- plan
09:40:56.479 INFO   Using runner pool for stack queue-include-dir
09:40:56.484 INFO   The runner-pool runner at queue-include-dir will be processed in the following order for command queue-include-dir:
- Unit ./bar
- Unit ./baz
- Unit ./foo

09:40:56.525 INFO   [foo] tofu: Initializing the backend...
09:40:56.525 INFO   [baz] tofu: Initializing the backend...
09:40:56.525 INFO   [bar] tofu: Initializing the backend...
09:40:56.526 INFO   [bar] tofu: Initializing provider plugins...
09:40:56.526 INFO   [bar] tofu: OpenTofu has been successfully initialized!
09:40:56.526 INFO   [bar] tofu:
09:40:56.526 INFO   [bar] tofu: You may now begin working with OpenTofu. Try running "tofu plan" to see
09:40:56.526 INFO   [bar] tofu: any changes that are required for your infrastructure. All OpenTofu commands
09:40:56.526 INFO   [bar] tofu: should now work.
09:40:56.526 INFO   [bar] tofu: If you ever set or change modules or backend configuration for OpenTofu,
09:40:56.526 INFO   [bar] tofu: rerun this command to reinitialize your working directory. If you forget, other
09:40:56.526 INFO   [bar] tofu: commands will detect it and remind you to do so if necessary.
09:40:56.526 INFO   [foo] tofu: Initializing provider plugins...
09:40:56.526 INFO   [baz] tofu: Initializing provider plugins...
09:40:56.526 INFO   [foo] tofu: OpenTofu has been successfully initialized!
09:40:56.526 INFO   [foo] tofu:
09:40:56.526 INFO   [baz] tofu: OpenTofu has been successfully initialized!
09:40:56.526 INFO   [foo] tofu: You may now begin working with OpenTofu. Try running "tofu plan" to see
09:40:56.526 INFO   [baz] tofu:
09:40:56.526 INFO   [foo] tofu: any changes that are required for your infrastructure. All OpenTofu commands
09:40:56.526 INFO   [baz] tofu: You may now begin working with OpenTofu. Try running "tofu plan" to see
09:40:56.526 INFO   [foo] tofu: should now work.
09:40:56.526 INFO   [baz] tofu: any changes that are required for your infrastructure. All OpenTofu commands
09:40:56.526 INFO   [foo] tofu: If you ever set or change modules or backend configuration for OpenTofu,
09:40:56.526 INFO   [baz] tofu: should now work.
09:40:56.526 INFO   [foo] tofu: rerun this command to reinitialize your working directory. If you forget, other
09:40:56.526 INFO   [baz] tofu: If you ever set or change modules or backend configuration for OpenTofu,
09:40:56.526 INFO   [foo] tofu: commands will detect it and remind you to do so if necessary.
09:40:56.526 INFO   [baz] tofu: rerun this command to reinitialize your working directory. If you forget, other
09:40:56.526 INFO   [baz] tofu: commands will detect it and remind you to do so if necessary.
09:40:56.547 STDERR [foo] tofu: OpenTofu has no command named "queue-include-dir".
09:40:56.547 STDERR [foo] tofu: To see all of OpenTofu's top-level commands, run:
09:40:56.547 STDERR [foo] tofu:   tofu -help
09:40:56.547 STDERR [baz] tofu: OpenTofu has no command named "queue-include-dir".
09:40:56.547 STDERR [baz] tofu: To see all of OpenTofu's top-level commands, run:
09:40:56.547 STDERR [baz] tofu:   tofu -help
09:40:56.547 STDERR [bar] tofu: OpenTofu has no command named "queue-include-dir".
09:40:56.547 STDERR [bar] tofu: To see all of OpenTofu's top-level commands, run:
09:40:56.547 STDERR [bar] tofu:   tofu -help
09:40:56.548 ERROR  [foo] tofu invocation failed in ./foo
09:40:56.548 ERROR  [bar] tofu invocation failed in ./bar
09:40:56.548 ERROR  [baz] tofu invocation failed in ./baz
09:40:56.549 ERROR  Run failed: 3 errors occurred:

* Failed to execute "tofu queue-include-dir foo plan" in ./bar
  OpenTofu has no command named "queue-include-dir".

  To see all of OpenTofu's top-level commands, run:
    tofu -help


  exit status 1

* Failed to execute "tofu queue-include-dir foo plan" in ./baz
  OpenTofu has no command named "queue-include-dir".

  To see all of OpenTofu's top-level commands, run:
    tofu -help


  exit status 1

* Failed to execute "tofu queue-include-dir foo plan" in ./foo
  OpenTofu has no command named "queue-include-dir".

  To see all of OpenTofu's top-level commands, run:
    tofu -help


  exit status 1


❯❯ Run Summary  3 units  64ms
   ────────────────────────────
   Failed       3
$ terragrunt run --all --working-dir not-queue-include-dir --queue-include-dir foo -- plan
09:41:04.041 INFO   Using runner pool for stack not-queue-include-dir
09:41:04.043 INFO   The runner-pool runner at not-queue-include-dir will be processed in the following order for command plan:
- Unit ./foo

09:41:04.080 INFO   [foo] tofu: Initializing the backend...
09:41:04.081 INFO   [foo] tofu: Initializing provider plugins...
09:41:04.081 INFO   [foo] tofu: OpenTofu has been successfully initialized!
09:41:04.081 INFO   [foo] tofu:
09:41:04.081 INFO   [foo] tofu: You may now begin working with OpenTofu. Try running "tofu plan" to see
09:41:04.081 INFO   [foo] tofu: any changes that are required for your infrastructure. All OpenTofu commands
09:41:04.081 INFO   [foo] tofu: should now work.
09:41:04.081 INFO   [foo] tofu: If you ever set or change modules or backend configuration for OpenTofu,
09:41:04.081 INFO   [foo] tofu: rerun this command to reinitialize your working directory. If you forget, other
09:41:04.081 INFO   [foo] tofu: commands will detect it and remind you to do so if necessary.
09:41:04.107 STDOUT [foo] tofu: No changes. Your infrastructure matches the configuration.
09:41:04.107 STDOUT [foo] tofu: OpenTofu has compared your real infrastructure against your configuration and
09:41:04.107 STDOUT [foo] tofu: found no differences, so no changes are needed.

❯❯ Run Summary  3 units  64ms
   ────────────────────────────
   Succeeded    1
   Excluded     2

Expected behavior

Users shouldn't have to care about this, and it should be simple for maintainers to have this work.

Nice to haves

  • Terminal output
  • Screenshots

Versions

  • Terragrunt version: 0.91.0
  • OpenTofu/Terraform version: 1.10.6
  • Environment details (Ubuntu 20.04, Windows 10, etc.): macOS

Additional context

Initial investigation indicates that this is caused by faulty logic for removing unrecognized flags during global flag parsing, which is potentially related to allowing OpenTofu/Terraform flag forwarding as arguments in the run command.

I think users are unlikely to run into this bug in the wild, but we should seriously consider switching to some standard popular CLI parsing library.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingcliIssues related to the CLI RedesignpreservedPreserved issues never go stale

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions