Create gopls.exe for offline Windows computer on a Mac - go

I am trying to set up Go on my offline Windows computer, using Visual Studio Code. I have successfully installed VS Code, Go, etc., but am running into trouble installing the Go tools. Unfortunately putting my Windows computer online isn't an option.
The most important tool for me is gopls the Go Language Server tool that provides hover, autocomplete, etc., and will really boost productivity.
I have tried various things, including:
Copying the binary file from $GOROOT/bin/gopls from my Internet-connected Mac to Windows.
Cloning the gopls repo from Github and rebuilding gopls for Windows as covered here by running go build -o gopls.exe main.go.
However I get the following error:
Couldn't start client gopls
Copying the cloned repo to my offline computer and running go install does not work as there are other dependencies needed to fully build and install gopls.
Another tool, staticcheck, provides precompiled binaries for all platforms here.
I have two questions:
Is there a publicly available repo/mirror/site that provides ready-made, downloadable binaries for the main set of Go tools for Windows?
Is there a way that the Go tools can be built for a Windows OS?

The bug on this was setting the environment variable correctly.
To give exact steps:
Browse to your Go src directory: $GOPATH/src/golang.org/x/
Clone Go tools repo: git clone https://github.com/golang/tools.git
cd tools/gopls.
Run GOOS=windows GOARCH=386 go build -o gopls.exe main.go to compile a binary file for gopls that is compatible on Windows.
Repeat steps 3 and 4 for each tool you need to install.
Ref: https://github.com/golang/go/wiki/WindowsCrossCompiling

Related

How can I run Go code with Bazel on Windows platform on my Mac?

I have to test Go code using Bazel on Windows Platform to make sure the tests pass on Windows too. However, I have a Mac machine. I have tried VirtualBox/Vagrant setup and shared the directory. I can get Go to work there too. Bazel gives some version conflict with Visual Studio, but I have a workaround with go test.
Is there a known Windows image for Mac that has Golang, Bazel, etc all setup already that I can simply docker run or something?
I would use an image like filipesilva/bazel-windows-docker-container, made to include bazel.
You can modify its Dockerfile to include a Golang binary archive (like go1.15.7.windows-amd64.zip) you simply unzip under %USERNAME\go
You should end up with a Windows image, to run on your Mac through VirtulaBox, or using a vagrant environment, as described in StefanScherer/docker-windows-box/.

Windows IDE for Haskell

I need to setup a simple and compelling dev environment for small proyects written in Haskell in Windows machines for freshmen.
I have tried several ways to integrate Haskell into VSCode in Windows with no success.
I had a nice setup a few years ago, but I´m finding problems with dependencies recreating that environment:
Editor: Atom
Global binaries build using: stack with ghc-mod hlint stylish-haskell
Atom plugins: language-haskell, ide-haskell, ide-haskell-repl, haskell-ghc-mod
It seems that the "cool" way right now is Language Server Protocol + VScode. ghc-mod seems not to be mantained anymore, Intero has reached EOL, HEI is merging with another project... Having a stable and updated dev environment looks like a moving target.
So, the question is: does anyone have reproducible step-by-step instructions for having VSCode working with Haskell in Windows?
I will test any suggestion in a fresh Windows 10 64bits VM and report the results.
Note: VSCode + Docker container is not an option. Most of the student´s machines have 4GB RAM.
Thanks in advance.
There's a tool called ghcid (not to be confused with ghcide) that, while nowhere near a full-blown IDE, is pretty robust and provides some niceties like re-compiling on save and showing compile errors. It doesn't support go-to-definition though. It has a VSCode plugin.
Here's a possible way of setting up things in Windows:
Download the GHC 8.8.3 binaries for Windows from here.
Download the cabal-install 3.0.0.0 binaries for Windows from here.
Decompress them in some folder.
Add entries to your PATH environment variable so that it has access to the /bin folder of the GHC installation and to the folder containing the cabal executable.
Open a Powershell console.
Run cabal udpate
Run cabal install --install-method=copy --installdir=somefolder ghcid to install the ghcid executable, where "somefolder" is the destination folder. (If the installation fails, try running the command from a Git Bash or Cygwin terminal as a workaround.) Put the destination folder in PATH.
Open (or restart) VSCode and install the "Haskell Syntax Highlighting" and haskell-ghcid plugins.
Go to an example cabal project, use the Ctrl-Shift-P shortcut, and execute the Start ghcid action. The ghcid terminal will appear.
Example of a ghcid session showing an error:
The haskell-ghcid plugin can read a .ghcid file in the project root containing flags that should be passed to the ghcid command.
Extra instructions to set up code formatting:
Install the ormulu formatter by running cabal install ormolu --install-method=copy --installdir=somefolder. Again, make sure that the destination folder in in PATH.
Open (or restart) VSCode and install the ormulu plugin.
Now the "Format Document" and "Format Selection" actions in VSCode will use ormulu.
Another way of installing GHC and getting to ghcid and ormulu could be by using the stack tool, which handles GHC installation by itself.

Downloading and installing Visual Studio Code Go dependencies

