In vscode-go, is there a way to run the local version of a tool for go:generate statements? - go

In many projects, I have a tools/tools.go file that allows me to have local versions of the tools I use (reference).
I also have go:generate statements in my code to automate the generation of mock files using https://github.com/golang/mock/mockgen. I have a local version of mockgen under a bin folder of my project and I can generate all the mocks of my project by running something like PATH=./bin:${PATH} go generate ./....
Vscode-go recognizes the go:generate statements in the code and can generate code for us.
I would like to know if there is a way to get vscode-go to run mockgen (through gopls.generate) using the local version I have under the bin directory.
With the setting go.alternateTools, vscode-go lets us specify an alternate path for the tools used by the extension (i.e. dlv). I tried to configure that option for mockgen but it didn't change anything. My guess is that it does not work because vscode-go runs mockgen through gopls.generate; it does not run mockgen directly. But this is just a guess.
Below is the error I get if I don't have a mockgen binary in my $PATH:
[Info - 5:44:18 PM] 2021/08/08 17:44:18 xxxxxxx.go:19: running "mockgen": exec: "mockgen": executable file not found in $PATH
operation="generate"
Does anyone have any idea? 😅
Edit: see #Zyl's comment there for a very good alternative.

I've run into the same problem today. Found this solution, which worked for me if someone needs this in future:
export GOPATH="$HOME/go"
export PATH="$GOPATH/bin:$PATH"
And then run (in the directory where file with the "go:generate" comment is located)
go generate

Related

Installing Julia packages using a .toml file?

I am totally new to Julia!
I would like to install a large number of packages for a project I'm joining.
The project comes with a "Project.toml" file
It seems like there should be a nice way to install all the packages I need for the project, perhaps using the Project.toml file
However, I have not yet found any site that indicates how I might be able to do this.
Could anyone please let me know if what I am doing is possible, and if so, point me to a reference that would show how?
If your Project.toml is located in a folder, like myproject/Project.toml, you can simply start Julia with julia --project=/path/to/myproject, and it will automatically detect the Project.toml file in myproject.
The first time you activate this project, in the REPL, you can switch to Pkg mode by typing ], and type the command instantiate. This will cause the package manager to download and install all of the packages listed in Project.toml and their dependencies.
Another way to switch between projects during interactive use is to run activate /path/to/myproject in Pkg-mode in the REPL.
How to install julia packages from a Project.toml
First, you will have navigate to the folder containing your Project.toml.
cd ../your-folder-containing-the-project.toml-file
in your terminal:
julia --project=.
]
instantiate
or
julia --project=. -e 'using Pkg; Pkg.instantiate()
The other answers went already to the point, but I want to add another important aspect.
If this project comes "only" with a Project.toml file, you will be able to install "sone version" of these packages, eventualy the Project.toml may also give you a range of versions known to work with the project you have been given.
But if this project comes also with a Manifest.toml file you will be able to recreate on your pc the exact environment, will all the exact versions of all dependent packages recursivelly, of the guy that passed you the project, using the ways desctibed in detail in the other answers (e.g. ] activate [folder]; instantiate).

Understanding go mod and cause of package is not in GOROOT

