Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gopackagesdriver fails to build #358

Closed
koonpeng opened this issue Apr 17, 2024 · 0 comments · Fixed by #360
Closed

gopackagesdriver fails to build #358

koonpeng opened this issue Apr 17, 2024 · 0 comments · Fixed by #360

Comments

@koonpeng
Copy link
Contributor

koonpeng commented Apr 17, 2024

gopackagesdriver is used to allow the language server (gopls) to resolve go libraries managed by bazel. The setup instructions is documented in https://github.com/bazelbuild/rules_go/wiki/Editor-setup, however following it results in the standard library packages unable to be resolved.

Running the below command gives more information

koonpeng@koonpeng:~/ws/googlecloudrobotics/core$ echo {} | bazel run -- @io_bazel_rules_go//go/tools/gopackagesdriver 
std
...
ERROR: /usr/local/google/home/koonpeng/.cache/bazel/_bazel_koonpeng/ae7eebad1ef4062c3ff22e4722fa5b41/external/io_bazel_rules_go/BUILD.bazel:42:7: GoStdlibList external/io_bazel_rules_go/stdlib_/stdlib.pkg.json failed: (Exit 1): builder failed: error executing command (from target @io_bazel_rules_go//:stdlib) bazel-out/k8-opt-exec-2B5CBBC6/bin/external/go_sdk/builder_reset/builder stdliblist -sdk external/go_sdk -installsuffix linux_amd64 -sdk external/go_sdk -out ... (remaining 3 arguments skipped)

Use --sandbox_debug to see verbose messages from the sandbox and retain the sandbox build root for debugging
# runtime/cgo
_cgo_export.c:3:10: fatal error: 'stdlib.h' file not found
stdliblist: error running subcommand external/go_sdk/bin/go: exit status 1
{"NotHandled":false,"Compiler":"gc","Arch":"amd64","Packages":[]}

gopackagesdriver is trying to build something which fails with stdlib.h not found. Our custom toolchain might be causing the issue, according to bazel-contrib/rules_go#3886 (comment), the CGO flags should be converted to absolute paths but for some reason it is not happening.

koonpeng@koonpeng:~/ws/googlecloudrobotics/core$ bazel build --subcommands --aspects=@io_bazel_rules_go//go/tools/go
packagesdriver:aspect.bzl%go_pkg_info_aspect --output_groups=go_pkg_driver_json_file,go_pkg_driver_stdlib_json_file,
go_pkg_driver_srcs @io_bazel_rules_go//:stdlib
...
(cd /usr/local/google/home/koonpeng/.cache/bazel/_bazel_koonpeng/ae7eebad1ef4062c3ff22e4722fa5b41/execroot/cloud_robotics && \
  exec env - \
    CC=external/llvm_toolchain/bin/cc_wrapper.sh \
    CGO_CFLAGS='-U_FORTIFY_SOURCE --target=x86_64-unknown-linux-gnu -U_FORTIFY_SOURCE -fstack-protector -fno-omit-frame-pointer -Wthread-safety -Wself-assign --sysroot=external/com_googleapis_storage_chrome_linux_amd64_sysroot -no-canonical-prefixes -Wno-builtin-macro-redefined -D__DATE__="redacted" -D__TIMESTAMP__="redacted" -D__TIME__="redacted" -fdebug-prefix-map=external/llvm_toolchain_llvm/=__bazel_toolchain_llvm_repo__/' \
    CGO_ENABLED=1 \
    CGO_LDFLAGS='--target=x86_64-unknown-linux-gnu -lm -no-canonical-prefixes -fuse-ld=lld -Wl,--build-id=md5 -Wl,--hash-style=gnu -Wl,-z,relro,-z,now -l:libc++.a -l:libc++abi.a -l:libunwind.a -rtlib=compiler-rt -lpthread -ldl --sysroot=external/com_googleapis_storage_chrome_linux_amd64_sysroot' \
    GOARCH=amd64 \
    GOEXPERIMENT=nocoverageredesign \
    GOOS=linux \
    GOPATH='' \
    GOROOT=external/go_sdk \
    GOROOT_FINAL=GOROOT \
    GOTOOLCHAIN=local \
    PATH=external/llvm_toolchain/bin:/bin:/usr/bin \
  bazel-out/k8-opt-exec-2B5CBBC6/bin/external/go_sdk/builder_reset/builder stdliblist -sdk external/go_sdk -installsuffix linux_amd64 -sdk external/go_sdk -out bazel-out/k8-fastbuild/bin/external/io_bazel_rules_go/stdlib_/stdlib.pkg.json -cache bazel-out/k8-fastbuild/bin/external/io_bazel_rules_go/stdlib_/gocache)

(notice that the sysroot is using relative path)

Disabling --incompatible_enable_cc_toolchain_resolution and --action_env=BAZEL_DO_NOT_DETECT_CPP_TOOLCHAIN=1 fixes the issue as it removes the --sysroot flag, so for now we can use this to workaround.

GOPACKAGESDRIVER_BAZEL_BUILD_FLAGS="--noincompatible_enable_cc_toolchain_resolution --action_env=BAZEL_DO_NOT_DETECT_CPP_TOOLCHAIN=0" exec bazel run -- @io_bazel_rules_go//go/tools/gopackagesdriver "${@}"
ensonic pushed a commit that referenced this issue Apr 19, 2024
This fixes #358. With this, it should now be able to follow the docs at
https://github.com/bazelbuild/rules_go/wiki/Editor-setup and have gopls
working.

Signed-off-by: Teo Koon Peng <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant