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

local variables are evaluated before their dependencies from data source have been computed #13299

Open
torarnv opened this issue Feb 17, 2025 · 9 comments
Labels

Comments

@torarnv
Copy link
Contributor

torarnv commented Feb 17, 2025

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 have a complex packer build "script", where i have a data source that fetches data from a website. I then have locals sections with local variables that reference this data source, and these locals are then referenced in other locals, which in turn are referenced in builders.

I'm seeing that sometimes the builds/sources are evaluating the locals before the root data source has been started, resulting in e.g.:

This object does not have an attribute named "vm_name".

I would have expected any evaluation of expressions like vm_name = local.vm_name on a source to be deferred until all the dependent variables, including data sources, have been resolved. Is this not the case?

Packer version

Packer v1.12.0

@torarnv torarnv added the bug label Feb 17, 2025
@torarnv
Copy link
Contributor Author

torarnv commented Feb 17, 2025

I'm not able to reproduce this with 1.11.2, so it seems like a 1.12.0 regression

@torarnv
Copy link
Contributor Author

torarnv commented Feb 17, 2025

Hmm, I see now that this is a feature of 1.12.0:

core: add support for a DAG-based evaluation on locals and datasources.
A long-standing odditiy of Packer has been the order of evaluation for
locals and data sources. In previous versions of Packer, the
data sources were evaluated first, then the local variables were, making
it impossible to have a datasource that referenced a local variable as
part of its configuration.

I guess there are kinks that still need to be ironed out?

I will use -use-sequential-evaluation for now :)

@torarnv
Copy link
Contributor Author

torarnv commented Feb 17, 2025

cc @lbajolet-hashicorp

@torarnv
Copy link
Contributor Author

torarnv commented Feb 17, 2025

The problem happens inconsistently, so it seems to be some sort of race going on

@torarnv
Copy link
Contributor Author

torarnv commented Feb 17, 2025

reproducer.zip

❯ while test $? -eq 0; do packer build -var version=14 ~/dev/ci/tart-image-bakery/testing/reproducer.pkr.hcl; done
test-image:14.6.1.null.test: output will be in this color.

==> test-image:14.6.1.null.test: Running local shell script: /var/folders/9w/s1fz2zxj7r17db8jvjjlpj_40000gn/T/packer-shell2429734663
    test-image:14.6.1.null.test: test-image:14.6.1
Build 'test-image:14.6.1.null.test' finished after 195 milliseconds 443 microseconds.

==> Wait completed after 195 milliseconds 520 microseconds

==> Builds finished. The artifacts of successful builds are:
--> test-image:14.6.1.null.test: Did not export anything. This is the null builder
test-image:14.6.1.null.test: output will be in this color.

==> test-image:14.6.1.null.test: Running local shell script: /var/folders/9w/s1fz2zxj7r17db8jvjjlpj_40000gn/T/packer-shell3465277797
    test-image:14.6.1.null.test: test-image:14.6.1
Build 'test-image:14.6.1.null.test' finished after 165 milliseconds 241 microseconds.

==> Wait completed after 165 milliseconds 278 microseconds

==> Builds finished. The artifacts of successful builds are:
--> test-image:14.6.1.null.test: Did not export anything. This is the null builder
Error: Error in function call

  on /Users/torarne/dev/ci/tart-image-bakery/testing/reproducer.pkr.hcl line 63:
  (source code not available)

with local as object with 2 attributes,
     var as object with 1 attribute "version".

Call to function "templatefile" failed: foo.pkrtpl.yaml:3,13-19: Unsupported
attribute; Can't access attributes on a primitive-typed value (string)., and 1
other diagnostic(s).

Error: Unsupported attribute

  on /Users/torarne/dev/ci/tart-image-bakery/testing/reproducer.pkr.hcl line 71:
  (source code not available)

This object does not have an attribute named "vm_name".

Error: Unsuitable value type

  on /Users/torarne/dev/ci/tart-image-bakery/testing/reproducer.pkr.hcl line 71:
  (source code not available)

Unsuitable value: value must be known

@lbajolet-hashicorp
Copy link
Contributor

Hey @torarnv ,

Thanks for the report, indeed looks like a DAG-related issue here, does -use-sequential-evaluation fix the issue for you?

I'll investigate this one today, thanks again for bringing this one to our attention

@torarnv
Copy link
Contributor Author

torarnv commented Feb 18, 2025

Thanks @lbajolet-hashicorp ! Yes, -use-sequential-evaluation mitigates the issue :)

@lbajolet-hashicorp
Copy link
Contributor

Awesome, thanks for confirming that the escape hatch does what it's supposed to :)

I've been able to reproduce the error you've encountered in my testing, thanks for providing this, very much appreciated!
It is indeed a non-deterministic error, I'll see to fix this ASAP.

@lbajolet-hashicorp
Copy link
Contributor

Huh, I think I understand the root cause, your merge(var, local) is likely the case here, as it thwarts our dependency detection. If we're lucky it gets evaluated after its dependencies are, but it is not guaranteed since the dependencies are not properly detected and added to the graph.

Fun one, if you want to make it work with the current dependency methods, I would suggest maybe declaring each local/var you need explicitely in the merge here, but I can see why this could be inconvenient.

I'll see what I can do to fix this edge-case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants