├── .gitignore ├── pkg └── something │ ├── something.go │ └── BUILD ├── .vscode ├── settings.json ├── tasks.json └── launch.json ├── cmd └── example │ ├── main.go │ └── BUILD ├── BUILD ├── Gopkg.toml ├── Gopkg.lock ├── WORKSPACE └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | bazel-* 2 | -------------------------------------------------------------------------------- /pkg/something/something.go: -------------------------------------------------------------------------------- 1 | package something 2 | 3 | func GetMessage() string { 4 | return "A walrus appears" 5 | } 6 | -------------------------------------------------------------------------------- /.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "go.gopath": "${workspaceFolder}/bazel-gopath", 3 | "go.gotoSymbol.resolveSymlinks": true 4 | } 5 | -------------------------------------------------------------------------------- /cmd/example/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "github.com/scele/go-dep-bazel-vscode-example/pkg/something" 5 | log "github.com/sirupsen/logrus" 6 | ) 7 | 8 | func main() { 9 | log.WithFields(log.Fields{ 10 | "animal": "walrus", 11 | }).Info(something.GetMessage()) 12 | } 13 | -------------------------------------------------------------------------------- /pkg/something/BUILD: -------------------------------------------------------------------------------- 1 | load("@io_bazel_rules_go//go:def.bzl", "go_library") 2 | 3 | go_library( 4 | name = "go_default_library", 5 | srcs = ["something.go"], 6 | importpath = "github.com/scele/go-dep-bazel-vscode-example/pkg/something", 7 | visibility = ["//visibility:public"], 8 | ) 9 | -------------------------------------------------------------------------------- /BUILD: -------------------------------------------------------------------------------- 1 | load("@io_bazel_rules_go//go:def.bzl", "gazelle") 2 | 3 | gazelle( 4 | name = "gazelle", 5 | command = "fix", 6 | args = [ 7 | "-build_file_name", 8 | "BUILD,BUILD.bazel", 9 | ], 10 | prefix = "github.com/scele/go-dep-bazel-vscode-example", 11 | ) 12 | -------------------------------------------------------------------------------- /.vscode/tasks.json: -------------------------------------------------------------------------------- 1 | { 2 | "version": "2.0.0", 3 | "tasks": [ 4 | { 5 | "label": "build", 6 | "type": "shell", 7 | "command": "bazel build -c dbg //cmd/example", 8 | "problemMatcher": [ 9 | "$go" 10 | ], 11 | "group": { 12 | "kind": "build", 13 | "isDefault": true 14 | } 15 | } 16 | ] 17 | } 18 | -------------------------------------------------------------------------------- /.vscode/launch.json: -------------------------------------------------------------------------------- 1 | { 2 | "version": "0.2.0", 3 | "configurations": [ 4 | { 5 | "name": "Launch //cmd/example (darwin_amd64_debug)", 6 | "type": "go", 7 | "request": "launch", 8 | "mode": "exec", 9 | "sourceDirectories": [ 10 | "${workspaceRoot}/", 11 | "${workspaceRoot}/bazel-go-dep-bazel-vscode-example/" 12 | ], 13 | "program": "${workspaceRoot}/bazel-bin/cmd/example/darwin_amd64_debug/example", 14 | "env": {}, 15 | "args": [], 16 | "preLaunchTask": "build", 17 | "showLog": true 18 | } 19 | ] 20 | } 21 | -------------------------------------------------------------------------------- /Gopkg.toml: -------------------------------------------------------------------------------- 1 | 2 | # Gopkg.toml example 3 | # 4 | # Refer to https://github.com/golang/dep/blob/master/docs/Gopkg.toml.md 5 | # for detailed Gopkg.toml documentation. 6 | # 7 | # required = ["github.com/user/thing/cmd/thing"] 8 | # ignored = ["github.com/user/project/pkgX", "bitbucket.org/user/project/pkgA/pkgY"] 9 | # 10 | # [[constraint]] 11 | # name = "github.com/user/project" 12 | # version = "1.0.0" 13 | # 14 | # [[constraint]] 15 | # name = "github.com/user/project2" 16 | # branch = "dev" 17 | # source = "github.com/myfork/project2" 18 | # 19 | # [[override]] 20 | # name = "github.com/x/y" 21 | # version = "2.4.0" 22 | 23 | 24 | [[constraint]] 25 | name = "github.com/sirupsen/logrus" 26 | version = "1.0.4" 27 | -------------------------------------------------------------------------------- /cmd/example/BUILD: -------------------------------------------------------------------------------- 1 | load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library") 2 | 3 | go_library( 4 | name = "go_default_library", 5 | srcs = ["main.go"], 6 | importpath = "github.com/scele/go-dep-bazel-vscode-example/cmd/example", 7 | visibility = ["//visibility:private"], 8 | deps = [ 9 | "//pkg/something:go_default_library", 10 | "@com_github_sirupsen_logrus//:go_default_library", 11 | ], 12 | ) 13 | 14 | go_binary( 15 | name = "example", 16 | embed = [":go_default_library"], 17 | importpath = "github.com/scele/go-dep-bazel-vscode-example/cmd/example", 18 | visibility = ["//visibility:public"], 19 | gc_linkopts = [ 20 | "-linkmode", 21 | "internal", 22 | ], 23 | ) 24 | -------------------------------------------------------------------------------- /Gopkg.lock: -------------------------------------------------------------------------------- 1 | # This file is autogenerated, do not edit; changes may be undone by the next 'dep ensure'. 2 | 3 | 4 | [[projects]] 5 | name = "github.com/sirupsen/logrus" 6 | packages = ["."] 7 | revision = "d682213848ed68c0a260ca37d6dd5ace8423f5ba" 8 | version = "v1.0.4" 9 | 10 | [[projects]] 11 | branch = "master" 12 | name = "golang.org/x/crypto" 13 | packages = ["ssh/terminal"] 14 | revision = "1875d0a70c90e57f11972aefd42276df65e895b9" 15 | 16 | [[projects]] 17 | branch = "master" 18 | name = "golang.org/x/sys" 19 | packages = ["unix","windows"] 20 | revision = "8f27ce8a604014414f8dfffc25cbcde83a3f2216" 21 | 22 | [solve-meta] 23 | analyzer-name = "dep" 24 | analyzer-version = 1 25 | inputs-digest = "97f3cd8b7a00b2b98fe58f834cf0d2f2c840a89809d15b451fdde156324ba6b1" 26 | solver-name = "gps-cdcl" 27 | solver-version = 1 28 | -------------------------------------------------------------------------------- /WORKSPACE: -------------------------------------------------------------------------------- 1 | workspace(name="com_github_scele_go_dep_bazel_vscode_example") 2 | 3 | http_archive( 4 | name = "io_bazel_rules_go", 5 | urls = ["https://github.com/scele/rules_go/archive/5c5051105c833287138b5e8c8361b82dc6e25721.tar.gz"], 6 | strip_prefix = "rules_go-5c5051105c833287138b5e8c8361b82dc6e25721", 7 | sha256 = "ce9a65847d38ace1ac1a43f3922f0671560ec63ea7011158822f3388a9f5de81", 8 | ) 9 | 10 | http_archive( 11 | name = "com_github_scele_rules_go_dep", 12 | urls = ["https://github.com/scele/rules_go_dep/archive/a814fad7f886bbe0fbd79df8549029708d570a37.tar.gz"], 13 | strip_prefix = "rules_go_dep-a814fad7f886bbe0fbd79df8549029708d570a37", 14 | sha256 = "3fe2c7cd2a629e8e55c5f6fef778ec753785a6da8f3435f45d7fc7b7ad78dfdb", 15 | ) 16 | 17 | load("@io_bazel_rules_go//go:def.bzl", "go_rules_dependencies", "go_register_toolchains") 18 | load("@com_github_scele_rules_go_dep//dep:dep.bzl", "dep_import") 19 | 20 | go_register_toolchains(go_version="1.9") 21 | go_rules_dependencies() 22 | 23 | dep_import( 24 | name = "godeps", 25 | prefix = "github.com/scele/go-dep-bazel-vscode-example", 26 | gopkg_lock = "//:Gopkg.lock", 27 | ) 28 | load("@godeps//:Gopkg.bzl", "go_deps") 29 | go_deps() 30 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |

