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

Seemingly random incorrect evaluation of the can() function #13018

Closed
durnik-ivo opened this issue Jun 4, 2024 · 7 comments · Fixed by #13039
Closed

Seemingly random incorrect evaluation of the can() function #13018

durnik-ivo opened this issue Jun 4, 2024 · 7 comments · Fixed by #13039

Comments

@durnik-ivo
Copy link

durnik-ivo commented Jun 4, 2024

Community Note

  • Please vote on this issue by adding a 👍 reaction to the original issue to help the community and maintainers prioritize this request
  • Please do not leave "+1" or other comments that do not add relevant new information or questions, they generate extra noise for issue followers and do not help prioritize the request
  • If you are interested in working on this issue or have submitted a pull request, please leave a comment

Overview of the Issue

I am afraid I am bringing the worst type of bug. I am getting different result for calling can() function between different executions of Packer. I was not able to determine what makes the difference between correct and faulty evaluation. Sometimes it is mostly right, sometimes 50:50 and for some of my templates it is always wrong. It sometimes seems that the more complex the template is the more often I get the wrong result.

I am noticing something similar with try() function. However, its result does not seem to change with different executions but with different arguments provided, so I might create a separate bug report for that later on.

Reproduction Steps

Repeatedly run provided Packer template with packer console . and print local object.

Packer version

From 1.11.0 and 1.9.4

Simplified Packer Template

locals {
  test_data = [
    { key = "value" }
  ]

  test_local = can(local.test_data) ? local.test_data : []
}

Operating system and Environment details

Tested in two environments for two different versions of Packer:

  • Apple MacBook M3 Pro with MacOS 14.5 Sonoma (1.11.0)
  • Ubuntu 20.04 on AWS EC2 instance (1.9.4)

Log Fragments and crash.log files

$ packer console .
> local
> {
  "test_data" = [
    {
      "key" = "value"
    },
  ]
  "test_local" = [
    {
      "key" = "value"
    },
  ]
}
> exit
$ packer console . # In this case, for the second try, can() is evaluated to false and thus [] is assigned. 
> local
> {
  "test_data" = [
    {
      "key" = "value"
    },
  ]
  "test_local" = []
}
> can(local.test_data) # Individual can() executed in the console evaluates correctly
true
@durnik-ivo durnik-ivo added the bug label Jun 4, 2024
@gitbisect
Copy link

gitbisect commented Jun 4, 2024

My trials on intel Mac. Packer version 1.8.5

➜  tmp ls
j.pkr.hcl
➜  tmp cat j.pkr.hcl
locals {
  test_data = [
    { key = "value" }
  ]

  test_local = can(local.test_data) ? local.test_data : []
}
➜  tmp packer console .
> local
> {
  "test_data" = [
    {
      "key" = "value"
    },
  ]
  "test_local" = [
    {
      "key" = "value"
    },
  ]
}
>
>
> local
> {
  "test_data" = [
    {
      "key" = "value"
    },
  ]
  "test_local" = [
    {
      "key" = "value"
    },
  ]
}
>
>
> exit
➜  tmp packer console .
> local
> {
  "test_data" = [
    {
      "key" = "value"
    },
  ]
  "test_local" = [
    {
      "key" = "value"
    },
  ]
}
>

> local
> {
  "test_data" = [
    {
      "key" = "value"
    },
  ]
  "test_local" = [
    {
      "key" = "value"
    },
  ]
}
>

