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

cannot find package "github.com/hashicorp/hcl/v2" #371

Closed
lvrfrc87 opened this issue May 4, 2020 · 11 comments
Closed

cannot find package "github.com/hashicorp/hcl/v2" #371

lvrfrc87 opened this issue May 4, 2020 · 11 comments

Comments

@lvrfrc87
Copy link

lvrfrc87 commented May 4, 2020

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/v2but I have pretty much the same error.

@apparentlymart
Copy link
Contributor

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:

  1. Run go get github.com/hashicorp/hcl to install the master branch (HCL 1) into your GOPATH.
  2. cd ~/go/src/github.com/hashicorp/hcl
  3. git checkout hcl2 to switch to the hcl2 branch
  4. cd - to return back to the directory you were working in before.
  5. When using HCL, import it still as github.com/hashicorp/hcl instead of as github.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/....

@lvrfrc87
Copy link
Author

lvrfrc87 commented May 5, 2020

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!

@apparentlymart
Copy link
Contributor

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.

@lsegal
Copy link

lsegal commented May 24, 2020

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.

@apparentlymart
Copy link
Contributor

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.

@lsegal
Copy link

lsegal commented May 28, 2020

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.

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

@sharkymcdongles
Copy link

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!

@hraban
Copy link

hraban commented Aug 4, 2020

@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.

@jamalkaksouri
Copy link

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/v2but I have pretty much the same error.

go clean --modcache

@sharkymcdongles
Copy link

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

@apparentlymart
Copy link
Contributor

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!

@apparentlymart apparentlymart closed this as not planned Won't fix, can't repro, duplicate, stale Sep 19, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants