Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Update BuildBlock with valid HCL2Ref #12167

Merged
merged 1 commit into from
Dec 20, 2022
Merged

Conversation

nywilken
Copy link
Contributor

@nywilken nywilken commented Dec 16, 2022

While looking into #11932 I found that Packer will throw an error when an unknown source is referenced from within the sources attribute for a build block. The hcl.Diagnostics error includes a Subject, which should highlight where in the HCL2 configuration file the unknown references resides. But when creating the BuildBlock no HCL2Ref data is copied over, thus the Subject is displayed with with zero value for an hcl.Range type.

This change updates the build creation logic to copy the HCL2Ref data from the build block at decode time so that it can be properly referenced downstream.

Closes #11932

Results before Change

~>  packer validate .
Error: Unknown source docker.autogenerated

  on  line 0:
  (source code not available)

Known: [docker.ubuntu]

Results After Change

~>  packer validate .
Error: Unknown source docker.autogenerated

  on build.pkr.hcl line 2:
  (source code not available)

Known: [docker.ubuntu]

@nywilken nywilken requested a review from a team as a code owner December 16, 2022 20:12
@nywilken nywilken added this to the 1.8.6 milestone Dec 16, 2022
@nywilken nywilken added the bug label Dec 16, 2022
@nywilken nywilken force-pushed the nywilken/add-HCL2Ref-build-blocks branch from d2949d5 to c258ce2 Compare December 16, 2022 20:15
Copy link
Contributor

@JenGoldstrich JenGoldstrich left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Functionality wise looks good to me, I think it might be good to add a test change to hcl2template/types.build_test.go to make sure when we pass in an invalid hcl block we get the line number back in the build block, what do you think?

Copy link
Contributor

@JenGoldstrich JenGoldstrich left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@vercel
Copy link

vercel bot commented Dec 19, 2022

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated
packer 🔄 Building (Inspect) Dec 19, 2022 at 10:44PM (UTC)

While looking into #11932 I found that Packer will throw an error when
an unknown source is referenced from within the sources attribute for
a build block. The hcl.Diagnostics error includes a Subject, which
should highlight where in the HCL2 configuration file the unknown source resides.
But when creating the BuildBlock no HCL2Ref data is copied over, thus the
Subject is displayed with with zero value for an hcl.Range type.

This change updates the build creation logic to copy the HCL2Ref data
from the build block at decode time so that it can be properly
referenced downstream.

Closes #11932

Failure due to change in output
```
--- FAIL: TestValidateCommand_ShowLineNumForMissing (0.00s)
    --- FAIL: TestValidateCommand_ShowLineNumForMissing/test-fixtures/validate-invalid/missing_build_block.pkr.hcl (0.00s)
        validate_test.go:377: Unexpected output:   (
                """
                Error: Unknown source file.cho

            -     on  line 0:
            +     on test-fixtures/validate-invalid/missing_build_block.pkr.hcl line 6:
                  (source code not available)

                ... // 4 identical lines
                """
              )
        validate_test.go:379:
FAIL
FAIL    github.com/hashicorp/packer/command     1.002s
```
@nywilken nywilken force-pushed the nywilken/add-HCL2Ref-build-blocks branch from 1652234 to 6cc731c Compare December 20, 2022 15:34
@nywilken nywilken merged commit 9002ac2 into main Dec 20, 2022
@nywilken nywilken deleted the nywilken/add-HCL2Ref-build-blocks branch December 20, 2022 20:38
@github-actions
Copy link

I'm going to lock this pull request 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 related to this change, 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 Jan 21, 2023
# for free to subscribe to this conversation on GitHub. Already have an account? #.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Packer validation message contains empty string for file when file unknown
2 participants