> ^C
➜  tmp packer console .
> local
> {
  "test_data" = [
    {
      "key" = "value"
    },
  ]
  "test_local" = [
    {
      "key" = "value"
    },
  ]
}
> local
> {
  "test_data" = [
    {
      "key" = "value"
    },
  ]
  "test_local" = [
    {
      "key" = "value"
    },
  ]
}
> ^C
➜  tmp packer console .
> can(local.test_data)
> true
>
>
> can(local.test_data)
> true
> ^C
➜  tmp packer console .
> can(local.test_data)
> true
> ^C
➜  tmp packer console .
> local
> {
  "test_data" = [
    {
      "key" = "value"
    },
  ]
  "test_local" = [
    {
      "key" = "value"
    },
  ]
}
>
>
> local
> {
  "test_data" = [
    {
      "key" = "value"
    },
  ]
  "test_local" = [
    {
      "key" = "value"
    },
  ]
}
>
>
> ^C
➜  tmp packer console .
> local
> {
  "test_data" = [
    {
      "key" = "value"
    },
  ]
  "test_local" = [
    {
      "key" = "value"
    },
  ]
}
>
>
> exit
➜  tmp packer console .
> can(local.test_data)
> true
> exit
➜  tmp packer console .
> local
> {
  "test_data" = [
    {
      "key" = "value"
    },
  ]
  "test_local" = [
    {
      "key" = "value"
    },
  ]
}
>
>
> can(local.test_data)
> true
>
>
> local
> {
  "test_data" = [
    {
      "key" = "value"
    },
  ]
  "test_local" = [
    {
      "key" = "value"
    },
  ]
}
> exit
➜  tmp packer console .
> local
> {
  "test_data" = [
    {
      "key" = "value"
    },
  ]
  "test_local" = [
    {
      "key" = "value"
    },
  ]
}
>
>
> exit
➜  tmp packer console .
> can(local.test_data)
> true
>
>
> local
> {
  "test_data" = [
    {
      "key" = "value"
    },
  ]
  "test_local" = [
    {
      "key" = "value"
    },
  ]
}
>
>
> ^C
➜  tmp packer console .
> local
> {
  "test_data" = [
    {
      "key" = "value"
    },
  ]
  "test_local" = [
    {
      "key" = "value"
    },
  ]
}
> ^C
➜  tmp

@durnik-ivo
Copy link
Author

I have tried 1.8.5 amd64 and arm64 binaries on Mac M3 and it shows the same error. The same on Ubuntu 22.04, it just took a bit more tries (I recommend trying with echo local | packer console .).

@gitbisect
Copy link

False once. True twice.
(Heads up to avoid confusion... in the snippet below ➜ tmp is the prompt on my Mac.)

➜  tmp echo local | packer console .
{
  "test_data" = [
    {
      "key" = "value"
    },
  ]
  "test_local" = []
}
➜  tmp echo local | packer console .
{
  "test_data" = [
    {
      "key" = "value"
    },
  ]
  "test_local" = [
    {
      "key" = "value"
    },
  ]
}
➜  tmp echo local | packer console .
{
  "test_data" = [
    {
      "key" = "value"
    },
  ]
  "test_local" = [
    {
      "key" = "value"
    },
  ]
}

@lbajolet-hashicorp
Copy link
Contributor

Interesting, I too am able to replicate this behaviour.

Are you experiencing this only with packer console, or is it a generalised issue?
I'll see if I can debug this, thanks for reporting it with a minimal test case, appreciate it!

@durnik-ivo
Copy link
Author

Thanks!

I observe this also in regular builds, but I was not able to prepare a simple test case, which would run as is, so I went down with packer console.

I have finally tried to prepare the test case for try() and it seems to do something similar. I guess these might be related, so probably not worth creating a separate issue, but let me know.

Code:

locals {
  test_data = [
    { key = "value" }
  ]

  test1 = try(local.test_data)
  test2 = try(local.test_data, false)
}

Output:

$ echo local | packer console .
{
  "test1" = [
    {
      "key" = "value"
    },
  ]
  "test2" = [
    {
      "key" = "value"
    },
  ]
  "test_data" = [
    {
      "key" = "value"
    },
  ]
}
$ echo local | packer console .
{
  "test1" = [
    {
      "key" = "value"
    },
  ]
  "test2" = false
  "test_data" = [
    {
      "key" = "value"
    },
  ]
}

@lbajolet-hashicorp
Copy link
Contributor

Yeah it's likely the same problem.

Looking at the code it seems to be an order of evaluation problem, not sure if the order in which the variables are defined an aggravating factor (I suspect it is by experience with a similar issue in the past), but definitely we should be more resilient to this, and we should build a solid order of evaluation rather than the optimistic one we've adopted here.

I'll see to open a PR soon for this.

Copy link

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.
If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 18, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants