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

cty.Value does not implement gob.GobEncoder #187

Open
nywilken opened this issue Jun 14, 2023 · 3 comments
Open

cty.Value does not implement gob.GobEncoder #187

nywilken opened this issue Jun 14, 2023 · 3 comments
Labels
bug Something isn't working dependencies Pull requests that update a dependency file version/bump minor A PR that changes behavior or contains breaking changes template configuration options.
Milestone

Comments

@nywilken
Copy link
Member

nywilken commented Jun 14, 2023

Related Errors

  • cannot use cty.Value{} (value of type cty.Value) as gob.GobEncoder value in variable declaration
  • Datasource.OutputSpec failed: gob: type cty.Type has no exported fields

What's this Error?

~> go get github.com/hashicorp/go-cty v1.13.0
~> make dev
packer-plugin-sdk/rpc/init.go:20:24: cannot use cty.Value{} (value of type cty.Value) 
as gob.GobEncoder value in variable declaration: cty.Value does not implement gob.GobEncoder 
(missing method GobEncode)

In v1.11.0, the github.com/zclconf/go-cty package dropped support for encoding/gob. Gob support is used by the Packer SDK to serialize HCL2 object specs over the wire. If a plugin or Packer upgrades their version of github.com/zclconf/go-cty to one that does not contain Gob support (v1.11.0+) the plugin will build but crash when trying to run a build on an HCL2 template.

The error being reported above is due to a compile type check added to the Packer SDK to alert a consumer of the SDK that they are using an unsupported version of github.com/zclconf/go-cty. The gob.GobEncodercheck was added as a guard to prevent Packer SDK consumers from becoming out of sync with unsupported go-cty versions.

How to fix this Error #

If you are seeing this error then you are running a release of the Packer plugin SDK that now enforces a type check for the encoding/gob support in github.com/zclconf/go-cty. There are a number of ways to fix this issue, which are all meant to be temporary fixes until v1.0.0 of the Packer Plugin SDK, which will move from using encoding/gob for serializing cty Values.

In an effort to provide Packer plugins access to the github.com/zclconf/go-cty enhancements, while still providing access to encoding/gob support the Packer team has forked the go-cty package under github.com/nywilken/go-cty. This is a temporary fork that will be removed once it is no longer needed. Until then plugin developers are encourage to use the fork as a replacement (within the go.mod file) for github.com/zclconf/go-cty.

For a complete list of changes within the go-cty fork for Packer refer to the CHANGELOG

If you are using a version of the Packer Plugin SDK prior to the release of the compile time check within the SDK you can simply use the recommended replace directive (Option 1) to pull in the fork and gain access to the latest enhancements to go-cty.

Option 1: Run packer-sdc fix Command

In an effort to automate the temporary fix, and its removal in the future, a fix sub-command has been added to the packer-sdc command; not to be confused with packer fix which fixes legacy JSON template files.

By running the packer-sdc fix command within the plugin's root project directory the command will check the plugin's go.mod file has a replace directive to the go-cty fork.

go get github.com/hashicorp/packer-plugin-sdk@v0.5.2
go install github.com/hashicorp/packer-plugin-sdk/cmd/packer-sdc@v0.5.2
packer-sdc fix .
go mod tidy

If no replace directive is found it will be automatically added to the go.mod file, which you can then run go mod tidy and go test ./... -v against your plugin to verify the fix has been applied.

In the future release running packer-sdc fix will update versions of the fork, and when the time comes remove it from the mod file.

Option 2: Manual Replace Directive

Add a replace directive to the go.mod file within your plugin's project root directory.
Please check the fork CHANGELOG and tags for the latest tagged release.

go get github.com/hashicorp/packer-plugin-sdk@v0.5.2 &&\
go mod edit -replace "github.com/zclconf/go-cty=github.com/nywilken/go-cty@v1.13.3" &&\
go mod tidy &&\
go test ./...
@nywilken nywilken added bug Something isn't working dependencies Pull requests that update a dependency file version/bump minor A PR that changes behavior or contains breaking changes template configuration options. labels Jun 14, 2023
@nywilken nywilken added this to the 0.5.0 milestone Jun 14, 2023
@nywilken nywilken pinned this issue Jun 16, 2023
nywilken added a commit that referenced this issue Jun 20, 2023
…/go-cty

In an effort to provide Packer plugins access to the github.com/zclconf/go-cty enhancements,
while still providing access to encoding/gob support the Packer team has forked the go-cty package under github.com/nywilken/go-cty.
This is a temporary fork that will be removed once it is no longer needed.