2 | 3 |

4 | 5 | ## Example setup for go + dep + bazel + vscode 6 | 7 | This project shows how to use [rules_go_dep](https://github.com/scele/rules_go_dep) to set up a development enviromnent. 8 | 9 | ### Prerequisites 10 | 11 | It is assumed that dep, bazel and vscode are already installed. 12 | 13 | Currently the setup requires a custom build of the `vscode-go` extension, which can be installed with the following commands: 14 | 15 | ```bash 16 | wget https://github.com/scele/vscode-go/releases/download/0.6.72-lpeltonen.2/Go-0.6.72-lpeltonen.2.vsix 17 | code --install-extension ./Go-0.6.72-lpeltonen.2.vsix 18 | rm ./Go-0.6.72-lpeltonen.2.vsix 19 | ``` 20 | 21 | The project setup points `vscode-go` to a fake `GOPATH` constructed into the bazel build area. To prevent `vscode-go` from installing its dependency tools there, you should set `go.toolsGopath` in your vscode user settings to point somewhere else (e.g. to your normal `GOPATH`). 22 | 23 | #### MacOS 24 | 25 | - [Install delve](https://github.com/derekparker/delve/blob/master/Documentation/installation/osx/install.md) (you may need to apply [this workaround](https://github.com/go-delve/homebrew-delve/issues/19#issuecomment-330442033) if the installation fails) 26 | 27 | ### Clone this example project 28 | 29 | ```bash 30 | go get -u github.com/scele/go-dep-bazel-vscode-example 31 | cd $GOPATH/src/github.com/scele/go-dep-bazel-vscode-example 32 | ``` 33 | 34 | > **NOTE:** The build does not require the source to be under `$GOPATH`, but `dep ensure` does. 35 | 36 | ### Build and run with bazel 37 | 38 | ```bash 39 | bazel build //... 40 | bazel run //cmd/example 41 | ``` 42 | 43 | ### Use vscode to debug 44 | 45 | ```bash 46 | code -n . 47 | ``` 48 | 49 | In vscode, open `cmd/example/main.go`, set a breakpoint, and hit F5 to start debugging. 50 | Starting debugging will automatically invoke `bazel build`. 51 | 52 | To build the app without launching debugger, you can hit Ctrl+Shift+B (⇧⌘B on macOS). 53 | 54 | ### Update go dependencies 55 | 56 | ```bash 57 | dep ensure 58 | rm -rf ./vendor 59 | ``` 60 | 61 | The `vendor` directory should be manually deleted, since the dependencies will be pulled through bazel. 62 | 63 | ### Update BUILD files 64 | 65 | ```bash 66 | bazel run //:gazelle 67 | ``` 68 | --------------------------------------------------------------------------------