I'm trying to play around with the lightning network. I have cloned the repo, and on disk placed it here (I'm using windows):
C:\Users\hallibut\Documents\GitHub\lnd
I'd like to run any of the tests in itest, lets say testMultiHopPayments. The cli commands I'm using after I cd into the above location is:
go test itest -run testMultiHopPayments
However, I keep getting the error:
package itest is not in GOROOT (C:\Program Files\Go\src\itest)
I've read through the various posts on this error, but I'm still not quite sure why it happens, and it's likely because I don't fully understand the go module (I'm new to go). This article, was probably the best in helping me understand the structure and env variables:
https://golangbyexample.com/workspace-hello-world-golang/
My understanding from the various readings is that whatever directory the go.mod file is in, indicates the module level directory. Prior to version 1.13 there was a required directory and structure, but now that should not be an issue if you're using at least version 1.13 and modules. I'm using 1.17.1. This is somewhat of an assumption or inference, but I believe everything lower in the directory structure is part of a package to be installed as part of the module (and is defined by the package keyword). However, I don't understand why a package with source code within a subdirectory would be missing/throw the aforementioned error. I've also tried running:
go mod install github.com/lightningnetwork/lnd/lntest/itest
That doesn't seem to do anything/has not effect on the error. What am I not understanding about packages? Looking at the go.mod file I observe that itest is not specifically defined anywhere… Not sure if that's required. Also, I assume I've got to run some build process prior? I attempted this with:
go install -v ./...
If you're using VS Code and Go Modules, you need to "Open folder" and point to the cloned repo, to get around that error

How can I install a specific version of golint for use globally?

I'm trying to create a docker image for use in a build pipeline that has various tools pre-installed for building and testing go projects. One tool we need is golint but I'm struggling to see how to install a specific version of it. The reason I want to lock down the version is to avoid accidental / unwanted / unintended breakages at a later date.
For a start, looking here the versions are not exactly in an easy-to-type format.
Also when I try to use the following command
go get -u golang.org/x/lint/golint#v0.0.0-20181217174547-8f45f776aaf1
I get an error
go: cannot use path#version syntax in GOPATH mode
Googling has so far yielded very few relevant results...
Is what I'm trying to do possible? Many thanks!
You need to be in go module mode to get code of a specific version, since in addition to downloading the code, the version is recorded in the go module file.
The easiest way to do this would be to create an empty directory, run go mod init, which will create a go.mod file.
Then, you can run go get golang.org/x/lint/golint#v0.0.0-20181217174547-8f45f776aaf1, which will add golint at that version to your go.mod file. You can then run go install golang.org/x/lint/golint from within that directory, which will install golint at the version specified into your $GOBIN directory (which defaults to $GOPATH/bin).

GO API installation fails "evq/chromaticity"

I am trying to install chromaticity on my own machine for testing, and no matter what i do i will always hit the error seen in this picture installation error
I dont know why it happened i tried searching but i found nothing online. my question is does anyone know why it happens? or can point me to the right direction? i have checked the folders and yes there are no GO files in there but i dont see why that is a problem
The api could be found here: https://github.com/evq/chromaticity
This is not an issue (as in bug) on the project, rather an issue due to lack of documentation on how to build the project itself.
If you look at the Makefile file on the root directory, you'll notice that static/static.go is a generated file as part of the build process. Such file is usually not committed to the repo so you'll need to build it yourself. To do so, you'll need to have go-bindata installed.
Here's what you need to do in order to build the project successfully:
Get the go-bindata package
go get -u github.com/jteeuwen/go-bindata/...
Get the project
go get github.com/evq/chromaticity
Go the project root directory
cd [...the chromaticity project root..]
Run make to generate the static/static.go file
make
Build/ install the project
go install
Update:
Noticed from your screenshot that you're using Windows, in that case you may need to workaround the issue of running Makefile in Windows. See here for possible solution: How to run a makefile in Windows?
I've run into the same issue when trying to "get" and then install this project. I looked into the code and there is no trace of Asset() function in github.com/evq/chromaticity/static. Moreover git history does not show any .go files in static/ directory. Personally, I would create issue in the project and/or look for different repo containing desired functionality.

How to build Golang blog from github?

Im not very good with Go and I am having a lot of problems with understanding how common website features are made, so I thought it would be good to see a real example. I tried building https://github.com/golang/blog but its not working.
My gopath is apparently C:/Users/me/go as it should be.
*Edit Except if I run cd $GOPATH/src, it says C:\src doesnt exist, it looks in C: not C:/Users
Method 1. (running go get -u golang.org/x/blog)
I open Powershell and run that in my Users/me/go/src directory and it says:
can't load package: package golang.org: no Go files in
C:\Users\me\go\src\golang.org
But it does download the source files. So its basically this step?
'u can manually git clone the repository to $GOPATH/src/golang.org/x/blog.'
Then I dont know where to run go build or what to run. I tried
go build -o blog.exe ./blog
and it says
can't load package: package blog: cannot find package "blog" in any of:
C:\Go\src\blog (from $GOROOT)
C:\Users\me\go\src\blog (from $GOPATH)
I tried running the same command in different directories of the project and doesnt work.
I'll try to answer your questions. (Note that I am a Linux user, so there may be some discrepancies with the Windows commands below. You may want to follow these directions: http://www.wadewegner.com/2014/12/easy-go-programming-setup-for-windows/ to setup the GOROOT environment variable.)
For method 1, the -u flag tells go to update the source code. Since you haven't downloaded it before, it lets you know with the error you see. What you want to run is go get golang.org/x/blog.
To build the package, you first want to change the directory (cd) to the package root, so cd %GOPATH%\src\golang.org\x\blog or cd C:\Users\me\go\src\golang.org\x\blog. Next, you want to run go build. Then, you can run the output file, which should automatically be named blog.exe.
Hopefully this helps! :)

Resources