hcl
hcl copied to clipboard
cannot find package "github.com/hashicorp/hcl/v2"
I am trying to write a provide following the tutorial on link.[https://www.terraform.io/docs/extend/writing-custom-providers.html]
When I try to do go get github.com/hashicorp/terraform-plugin-sdk/plugin
I have the below error:
package github.com/hashicorp/hcl/v2: cannot find package "github.com/hashicorp/hcl/v2" in any of:
/usr/local/go/src/github.com/hashicorp/hcl/v2 (from $GOROOT)
/Users/federicoolivieri/go/src/github.com/hashicorp/hcl/v2 (from $GOPATH)
package github.com/hashicorp/hcl/v2/hclsyntax: cannot find package "github.com/hashicorp/hcl/v2/hclsyntax" in any of:
/usr/local/go/src/github.com/hashicorp/hcl/v2/hclsyntax (from $GOROOT)
/Users/federicoolivieri/go/src/github.com/hashicorp/hcl/v2/hclsyntax (from $GOPATH)
I then tried go get github.com/hashicorp/hcl/v2
but I have pretty much the same error.
Hi @lvrfrc87!
It looks like you are using Go in "GOPATH mode", which is a legacy mode that doesn't understand Go Modules and therefore can't install HCL v2. HCL 2 was published after Go Modules was introduced, so it can be automatically installed only in Go Modules mode. (For more information, see our Version Selection wiki page.)
If you aren't able to use Modules mode then you may be able to get this to work with some additional manual steps, using the minimal module compatibility mechanisms. I must admit I'm a bit rusty on how exactly that works but I think the following steps should allow you to use HCL v2 in a codebase that isn't yet using modules mode:
- Run
go get github.com/hashicorp/hcl
to install the master branch (HCL 1) into yourGOPATH
. -
cd ~/go/src/github.com/hashicorp/hcl
-
git checkout hcl2
to switch to thehcl2
branch -
cd -
to return back to the directory you were working in before. - When using HCL, import it still as
github.com/hashicorp/hcl
instead of asgithub.com/hashicorp/hcl/v2
.
That step five is the one I'm not totally sure about. My understanding/memory is that in minimal compatibility mode Go will accept that the packages within HCL refer to each other as .../hcl/v2/...
even though it's actually installed in just .../hcl/...
.
Hi @apparentlymart,
Thank you very much for your response. Really appreciated your quick feedback.
I read about the GOPATH
and module stuff. Weird though, as I have a fresh install of go ( I should have mentioned that) go version go1.14.2 darwin/amd64
I ll give a try and let you know!
If you aren't working in a legacy codebase that needs to keep using GOPATH
mode then I think you can force modules mode on by initializing your codebase as a Go Module using the go mod init
command. There are some other ways to do it too, but including a go.mod
file in your module root directory (that's what go mod init
generates) has the benefit that anyone else using your same repository should also see it behave in modules mode, whereas the other options are for your local system only.
FYI I ran into this issue while trying to install a Terraform binary using go get
and had to do a bit of digging to understand the problem. It had been a while since I was using Go for anything other than as a command-line end-user, so this was confusing for me and left me stumped for a bit.
The problem command was a simple go get github.com/terraform-linters/tflint
to pull tflint down onto my machine (I needed a build of master due to hashicorp/vscode-terraform#229); this single command run from an arbitrary directory was giving the OP's error.
For anyone else who might run into this, I discovered that you can work around by setting the env var GO111MODULE=on
:
# works
GO111MODULE=on go get github.com/terraform-linters/tflint
I'm not sure if this is the right way to solve the problem though, I haven't been around Go modules for long enough to know if this is just standard practice.
Hi @lsegal,
GO111MODULE=on
is a way to force the go
command to work in "modules mode" even though you are running it inside your GOPATH
. That's a fine way to go if you're happy working inside GOPATH
and wish to continue doing so.
I think with recent versions of Go modules mode ought to be activated by default for tflint
because it has a go.mod
file inside, but perhaps there are some special quirks around that initial go get
because the tflint
source code isn't yet available on your system and so the toolchain can't check for go.mod
being present before running it. I usually clone Git repositories containing Go modules using git clone
rather than go get
, but setting GO111MODULE=on
seems like a reasonable alternative for working with go get
inside a GOPATH
.
GO111MODULE=on
is a way to force thego
command to work in "modules mode" even though you are running it inside yourGOPATH
. That's a fine way to go if you're happy working insideGOPATH
and wish to continue doing so.
The thing is I was not running this inside my GOPATH.
C:\Users\Loren
λ go get github.com/terraform-linters/tflint
package github.com/hashicorp/hcl/v2: cannot find package "github.com/hashicorp/hcl/v2" in any of:
c:\go\src\github.com\hashicorp\hcl\v2 (from $GOROOT)
C:\Users\Loren\go\src\github.com\hashicorp\hcl\v2 (from $GOPATH)
package github.com/hashicorp/hcl/v2/hclparse: cannot find package "github.com/hashicorp/hcl/v2/hclparse" in any of:
c:\go\src\github.com\hashicorp\hcl\v2\hclparse (from $GOROOT)
C:\Users\Loren\go\src\github.com\hashicorp\hcl\v2\hclparse (from $GOPATH)
package github.com/hashicorp/hcl/v2/gohcl: cannot find package "github.com/hashicorp/hcl/v2/gohcl" in any of:
c:\go\src\github.com\hashicorp\hcl\v2\gohcl (from $GOROOT)
C:\Users\Loren\go\src\github.com\hashicorp\hcl\v2\gohcl (from $GOPATH)
package github.com/hashicorp/hcl/v2/hclsyntax: cannot find package "github.com/hashicorp/hcl/v2/hclsyntax" in any of:
c:\go\src\github.com\hashicorp\hcl\v2\hclsyntax (from $GOROOT)
C:\Users\Loren\go\src\github.com\hashicorp\hcl\v2\hclsyntax (from $GOPATH)
C:\Users\Loren
λ echo %GOPATH%
C:\Users\Loren\go
C:\Users\Loren
λ go version
go version go1.14 windows/amd64
Sorry, I don't really understand what you mean here. Any documentation you can link? I am using Fedora 31 with go version 1.13.10.
I have my gopath set in my zshrc as such:
export GOPATH="/home/user/go"
When I try to do:
go get github.com/hashicorp/hcl/v2
I get same as @lsegal 's paste. What do I need to do to have this working properly? I was able to get it to work using @apparentlymart 's workaround, but I'd like to use whatever is the proper way to handle this instead of applying workarounds.
Thanks guys!
@sharkymcdongles
in your homedir:
GO111MODULE=on go get -u github.com/aiven/terraform-provider-aiven
...or whichever package you want. This is on a system with GOPATH set to ~/go. You will then find the binary in ~/go/bin/.
thanks @lsegal , great tip.
I am trying to write a provide following the tutorial on link.[https://www.terraform.io/docs/extend/writing-custom-providers.html]
When I try to do
go get github.com/hashicorp/terraform-plugin-sdk/plugin
I have the below error:package github.com/hashicorp/hcl/v2: cannot find package "github.com/hashicorp/hcl/v2" in any of: /usr/local/go/src/github.com/hashicorp/hcl/v2 (from $GOROOT) /Users/federicoolivieri/go/src/github.com/hashicorp/hcl/v2 (from $GOPATH) package github.com/hashicorp/hcl/v2/hclsyntax: cannot find package "github.com/hashicorp/hcl/v2/hclsyntax" in any of: /usr/local/go/src/github.com/hashicorp/hcl/v2/hclsyntax (from $GOROOT) /Users/federicoolivieri/go/src/github.com/hashicorp/hcl/v2/hclsyntax (from $GOPATH)
I then tried
go get github.com/hashicorp/hcl/v2
but I have pretty much the same error.
go clean --modcache
I am no longer having issues with newer golang versions 1.18+. Seems they made it much more streamlined insofar as setting up dev envs and such. Thanks for all the help! <3
Hi all! I'm sorry things have continued to be confusing here, but I'm glad to hear that recent Go toolchain updates have helped.
At this point GOPATH mode has been obsolete for several Go toolchain versions and so I believe the expectation is that if you are using modern Go that you don't set GOPATH
at all and instead you clone the main codebase you intend to use at a location of your choice in your filesystem and then use the Go toolchain inside that directory. As long as the codebase is modern enough to be a Go module, the module metadata in the repository should be sufficient to select appropriate dependencies and download them into the module cache for compilation.
We aren't equipped to offer general advice on using the Go toolchain in this repository, and it seems like the original request was solved, so I'm going to close this issue. If anyone runs into similar problems later I think it would be better to ask about them in general Go discussion forums rather than here, because what we've been discussing is a general situation with Go modules and not something specific to HCL. Thanks!