Refer to #187
nywilken added a commit that referenced this issue Jul 5, 2023
…/go-cty

In an effort to provide Packer plugins access to the github.com/zclconf/go-cty enhancements,
while still providing access to encoding/gob support the Packer team has forked the go-cty package under github.com/nywilken/go-cty.
This is a temporary fork that will be removed once it is no longer needed.

Refer to #187
@Prateeknandle
Copy link

After following option - 1 I'm getting error while running go test ./...

$ go test ./...                                             ✹main 
# github.com/hashicorp/hcl/v2/hcldec
../../../pkg/mod/github.com/hashicorp/hcl/v2@v2.17.0/hcldec/spec.go:1627:19: undefined: cty.RefinementBuilder
../../../pkg/mod/github.com/hashicorp/hcl/v2@v2.17.0/hcldec/spec.go:1643:20: wrappedVal.RefineWith undefined (type cty.Value has no field or method RefineWith)

go.mod :
replace github.com/zclconf/go-cty => github.com/nywilken/go-cty v1.12.1 // added by packer-sdc fix as noted in github.com/hashicorp/packer-plugin-sdk/issues/187

packer-plugin-sdk v0.5.1 in go.mod

is this also an issue, anybody faced it, how to resolve it

@Prateeknandle
Copy link

FYI : replace github.com/hashicorp/hcl/v2 => github.com/hashicorp/hcl/v2 v2.16.2 this worked

@nywilken
Copy link
Member Author

After following option - 1 I'm getting error while running go test ./...

$ go test ./...                                             ✹main 
# github.com/hashicorp/hcl/v2/hcldec
../../../pkg/mod/github.com/hashicorp/hcl/v2@v2.17.0/hcldec/spec.go:1627:19: undefined: cty.RefinementBuilder
../../../pkg/mod/github.com/hashicorp/hcl/v2@v2.17.0/hcldec/spec.go:1643:20: wrappedVal.RefineWith undefined (type cty.Value has no field or method RefineWith)

go.mod : replace github.com/zclconf/go-cty => github.com/nywilken/go-cty v1.12.1 // added by packer-sdc fix as noted in github.com/hashicorp/packer-plugin-sdk/issues/187

packer-plugin-sdk v0.5.1 in go.mod

is this also an issue, anybody faced it, how to resolve it

@Prateeknandle thanks again for bubbling this up. As you found version of hcl/v2 2.17.0+ required a newer version of the github.com/zclconf/go-cty. This issue has been addressed in the latest Packer Plugin SDK release v0.5.2, where the go-cty fork now contains all the changes from zclconf/go-cty@v1.13.3.

To update your plugin pull down the latest version of the Packer plugin SDK and re-apply the GoCty fix.

go get github.com/hashicorp/packer-plugin-sdk@v0.5.2
go install github.com/hashicorp/packer-plugin-sdk/cmd/packer-sdc@v0.5.2
packer-sdc fix .
go mod tidy

breed808 added a commit to breed808/packer-plugin-tss that referenced this issue Dec 4, 2023
Manual intervention required to update:

* github.com/hashicorp/hcl/v2
* github.com/hashicorp/packer-plugin-sdk
* github.com/zclconf/go-cty

See hashicorp/packer-plugin-sdk#187 for
further context.
dbast added a commit to mkaczanowski/packer-builder-arm that referenced this issue Dec 23, 2023
dbast added a commit to mkaczanowski/packer-builder-arm that referenced this issue Dec 23, 2023
* Bump github.com/hashicorp/packer-plugin-sdk from 0.5.1 to 0.5.2

Bumps [github.com/hashicorp/packer-plugin-sdk](https://github.com/hashicorp/packer-plugin-sdk) from 0.5.1 to 0.5.2.
- [Release notes](https://github.com/hashicorp/packer-plugin-sdk/releases)
- [Changelog](https://github.com/hashicorp/packer-plugin-sdk/blob/main/CHANGELOG.md)
- [Commits](hashicorp/packer-plugin-sdk@v0.5.1...v0.5.2)

---
updated-dependencies:
- dependency-name: github.com/hashicorp/packer-plugin-sdk
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>

* Apply `packer-sdc fix .`

See also hashicorp/packer-plugin-sdk#187 (comment)

---------

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Daniel Bast <2790401+dbast@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working dependencies Pull requests that update a dependency file version/bump minor A PR that changes behavior or contains breaking changes template configuration options.
Projects
None yet
Development

No branches or pull requests

2 participants