rules_go icon indicating copy to clipboard operation
rules_go copied to clipboard

Proper way to import go_proto_library gRPC generated service Go file in VSCode?

Open vymao opened this issue 3 years ago • 2 comments

What version of rules_go are you using?

v0.32.0

What version of gazelle are you using?

v0.25.0

What version of Bazel are you using?

Build label: 5.2.0

Does this issue reproduce with the latest releases of all the above?

Yes

What did you do?

Successfully used rules_go to build a gRPC service:

go_proto_library(
    name = "processor_go_proto",
    compilers = ["@io_bazel_rules_go//proto:go_grpc"],
    importpath = "github.com/vymao/Anthem/back-end/main/services/shared/proto/processor",
    proto = ":processor_proto",
    deps = ["//services/shared/proto/common:common_go_proto"],
)

However, I'm not sure how to import the resulting file. The generated file is nested under bazel_bin and under the original proto file path. Just importing this generated file based on the path specified in the option go_package field in the proto doesn't seem to work. For example, if I specify option go_package = github.com/test/path/to/proto, and if I then try to do import github.com/test/path/to/proto in my Go file after building the proto, this does not build.

What did you expect to see?

A successful import.

What did you see instead?

ERROR: /Users/victor/Desktop/Anthem/back-end/main/server/app/router/BUILD.bazel:3:11: GoCompilePkg server/app/router/router.a failed: (Exit 1): builder failed: error executing command bazel-out/darwin-opt-exec-2B5CBBC6/bin/external/go_sdk/builder compilepkg -sdk external/go_sdk -installsuffix darwin_amd64 -src server/app/router/handlerFunctions.go -src ... (remaining 43 arguments skipped)

Use --sandbox_debug to see verbose messages from the sandbox and retain the sandbox build root for debugging
server/app/router/handlerFunctions.go:19:16: undefined: processor.Processor
compilepkg: error running subcommand external/go_sdk/pkg/tool/darwin_amd64/compile: exit status 2
Target //server/app/router:router failed to build
Use --verbose_failures to see the command lines of failed build steps.
INFO: Elapsed time: 1.202s, Critical Path: 0.46s
INFO: 3 processes: 2 internal, 1 darwin-sandbox.
FAILED: Build did NOT complete successfully

Is there a way around this? Is there a better IDE to use?

vymao avatar Jul 05 '22 20:07 vymao

Have you set up VSCode to use the LSP plugin?

https://github.com/bazelbuild/rules_go/wiki/Editor-setup#visual-studio-code

achew22 avatar Jul 06 '22 02:07 achew22

Thanks for this. I think I may be running into a similar issue as this. Right now, my settings.json file looks like this:

{
    "files.associations": {
        "iosfwd": "cpp",
        "stdexcept": "cpp",
        "__bit_reference": "cpp",
        "__bits": "cpp",
        "__config": "cpp",
        "__debug": "cpp",
        "__errc": "cpp",
        "__functional_base": "cpp",
        "__hash_table": "cpp",
        "__locale": "cpp",
        "__mutex_base": "cpp",
        "__node_handle": "cpp",
        "__nullptr": "cpp",
        "__split_buffer": "cpp",
        "__string": "cpp",
        "__threading_support": "cpp",
        "__tree": "cpp",
        "__tuple": "cpp",
        "algorithm": "cpp",
        "any": "cpp",
        "array": "cpp",
        "atomic": "cpp",
        "bit": "cpp",
        "bitset": "cpp",
        "cctype": "cpp",
        "cfenv": "cpp",
        "chrono": "cpp",
        "cinttypes": "cpp",
        "clocale": "cpp",
        "cmath": "cpp",
        "codecvt": "cpp",
        "complex": "cpp",
        "condition_variable": "cpp",
        "csignal": "cpp",
        "cstdarg": "cpp",
        "cstddef": "cpp",
        "cstdint": "cpp",
        "cstdio": "cpp",
        "cstdlib": "cpp",
        "cstring": "cpp",
        "ctime": "cpp",
        "cwchar": "cpp",
        "cwctype": "cpp",
        "deque": "cpp",
        "exception": "cpp",
        "forward_list": "cpp",
        "fstream": "cpp",
        "functional": "cpp",
        "future": "cpp",
        "initializer_list": "cpp",
        "iomanip": "cpp",
        "ios": "cpp",
        "iostream": "cpp",
        "istream": "cpp",
        "iterator": "cpp",
        "limits": "cpp",
        "list": "cpp",
        "locale": "cpp",
        "map": "cpp",
        "memory": "cpp",
        "mutex": "cpp",
        "new": "cpp",
        "numeric": "cpp",
        "optional": "cpp",
        "ostream": "cpp",
        "queue": "cpp",
        "random": "cpp",
        "ratio": "cpp",
        "regex": "cpp",
        "scoped_allocator": "cpp",
        "set": "cpp",
        "sstream": "cpp",
        "stack": "cpp",
        "streambuf": "cpp",
        "string": "cpp",
        "string_view": "cpp",
        "system_error": "cpp",
        "thread": "cpp",
        "tuple": "cpp",
        "type_traits": "cpp",
        "typeindex": "cpp",
        "typeinfo": "cpp",
        "unordered_map": "cpp",
        "unordered_set": "cpp",
        "utility": "cpp",
        "valarray": "cpp",
        "variant": "cpp",
        "vector": "cpp",
        "*.inc": "cpp"
    },
    "go.goroot": "${workspaceFolder}/bazel-main/external/go_sdk",
    "go.toolsEnvVars": {
      "GOPACKAGESDRIVER": "${workspaceFolder}/tools/gopackagesdriver.sh"
    },
    "go.enableCodeLens": {
      "references": false,
      "runtest": false
    },
    "gopls": {
      "build.directoryFilters": [
        "-bazel-bin",
        "-bazel-out",
        "-bazel-testlogs",
        "-bazel-main",
      ],
      "formatting.gofumpt": true,
      "formatting.local": "github.com/vymao/Anthem/back-end/main",
      "ui.completion.usePlaceholders": true,
      "ui.semanticTokens": true,
      "ui.codelenses": {
        "gc_details": false,
        "regenerate_cgo": false,
        "generate": false,
        "test": false,
        "tidy": false,
        "upgrade_dependency": false,
        "vendor": false
      },
    },
    "go.useLanguageServer": true,
    "go.buildOnSave": "off",
    "go.lintOnSave": "off",
    "go.vetOnSave": "off",
}

Yet I get a notification like

Error loading workspace: no packages returned: packages.Load error

even after adding GOPACKAGESDRIVER_BAZEL_BUILD_FLAGS=--strategy=GoStdlibList=local to the tools/gopackagesdriver.sh file.

vymao avatar Jul 07 '22 03:07 vymao

Have the same issue. I'm using emacs, this is my dir-locals.el:

((go-mode . ((lsp-go-directory-filters . ["-bazel-bin" "-bazel-foo" "-bazel-out" "-bazel-testlogs" "-external"])
             (lsp-go-codelenses . nil)
             (lsp-go-env . ((GOPACKAGESDRIVER . "./tools/gopackagesdriver.sh"))))))

lromor avatar Mar 13 '23 16:03 lromor