From 88b64deab764edc4316de13c184157135cd50eff Mon Sep 17 00:00:00 2001 From: Dmitry Ivankov Date: Fri, 31 Oct 2025 19:51:12 +0100 Subject: [PATCH] bazel_7: fix bazelDeps build on darwin under sandbox=true Fixes ``` Server crashed during startup. Now printing ..../server/jvm.out OpenJDK 64-Bit Server VM warning: Options -Xverify:none and -noverify were deprecated in JDK 13 and will likely be removed in a future release. FATAL: CHECK failed: (suspend_state.connect_port) != (0): ``` by overriding sleep&suspend prevention module in darwin bootstrap bazel binary Initially had this code implemented for `bazel_8`, but in the end recent `bazel_8` versions don't need binary bootstrap bazel and don't hit this issue anymore (https://github.com/NixOS/nixpkgs/pull/362414) Now hit this again trying to update `bazel_7`, so making PR for `bazel_7`, separately from version bump as it is the same thing for 7.6.0 and 7.7.0 --- .../ba/bazel_7/darwin_hacks/BlazeModule.java | 7 ++ .../darwin_hacks/SleepPreventionModule.java | 9 +++ .../darwin_hacks/SystemSuspensionModule.java | 15 ++++ .../ba/bazel_7/darwin_hacks/default.nix | 75 +++++++++++++++++++ pkgs/by-name/ba/bazel_7/package.nix | 21 +++++- 5 files changed, 125 insertions(+), 2 deletions(-) create mode 100644 pkgs/by-name/ba/bazel_7/darwin_hacks/BlazeModule.java create mode 100644 pkgs/by-name/ba/bazel_7/darwin_hacks/SleepPreventionModule.java create mode 100644 pkgs/by-name/ba/bazel_7/darwin_hacks/SystemSuspensionModule.java create mode 100644 pkgs/by-name/ba/bazel_7/darwin_hacks/default.nix diff --git a/pkgs/by-name/ba/bazel_7/darwin_hacks/BlazeModule.java b/pkgs/by-name/ba/bazel_7/darwin_hacks/BlazeModule.java new file mode 100644 index 0000000000000..5ace4066c3282 --- /dev/null +++ b/pkgs/by-name/ba/bazel_7/darwin_hacks/BlazeModule.java @@ -0,0 +1,7 @@ +package com.google.devtools.build.lib.runtime; + +// We only need to capture enough details from Bazel source code +// to make our modules compile and be binary-compatible. +// This BlazeModule won't be injected into Bazel classpath. +public abstract class BlazeModule { +} diff --git a/pkgs/by-name/ba/bazel_7/darwin_hacks/SleepPreventionModule.java b/pkgs/by-name/ba/bazel_7/darwin_hacks/SleepPreventionModule.java new file mode 100644 index 0000000000000..53b207b706918 --- /dev/null +++ b/pkgs/by-name/ba/bazel_7/darwin_hacks/SleepPreventionModule.java @@ -0,0 +1,9 @@ +package com.google.devtools.build.lib.platform; + +import com.google.devtools.build.lib.runtime.BlazeModule; + +// This class can fail in Nix sandbox on Darwin so we replace +// it with a stub version +public final class SleepPreventionModule extends BlazeModule { + // do nothing +} diff --git a/pkgs/by-name/ba/bazel_7/darwin_hacks/SystemSuspensionModule.java b/pkgs/by-name/ba/bazel_7/darwin_hacks/SystemSuspensionModule.java new file mode 100644 index 0000000000000..c541f6a4c159c --- /dev/null +++ b/pkgs/by-name/ba/bazel_7/darwin_hacks/SystemSuspensionModule.java @@ -0,0 +1,15 @@ +package com.google.devtools.build.lib.platform; + +import com.google.devtools.build.lib.runtime.BlazeModule; + +// This class can fail in Nix sandbox on Darwin so we replace +// it with a stub version +public final class SystemSuspensionModule extends BlazeModule { + // do nothing + + // this method is part of module interface + synchronized void suspendCallback(int reason) { + // do nothing + } +} + diff --git a/pkgs/by-name/ba/bazel_7/darwin_hacks/default.nix b/pkgs/by-name/ba/bazel_7/darwin_hacks/default.nix new file mode 100644 index 0000000000000..8f94f2e8f0312 --- /dev/null +++ b/pkgs/by-name/ba/bazel_7/darwin_hacks/default.nix @@ -0,0 +1,75 @@ +{ + stdenv, + buildJdk, + runJdk, + version, + writeScript, +}: +let + fakeBazelModules = stdenv.mkDerivation { + pname = "fakeBazelModules"; + inherit version; + dontUnpack = true; + buildPhase = " + # Java file name needs to match class name so we need to copy + cp ${./BlazeModule.java} ./BlazeModule.java + cp ${./SystemSuspensionModule.java} ./SystemSuspensionModule.java + cp ${./SleepPreventionModule.java} ./SleepPreventionModule.java + + # compile all sources + ${buildJdk}/bin/javac -d . BlazeModule.java SystemSuspensionModule.java SleepPreventionModule.java + + # don't package BlazeModule.class as we want to use real base class, but a stub compatible version was needed to make + # fake modules compile + ${buildJdk}/bin/jar cf output.jar ./com/google/devtools/build/lib/platform/{SystemSuspension,SleepPrevention}Module.class + "; + installPhase = '' + runHook preInstall + + mkdir -p $out/ + install -Dm755 output.jar $out/output.jar + + runHook postInstall + ''; + + }; + # bin/java wrapper that injects fake modules into classpath + jvmInterceptSh = writeScript "java" '' + #!/usr/bin/env bash + + # replaces + # .. -jar foo.jar .. + # with + # .. -cp ..overrides..:foo.jar MainClass .. + # to inject fake modules classes into Bazel + + for (( i=2; i <= "$#"; i++ )); do + if [[ "''${!i}" == "-jar" ]]; then + prev=$(( i - 1 )) + jar=$(( i + 1 )) + MAIN_CLASS=$(unzip -qc "''${!jar}" META-INF/MANIFEST.MF | grep "^Main-Class: " | cut -d " " -f 2- | tr -d "\r") + next=$(( jar + 1 )) + exec "${runJdk}/bin/java" "''${@:1:prev}" -cp "${fakeBazelModules}/output.jar:''${!jar}" "$MAIN_CLASS" "''${@:next}" + fi + done + + echo "Failed to find -jar argument in: $@" >&2 + exit 1 + ''; +in +{ + jvmIntercept = stdenv.mkDerivation { + pname = "jvmIntercept"; + inherit version; + dontUnpack = true; + installPhase = '' + runHook preInstall + + mkdir -p $out/bin + install -Dm755 ${jvmInterceptSh} $out/bin/java + + runHook postInstall + ''; + }; + +} diff --git a/pkgs/by-name/ba/bazel_7/package.nix b/pkgs/by-name/ba/bazel_7/package.nix index 7952a07757c88..162c0e5014b99 100644 --- a/pkgs/by-name/ba/bazel_7/package.nix +++ b/pkgs/by-name/ba/bazel_7/package.nix @@ -5,6 +5,7 @@ fetchurl, makeWrapper, writeTextFile, + writeScript, replaceVars, writeShellApplication, makeBinaryWrapper, @@ -168,6 +169,22 @@ let bazelDeps = let bazelForDeps = if stdenv.hostPlatform.isDarwin then bazelBootstrap else bazelFhs; + serverJavabase = + if stdenv.hostPlatform.isDarwin then + let + hacks = (import ./darwin_hacks/default.nix) { + inherit + stdenv + buildJdk + runJdk + version + writeScript + ; + }; + in + hacks.jvmIntercept + else + runJdk; in stdenv.mkDerivation { name = "bazelDeps"; @@ -203,8 +220,8 @@ let buildPhase = '' runHook preBuild export HOME=$(mktemp -d) - (cd bazel_src; ${bazelForDeps}/bin/bazel --server_javabase=${runJdk} mod deps --curses=no; - ${bazelForDeps}/bin/bazel --server_javabase=${runJdk} vendor src:bazel_nojdk \ + (cd bazel_src; ${bazelForDeps}/bin/bazel --server_javabase=${serverJavabase} mod deps --curses=no; + ${bazelForDeps}/bin/bazel --server_javabase=${serverJavabase} vendor src:bazel_nojdk \ --curses=no \ --vendor_dir ../vendor_dir \ --verbose_failures \