cachix / devenv

Fast, Declarative, Reproducible, and Composable Developer Environments
https://devenv.sh
Apache License 2.0
3.56k stars 259 forks source link

Building a Go program using Bazel fails on macOS #1099

Closed kyleconroy closed 1 month ago

kyleconroy commented 1 month ago

Describe the bug

I can no longer build my Go project using Bazel after upgrading to devenv 1.0.

While I've pushed a repository with a full reproduction, I realize there are still many moving pieces. This could be an issue with Devenv, Nix, Bazel, or rules_go. I'm having a difficult time figuring out where the issue lies, so I thought I'd start here.

To reproduce

git clone https://github.com/kyleconroy/devenv-bazel-go-broken
cd devenv-bazel-go-broken
devenv shell
bazelisk run //:gazelle

Outside the devenv shell, this runs successfully

%  bazelisk run //:gazelle 
INFO: Analyzed target //:gazelle (3 packages loaded, 81 targets configured).
INFO: From GoLink external/gazelle~/cmd/gazelle/gazelle_/gazelle [for tool]:
ld: warning: ignoring duplicate libraries: '-lm'
INFO: Found 1 target...
Target //:gazelle up-to-date:
  bazel-bin/gazelle-runner.bash
  bazel-bin/gazelle
INFO: Elapsed time: 8.646s, Critical Path: 7.64s
INFO: 40 processes: 5 internal, 35 darwin-sandbox.
INFO: Build completed successfully, 40 total actions
INFO: Running command line: bazel-bin/gazelle

Once I've activated the shell, it fails.

$ bazelisk run //:gazelle
INFO: Analyzed target //:gazelle (3 packages loaded, 81 targets configured).
ERROR: /private/var/tmp/_bazel_kyle/c3d7894f4cdf3c1bcc4683dd549d8b70/external/gazelle~/cmd/gazelle/BUILD.bazel:4:15: GoLink external/gazelle~/cmd/gazelle/gazelle_/gazelle [for tool] failed: (Exit 1): builder failed: error executing GoLink command (from target @@gazelle~//cmd/gazelle:gazelle) bazel-out/darwin_arm64-opt-exec-ST-13d3ddad9198/bin/external/rules_go~~go_sdk~devenv-bazel-go-broken__download_0/builder_reset/builder link -sdk ... (remaining 71 arguments skipped)

Use --sandbox_debug to see verbose messages from the sandbox and retain the sandbox build root for debugging
external/rules_go~~go_sdk~devenv-bazel-go-broken__download_0/pkg/tool/darwin_arm64/link: running external/bazel_tools~cc_configure_extension~local_config_cc/cc_wrapper.sh failed: exit status 1
ld: framework not found CoreFoundation
clang: error: linker command failed with exit code 1 (use -v to see invocation)

link: error running subcommand GOROOT_FINAL=GOROOT \
GOROOT=bazel-out/darwin_arm64-opt-exec-ST-13d3ddad9198/bin/external/rules_go~/stdlib_ \
PATH=/usr/bin:external/bazel_tools~cc_configure_extension~local_config_cc:/bin \
GOPATH= \
GOEXPERIMENT=nocoverageredesign \
GOTOOLCHAIN=local \
TMPDIR=/tmp/nix-shell.2N0hcx \
CGO_ENABLED=1 \
GOOS=darwin \
ZERO_AR_DATE=1 \
GOARCH=arm64 \
__CF_USER_TEXT_ENCODING=0x1F5:0x0:0x0 \
external/rules_go~~go_sdk~devenv-bazel-go-broken__download_0/pkg/tool/darwin_arm64/link -importcfg bazel-out/darwin_arm64-opt-exec-ST-13d3ddad9198/bin/external/gazelle~/cmd/gazelle/gazelle_/importcfg2215023674 -o bazel-out/darwin_arm64-opt-exec-ST-13d3ddad9198/bin/external/gazelle~/cmd/gazelle/gazelle_/gazelle -extld external/bazel_tools~cc_configure_extension~local_config_cc/cc_wrapper.sh -buildid=redacted -s -w -extldflags "-mmacosx-version-min=14.2 -no-canonical-prefixes -fobjc-link-runtime -headerpad_max_install_names -Wl,-dead_strip -lm" /private/var/tmp/_bazel_kyle/c3d7894f4cdf3c1bcc4683dd549d8b70/sandbox/darwin-sandbox/118/execroot/_main/bazel-out/darwin_arm64-opt-exec-ST-13d3ddad9198/bin/external/gazelle~/cmd/gazelle/gazelle.a: exit status 2
Target //:gazelle failed to build
Use --verbose_failures to see the command lines of failed build steps.
INFO: Elapsed time: 9.088s, Critical Path: 7.71s
INFO: 40 processes: 6 internal, 34 darwin-sandbox.
ERROR: Build did NOT complete successfully
ERROR: Build failed. Not running target

Version

I'm running macOS 13.6.6 and devenv 1.0.2.

$ devenv version
devenv 1.0.2 (aarch64-darwin)
kyleconroy commented 1 month ago

I've tried directly adding CoreFoundation (via https://github.com/cachix/devenv/issues/508), but it didn't fix anything.

  # https://devenv.sh/packages/
  packages = [ 
    pkgs.bazelisk
    pkgs.git
  ]
  ++ # array concatenation operator
  lib.optionals pkgs.stdenv.isDarwin (with pkgs.darwin.apple_sdk; [
    frameworks.CoreFoundation
    frameworks.Security
    frameworks.SystemConfiguration
  ]);
domenkozar commented 1 month ago

I'm not familiar with Bazel, could we somehow reproduce the issue without bazel? That would narrow it down and allow me to help.

kyleconroy commented 1 month ago

It appears the issue is with Bazel itself. Downgrading to Bazel 6.5.0 fixes the issue. Upgrading back to Bazel 7.0.1 causes the issue to reappear.

kyleconroy commented 1 month ago

After reading through the Bazel release notes, I've found https://github.com/bazelbuild/rules_go/issues/3793 which seems like the issue. I'll reopen this if it turns out devenv needs to change something.