I have installed Visual Studio Code and the Go extension on an offline computer and now I need to install the dependencies on that computer.
How can I do it please?
Note: If both of your online and offline PCs have the same OS and architecture, simply copy the $GOPATH directory and you are good to go.
(Note: Replace all $GOPATH with %GOPATH% for Windows OS)
Online
Go to the Go tools that the Go extension depends on, and follow instructions for your OS:
An example for dlv:
go get -u -v github.com/go-delve/delve/cmd/dlv
An example for The Language Server:
go get -u -v golang.org/x/tools/cmd/gopls
That is all.
Offline
For the offline installation you need first copy these files to the offline computer and just run e.g.:
cd $GOPATH/src/github.com/go-delve/delve/cmd/dlv
go install
Repeat steps 1 and 2 for all the packages you need, or simply have a clean GOPATH/src on first PC then compress all, then copy the compressed file and uncompress it then run go install for all packages you need to install.
Go Tools:
Online
The easiest way to install is to run:
go get -u golang.org/x/tools/...
Offline copy
You can also manually git clone the repository to:
$GOPATH/src/golang.org/x/tools

How to install golang tools globally and then use them in multiple projects in different GOPATH?

Most of the golang tools like golint, gopkgs etc are installed like libraries with go get for instance, go get -v github.com/golang/lint/golint or go get -v github.com/tpng/gopkgs. I wonder why these are not just binaries that run through the source code, like go fmt, for example?
Since I work on multiple Go projects at the same time, I prefer having different GOPATH for different projects and so I am having to install these tools into every single project so that I can lint or have auto completions.
Am I doing something wrong or is there a way to install these tools globally and then just use them in multiple projects? How do people handling multiple projects manage this?
EDIT:
I am not asking about vendoring of libraries or about projects using different versions of Go at the same time. My question is about having to install tools like lint and gopkgs into every GOPATH, why they were designed to be treated like libraries instead of being provided as a global binary like go fmt which then could've been used in multiple projects, just like we use go fmt
so I am having to install these tools into every single project so that I can lint or have auto completions.
No you don't: see how Visual Studio Code does it through its Microsoft vscode-go plugin (since its 0.6.53 version, January 2017).
New setting go.toolsGopath, for providing an alternate location to install all the Go tools that the extension depends on, if you don't want them cluttering your GOPATH.
See PR 351 and PR 737
The tools installed in that one common `` folder are:
'gocode': 'github.com/nsf/gocode',
'gopkgs': 'github.com/tpng/gopkgs',
'go-outline': 'github.com/ramya-rao-a/go-outline',
'go-symbols': 'github.com/acroca/go-symbols',
'guru': 'golang.org/x/tools/cmd/guru',
'gorename': 'golang.org/x/tools/cmd/gorename',
'gomodifytags': 'github.com/fatih/gomodifytags',
'impl': 'github.com/josharian/impl'
(and a few others, around godoc, goimports or goreturns, dlv, ...),
That means your GOPATH is composed of:
your project-specific workspace folder
a global go.toolsGopath workspace folder dedicated to tools used by all projects.
The tools are installed/updated in the bin/ subfolder of that latter workspace.
You can do that manually too (without Visual Studio Code): simply set GOPATH to that global tools folder whenever you want to install/update the tools.
Then reset GOPATH to my/project/dedicated/workspace;/tools/workspace, and add both bin/ subfolders to your $PATH/%PATH%.
The OP Nithin adds in the comments:
these tools can be compiled to binaries and if those binaries are available in $PATH, most editors, as far as I tested (based on your post), both vscode and atom (go-plus) will work and wont go get them again.
It is easier to update if they are treated like libraries. (I mean go get)

using stackage on windows

I am trying to use stackage on windows. I cloned the git repo, ran cabal install --only-dependencies, cabal configure, cabal build. Everything works
then dist\build\Stackage\stackage.exe select
Loading Haskell Platform
Loading package database
Narrowing package database
Printing build plan to build-plan.log
Checking for bad versions
authenticate-oauth-1.4.0.8 (FP Complete <michael#fpcomplete.com> #yesodweb) cannot use:
- RSA-2.0 -- ==1.2.*
threepenny-gui-0.4.1.0 (FP Complete <michael#fpcomplete.com>) cannot use:
- aeson-0.7.0.2 -- ==0.6.*
stackage.exe: Conflicting build plan, exiting
the readme mention *.sh scripts like ./patching/scripts/create-tarballs.sh. I tried but failed to run them with cygwin. Are they important?
How can I use stackage on windows?
edit I was able to run the ./patching/scripts/create-tarballs.sh script using msys. But now the error message is:
Loading Haskell Platform
Loading package database
stackage.exe: Missing cabal file "MFlow-0.3.3/MFlow.cabal" in tarball: "patching/tarballs\\MFlow-0.3.3.tar.gz"
I checked the archive: the cabal file is inside.
Windows users are not recommended to install stackage by Haskell Platform installer due to some limitation:
On Windows, it does not provide a complete environment (missing MSYS).
By placing a large number of packages in the global package database, Haskell Platform installations are more easily corrupted.
The choice of package versions conflicts with the needs of many commonly used packages.
Some of the package versions included with the platform have known and severe bugs, and cannot be reliably upgraded.
As for solution to overcome, uninstall the Haskell platform first, then install minghc for windows by the following link: https://github.com/fpco/minghc#readme
Open command prompt run cabal update and cabal install alex happy.
Finally, install stackage.
Update 2015
A new tool has been developed by Commercial Haskell group for project development -- Stack, it can be install along with the latest Haskell Platform (7.10.2).
Features include:
Installing GHC automatically, in an isolated location.
Installing packages needed for your project.
Building your project.
Testing your project.
Benchmarking your project.
I have tried it for haskell web project, it works smoothly.

Resources