Skip to content

Using bool parameter values not intuitive #94

Open

Description

@Emyrk

This does not behave as expected because the value is the string literal"true", not the boolean value true. This should be made more intuitive, or throw a warning?

data "coder_parameter" "ai" {
  name        = "ai"
  display_name = "Use AI?"
  description = "Do you want to use AI for this workspace?"
  type        = "bool"
  form_type   = "checkbox"
  default = false
  order       = 0
}

data "coder_parameter" "ai_prompt" {
  count = data.coder_parameter.ai.value == true ? 1 : 0 
  name        = "prompt"
  display_name = "Prompt"
  description = ""
  type        = "string"
  form_type   = "textarea"
  default = ""
  styling = jsonencode({
    placeholder = "hello world"
  })
}

Activity

  1. changed the title [-]Using `bool` parameters not intuitive[/-] [+]Using `bool` parameter values not intuitive[/+] on Apr 22, 2025
  2. added theissue type on Apr 22, 2025
  3. bpmct commented on Apr 22, 2025

    @bpmct
    Member

    I imagine many folks will run into this confusion. Huge fan of some form of warning or "magic" handling

  4. angrycub commented on Aug 4, 2025

    @angrycub

    While it might be surprising that the type isn't plumbed through the datasource (my assumption here is that we can't produce a constant schema in the provider and are leaning on maps to carry the values), The big trick is to not test for equality. If the line was

    count = data.coder_parameter.ai.value ? 1 : 0 

    Terraform should do automatic type conversion, converting it into a boolean on the fly. However, it will specifically not do it in the equality case.

    Where possible, Terraform automatically converts values from one type to another in order to produce the expected type. If this isn't possible, Terraform will produce a type mismatch error and you must update the configuration with a more suitable expression. Automatic type conversion does not occur when using the equality operator.

    Terraform automatically converts number and bool values to strings when needed. It also converts strings to numbers or bools, as long as the string contains a valid representation of a number or bool value.

    true converts to "true", and vice-versa
    false converts to "false", and vice-versa

    Ref: https://developer.hashicorp.com/terraform/language/expressions/types#type-conversion

    It's a bit of an anti-pattern to explicitly test values against bool constants because TF lets you flex a bit with automatic type conversion. That said, I'm with y'all; folks are going to run into this just because some of the nuances of Terraform aren't super obvious.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    documentationImprovements or additions to documentation

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions