Adopt Go workspace best practices
PATCH 1/1 Adopt Go workspace best practices
Relative imports break when the project is built from within GOPATH, where most Go developers prefer to develop:
device.go:10:2: local import "./ratelimiter" in non-local package tun_darwin.go:10:2: local import "./rwcancel" in non-local package noise-protocol.go:10:2: local import "./tai64n" in non-local package cookie.go:10:2: local import "./xchacha20poly1305" in non-local package
On the other hand, the vendor folder is not supported outside of GOPATH, so it's unclear how/if that ever worked.
Moreover, using go get instead of dep means that dependencies are not pinned, and HEAD is used instead, which makes builds unreproducible.
This adds a Makefile that creates a local GOPATH with a symlink trick, so developing outside GOPATH still works. Not only that, but a system GOPATH is not required at all as long as the Makefile is used. And if the project is cloned in the right place in GOPATH all standard tools will work as expected.
So all workflows should work just as weel or better. The only new requirement is dep, but that's unavoidable for reproducible builds.
Finally, run goimports on all files to format import statements.
From 6dd7ad9747dff4c204a16782466a0b09a7919a15 Mon Sep 17 00:00:00 2001
From: Filippo Valsorda <[email protected]>
Date: Sun, 20 May 2018 14:34:24 -0400
Subject: [PATCH] Adopt Go workspace best practices
Relative imports break when the project is built from within GOPATH, where most Go developers prefer to develop:
device.go:10:2: local import "./ratelimiter" in non-local package tun_darwin.go:10:2: local import "./rwcancel" in non-local package noise-protocol.go:10:2: local import "./tai64n" in non-local package cookie.go:10:2: local import "./xchacha20poly1305" in non-local package
On the other hand, the vendor folder is not supported outside of GOPATH, so it's unclear how/if that ever worked.
Moreover, using go get instead of dep means that dependencies are not pinned, and HEAD is used instead, which makes builds unreproducible.
This adds a Makefile that creates a local GOPATH with a symlink trick, so developing outside GOPATH still works. Not only that, but a system GOPATH is not required at all as long as the Makefile is used. And if the project is cloned in the right place in GOPATH all standard tools will work as expected.
So all workflows should work just as weel or better. The only new requirement is dep, but that's unavoidable for reproducible builds.
Finally, run goimports on all files to format import statements.
--- .gitignore | 1 + Gopkg.lock | 36 +++++++++++++++++++++++++++++++++++- Makefile | 14 ++++++++++++-- README.md | 13 ++++++++++--- conn.go | 3 ++- conn_linux.go | 5 +++-- cookie.go | 7 ++++--- device.go | 3 ++- generate-vendor.sh | 20 -------------------- kdf_test.go | 3 ++- noise-helpers.go | 3 ++- noise-protocol.go | 7 ++++--- noise-types.go | 1 + receive.go | 7 ++++--- rwcancel/rwcancel_unix.go | 3 ++- send.go | 7 ++++--- tun_darwin.go | 7 ++++--- tun_linux.go | 7 ++++--- tun_windows.go | 5 +++-- uapi_darwin.go | 3 ++- uapi_linux.go | 5 +++-- uapi_windows.go | 3 ++- xchacha20poly1305/xchacha20.go | 1 + 23 files changed, 107 insertions(+), 57 deletions(-) delete mode 100755 generate-vendor.sh
.gitignore +1 -0
@@ -1,2 +1,3 @@ wireguard-go vendor +.GOPATH
Gopkg.lock +35 -1
@@ -1,16 +1,50 @@ -# This was generated by ./generate-vendor.sh +# This file is autogenerated, do not edit; changes may be undone by the next 'dep ensure'. + + +[[projects]] + name = "github.com/Microsoft/go-winio" + packages = ["."] + revision = "7da180ee92d8bd8bb8c37fc560e673e6557c392f" + version = "v0.4.7" + [[projects]] branch = "master" name = "golang.org/x/crypto" + packages = [ + "blake2s", + "chacha20poly1305", + "curve25519", + "internal/chacha20", + "poly1305" + ] revision = "1a580b3eff7814fc9b40602fd35256c63b50f491" [[projects]] branch = "master" name = "golang.org/x/net" + packages = [ + "bpf", + "internal/iana", + "internal/socket", + "ipv4", + "ipv6" + ] revision = "2491c5de3490fced2f6cff376127c667efeed857" [[projects]] branch = "master" name = "golang.org/x/sys" + packages = [ + "cpu", + "unix", + "windows", + "windows/registry" + ] revision = "7c87d13f8e835d2fb3a70a2912c811ed0c1d241b" +[solve-meta] + analyzer-name = "dep" + analyzer-version = 1 + inputs-digest = "4aa3076861343acd63ca1f40b2c67c0558cdb89ac5eab3fc85a066f51a578c9a" + solver-name = "gps-cdcl" + solver-version = 1
Makefile +12 -2
@@ -8,15 +8,25 @@ $(error Do not build this for Linux. Instead use the Linux kernel module. See wi endif endif +IMPORT_PATH := git.zx2c4.com/wireguard-go + all: wireguard-go -wireguard-go: $(wildcard *.go) $(wildcard */*.go) - go build -v -o $@ +wireguard-go: $(wildcard *.go) $(wildcard */*.go) Gopkg.* .GOPATH/.ok + cd "$(PWD)/.GOPATH/src/$(IMPORT_PATH)" && GOPATH="$(PWD)/.GOPATH" dep ensure + GOPATH="$(PWD)/.GOPATH" go build -v $(IMPORT_PATH) install: wireguard-go @install -v -d "$(DESTDIR)$(BINDIR)" && install -m 0755 -v wireguard-go "$(DESTDIR)$(BINDIR)/wireguard-go" clean: rm -f wireguard-go + rm -rf bin .GOPATH .PHONY: clean install + +.GOPATH/.ok: + @mkdir -p ".GOPATH/src/$(IMPORT_PATH)" + @rmdir ".GOPATH/src/$(IMPORT_PATH)" + @ln -s ../../.. ".GOPATH/src/$(IMPORT_PATH)" + @touch $@
README.md +10 -3
@@ -44,16 +44,23 @@ Work in progress, but nothing yet to share. ## Building -You can satisfy dependencies with either `go get -d -v` or `dep ensure -vendor-only`. Then run `make`. As this is a Go project, a `GOPATH` is required. For example, wireguard-go can be built with: +Make sure you have Go and `dep` installed, then run `make`. ``` $ git clone https://git.zx2c4.com/wireguard-go $ cd wireguard-go -$ export GOPATH="$PWD/gopath" -$ go get -d -v $ make ``` +Or, with a properly set up GOPATH, run `dep ensure`. + +``` +$ git clone https://git.zx2c4.com/wireguard-go "$(go env GOPATH)/src/git.zx2c4.com/wireguard-go" +$ cd "$(go env GOPATH)/src/git.zx2c4.com/wireguard-go" +$ dep ensure +$ go build +``` + ## License This program is free software; you can redistribute it and/or modify
conn.go +2 -1
@@ -8,9 +8,10 @@ package main import ( "errors" + "net" + "golang.org/x/net/ipv4" "golang.org/x/net/ipv6" - "net" ) const (
conn_linux.go +3 -2
@@ -16,13 +16,14 @@ package main import ( - "./rwcancel" "errors" - "golang.org/x/sys/unix" "net" "strconv" "sync" "unsafe" + + "git.zx2c4.com/wireguard-go/rwcancel" + "golang.org/x/sys/unix" ) type IPv4Source struct {
cookie.go +4 -3
@@ -7,13 +7,14 @@ package main import ( - "./xchacha20poly1305" "crypto/hmac" "crypto/rand" - "golang.org/x/crypto/blake2s" - "golang.org/x/crypto/chacha20poly1305" "sync" "time" + + "git.zx2c4.com/wireguard-go/xchacha20poly1305" + "golang.org/x/crypto/blake2s" + "golang.org/x/crypto/chacha20poly1305" ) type CookieChecker struct {
device.go +2 -1
@@ -7,11 +7,12 @@ package main import ( - "./ratelimiter" "runtime" "sync" "sync/atomic" "time" + + "git.zx2c4.com/wireguard-go/ratelimiter" ) const (
generate-vendor.sh deleted +0 -20
@@ -1,20 +0,0 @@ -#!/bin/bash - -echo "# This was generated by ./generate-vendor.sh" > Gopkg.lock -echo "# This was generated by ./generate-vendor.sh" > Gopkg.toml - -while read -r package; do - cat >> Gopkg.lock <<-_EOF - [[projects]] - branch = "master" - name = "$package" - revision = "$(< "$GOPATH/src/$package/.git/refs/heads/master")" - - _EOF - cat >> Gopkg.toml <<-_EOF - [[constraint]] - branch = "master" - name = "$package" - - _EOF -done < <(sed -n 's/.*"\(golang.org\/x\/[^/]\+\)\/\?.*".*/\1/p' *.go */*.go | sort | uniq)
kdf_test.go +2 -1
@@ -8,8 +8,9 @@ package main import ( "encoding/hex" - "golang.org/x/crypto/blake2s" "testing" + + "golang.org/x/crypto/blake2s" ) type KDFTest struct {
noise-helpers.go +2 -1
@@ -10,9 +10,10 @@ import ( "crypto/hmac" "crypto/rand" "crypto/subtle" + "hash" + "golang.org/x/crypto/blake2s" "golang.org/x/crypto/curve25519" - "hash" ) /* KDF related functions.
noise-protocol.go +4 -3
@@ -7,13 +7,14 @@ package main import ( - "./tai64n" "errors" + "sync" + "time" + + "git.zx2c4.com/wireguard-go/tai64n" "golang.org/x/crypto/blake2s" "golang.org/x/crypto/chacha20poly1305" "golang.org/x/crypto/poly1305" - "sync" - "time" ) const (
noise-types.go +1 -0
@@ -10,6 +10,7 @@ import ( "crypto/subtle" "encoding/hex" "errors" + "golang.org/x/crypto/chacha20poly1305" )
receive.go +4 -3
@@ -9,14 +9,15 @@ package main import ( "bytes" "encoding/binary" - "golang.org/x/crypto/chacha20poly1305" - "golang.org/x/net/ipv4" - "golang.org/x/net/ipv6" "net" "strconv" "sync" "sync/atomic" "time" + + "golang.org/x/crypto/chacha20poly1305" + "golang.org/x/net/ipv4" + "golang.org/x/net/ipv6" ) type QueueHandshakeElement struct {
rwcancel/rwcancel_unix.go +2 -1
@@ -7,9 +7,10 @@ package rwcancel import ( "errors" - "golang.org/x/sys/unix" "os" "syscall" + + "golang.org/x/sys/unix" ) type RWCancel struct {
send.go +4 -3
@@ -9,13 +9,14 @@ package main import ( "bytes" "encoding/binary" - "golang.org/x/crypto/chacha20poly1305" - "golang.org/x/net/ipv4" - "golang.org/x/net/ipv6" "net" "sync" "sync/atomic" "time" + + "golang.org/x/crypto/chacha20poly1305" + "golang.org/x/net/ipv4" + "golang.org/x/net/ipv6" ) /* Outbound flow
tun_darwin.go +4 -3
@@ -7,16 +7,17 @@ package main import ( - "./rwcancel" "encoding/binary" "errors" "fmt" - "golang.org/x/net/ipv6" - "golang.org/x/sys/unix" "io/ioutil" "net" "os" "unsafe" + + "git.zx2c4.com/wireguard-go/rwcancel" + "golang.org/x/net/ipv6" + "golang.org/x/sys/unix" ) const utunControlName = "com.apple.net.utun_control"
tun_linux.go +4 -3
@@ -12,18 +12,19 @@ package main */ import ( - "./rwcancel" "bytes" "encoding/binary" "errors" "fmt" - "golang.org/x/net/ipv6" - "golang.org/x/sys/unix" "net" "os" "strconv" "time" "unsafe" + + "git.zx2c4.com/wireguard-go/rwcancel" + "golang.org/x/net/ipv6" + "golang.org/x/sys/unix" ) const (
tun_windows.go +3 -2
@@ -10,13 +10,14 @@ import ( "encoding/binary" "errors" "fmt" - "golang.org/x/sys/windows" - "golang.org/x/sys/windows/registry" "net" "sync" "syscall" "time" "unsafe" + + "golang.org/x/sys/windows" + "golang.org/x/sys/windows/registry" ) /* Relies on the OpenVPN TAP-Windows driver (NDIS 6 version)
uapi_darwin.go +2 -1
@@ -9,10 +9,11 @@ package main import ( "errors" "fmt" - "golang.org/x/sys/unix" "net" "os" "path" + + "golang.org/x/sys/unix" ) const (
uapi_linux.go +3 -2
@@ -7,13 +7,14 @@ package main import ( - "./rwcancel" "errors" "fmt" - "golang.org/x/sys/unix" "net" "os" "path" + + "git.zx2c4.com/wireguard-go/rwcancel" + "golang.org/x/sys/unix" ) const (
uapi_windows.go +2 -1
@@ -11,9 +11,10 @@ package main import ( "fmt" + "net" + "github.com/Microsoft/go-winio" "golang.org/x/sys/windows" - "net" ) const (
xchacha20poly1305/xchacha20.go +1 -0
@@ -8,6 +8,7 @@ package xchacha20poly1305 import ( "encoding/binary" + "golang.org/x/crypto/chacha20poly1305" )
Signature · 1 line
2.16.3
Quoted 7 lines · Filippo Valsorda
Relative imports break when the project is built from within GOPATH, where most Go developers prefer to develop:
device.go:10:2: local import "./ratelimiter" in non-local package tun_darwin.go:10:2: local import "./rwcancel" in non-local package noise-protocol.go:10:2: local import "./tai64n" in non-local package cookie.go:10:2: local import "./xchacha20poly1305" in non-local package
We also carry a patch for this in nixpkgs for that reason:
https://github.com/NixOS/nixpkgs/commit/b599f672e44270d2c6154b7855ddec11e4d43a93#diff-b3f319067b7e71fdd1f6b94ef51a983aR17
Quoted 23 lines
On the other hand, the vendor folder is not supported outside of GOPATH, so it's unclear how/if that ever worked.
Moreover, using go get instead of dep means that dependencies are not pinned, and HEAD is used instead, which makes builds unreproducible.
This adds a
Makefilethat creates a local GOPATH with a symlink trick, so developing outside GOPATH still works. Not only that, but a system GOPATH is not required at all as long as theMakefileis used. And if the project is cloned in the right place in GOPATH all standard tools will work as expected.So all workflows should work just as weel or better. The only new requirement is dep, but that's unavoidable for reproducible builds.
Finally, run goimports on all files to format import statements.
_______________________________________________
WireGuard mailing list
[email protected]
https://lists.zx2c4.com/mailman/listinfo/wireguard