tweag / rules_haskell

Haskell rules for Bazel.
https://haskell.build
Apache License 2.0
266 stars 80 forks source link

CI - coverage does not work on Darwin #1768

Closed avdv closed 2 years ago

avdv commented 2 years ago

Describe the bug

After upgrading rules_nixpkgs, the coverage test fails:

https://github.com/tweag/rules_haskell/blob/62a35834bff96e7e9ec0fe8118f02681fc7036be/.github/workflows/workflow.yaml#L58

See https://github.com/tweag/rules_haskell/runs/7231655925?check_suite_focus=true

To Reproduce

on Darwin, run

nix-shell --arg docTools false --pure --run '
            set -euo pipefail
            ./tests/run-start-script.sh --use-nix
            bazel build //tests:run-tests
            ./bazel-ci-bin/tests/run-tests
            bazel coverage //...
            bazel build //docs:api_html
            bazel build //docs:guide_html
            '

Expected behavior

This should simply work.

Environment

Additional context

This time it passed the tests with flying colors, but failed the bazel coverage //... invocation. See https://app.buildbuddy.io/invocation/e9802e2f-a258-4534-bd76-6bb27193a37a#log:

ERROR: /Users/runner/work/rules_haskell/rules_haskell/tests/two-same-file/BUILD.bazel:6:13: HaskellLinkBinary tests/two-same-file/one failed: (Exit 1): ghc_wrapper failed: error executing command
  (cd /private/var/tmp/_bazel_runner/d1e3bdaa0c37ce8d00ecff62b05849c3/sandbox/processwrapper-sandbox/4287/execroot/rules_haskell && \
  exec env - \
    CC_WRAPPER_CC_PATH=external/nixpkgs_config_cc/cc_wrapper.sh \
    CC_WRAPPER_CPU=darwin \
    CC_WRAPPER_PLATFORM=darwin \
    LANG=C.UTF-8 \
    RULES_HASKELL_DOCDIR_PATH=/nix/store/li6yc9c5ya7id1pwr71a1cgj7bah5lf7-ghc-8.10.7-doc/share/doc/ghc/html/libraries/base-4.14.3.0 \
    RULES_HASKELL_GHC_PATH=external/rules_haskell_ghc_nixpkgs/bin/ghc \
    RULES_HASKELL_GHC_PKG_PATH=external/rules_haskell_ghc_nixpkgs/bin/ghc-pkg \
    RULES_HASKELL_LIBDIR_PATH=/nix/store/4r3mqcs0qxpzv4pmzpwcqyafcs49y2mz-ghc-8.10.7/lib/ghc-8.10.7 \
  bazel-out/host/bin/haskell/ghc_wrapper bazel-out/darwin-fastbuild/bin/tests/two-same-file/compile_flags_one__HaskellLinkBinary bazel-out/darwin-fastbuild/bin/tests/two-same-file/extra_args_one__HaskellLinkBinary)
Execution platform: @rules_nixpkgs_core//platforms:host
Use --sandbox_debug to see verbose messages from the sandbox
ld: file not found: /nix/store/zfh3npfhfjjgwi0dqpriklip5k15ppmj-clang-7.1.0/lib/clang/7.1.0/lib/darwin/libclang_rt.profile_osx.a

_Originally posted by @avdv in https://github.com/tweag/rules_haskell/issues/1750#issuecomment-1152184953_

Here are some details:

It seems the compiler (clang on Darwin) is invoked with:

-dynamiclib -o bazel-out/darwin-fastbuild/bin/tests/library-with-sysdeps/libHStestsZSlibrary-with-sysdepsZSlibrary-with-sysdeps-ghc8.10.7.dylib \
-undefined dynamic_lookup -single_module -Wl,-install_name,@rpath/libHStestsZSlibrary-with-sysdepsZSlibrary-with-sysdeps-ghc8.10.7.dylib bazel-out/darwin-fastbuild/bin/tests/library-with-sysdeps/_obj/library-with-sysdeps/Lib.dyn_o \
 -Lbazel-out/darwin-fastbuild/bin/_solib_darwin/_U@zlib.dev_S_S_Czlib___Ulib -L/nix/store/4r3mqcs0qxpzv4pmzpwcqyafcs49y2mz-ghc-8.10.7/lib/ghc-8.10.7/base-4.14.3.0 -L/nix/store/cswxlzvkanvri87lss28v8y4k5p6agby-libiconv-50/lib \
-L/nix/store/4r3mqcs0qxpzv4pmzpwcqyafcs49y2mz-ghc-8.10.7/lib/ghc-8.10.7/integer-gmp-1.0.3.0 \
-L/nix/store/pfnc51sa45vnb9xbap75srkwdlk1ka0h-gmp-6.2.1/lib \
-L/nix/store/4r3mqcs0qxpzv4pmzpwcqyafcs49y2mz-ghc-8.10.7/lib/ghc-8.10.7/ghc-prim-0.6.1 \
-L/nix/store/4r3mqcs0qxpzv4pmzpwcqyafcs49y2mz-ghc-8.10.7/lib/ghc-8.10.7/rts \
-L/nix/store/ybnm7q5k91990plbq6h36603rv8pa6xm-libffi-3.4.2/lib \
-lHSbase-4.14.3.0-ghc8.10.7 -lHSinteger-gmp-1.0.3.0-ghc8.10.7 -lHSghc-prim-0.6.1-ghc8.10.7 \
-lz.1.2.11 -lz.1 -lz -liconv -lgmp -Wl,-dead_strip_dylibs -Wl,-headerpad,8000 \
-fprofile-instr-generate \
-undefined dynamic_lookup -headerpad_max_install_names -lc++ -lm -u _adler32 -u _adler32 -u _adler32 -headerpad_max_install_names -Wl,-rpath,@loader_path/../../_solib_darwin

(note the -fprofile-instr-generate flag)

and clang then invokes the linker thusly:

clang version 7.1.0 (tags/RELEASE_710/final)
Target: x86_64-apple-darwin
Thread model: posix
InstalledDir: /nix/store/zfh3npfhfjjgwi0dqpriklip5k15ppmj-clang-7.1.0/bin
 "/nix/store/sb2p0xy2c1v5h7ijlrp7zbdk7hhr1sfk-clang-wrapper-7.1.0/bin/ld" /nix/store/zfh3npfhfjjgwi0dqpriklip5k15ppmj-clang-7.1.0/lib/clang/7.1.0/lib/darwin/libclang_rt.profile_osx.a -demangle -lto_library /nix/store/zfh3npfhfjjgwi0dqpriklip5k15ppmj-clang-7.1.0/lib/libLTO.dylib -dynamic -dylib -arch x86_64 -headerpad_max_install_names -headerpad_max_install_names -macosx_version_min 10.12.0 -single_module -undefined dynamic_lookup -undefined dynamic_lookup -undefined dynamic_lookup -undefined dynamic_lookup -u _adler32 -u _adler32 -u _adler32 -o bazel-out/darwin-fastbuild/bin/tests/library-with-sysdeps/libHStestsZSlibrary-with-sysdepsZSlibrary-with-sysdeps-ghc8.10.7.dylib -Lbazel-out/darwin-fastbuild/bin/_solib_darwin/_U@zlib.dev_S_S_Czlib___Ulib -L/nix/store/4r3mqcs0qxpzv4pmzpwcqyafcs49y2mz-ghc-8.10.7/lib/ghc-8.10.7/base-4.14.3.0 -L/nix/store/cswxlzvkanvri87lss28v8y4k5p6agby-libiconv-50/lib -L/nix/store/4r3mqcs0qxpzv4pmzpwcqyafcs49y2mz-ghc-8.10.7/lib/ghc-8.10.7/integer-gmp-1.0.3.0 -L/nix/store/pfnc51sa45vnb9xbap75srkwdlk1ka0h-gmp-6.2.1/lib -L/nix/store/4r3mqcs0qxpzv4pmzpwcqyafcs49y2mz-ghc-8.10.7/lib/ghc-8.10.7/ghc-prim-0.6.1 -L/nix/store/4r3mqcs0qxpzv4pmzpwcqyafcs49y2mz-ghc-8.10.7/lib/ghc-8.10.7/rts -L/nix/store/ybnm7q5k91990plbq6h36603rv8pa6xm-libffi-3.4.2/lib -L/nix/store/5ygwml3yjcdvv5y35c2p9z4qavknfs0i-libcxx-7.1.0/lib -L/nix/store/0yfv821j8pkh8ya553cdhpik8n12ky7a-libcxxabi-7.1.0/lib -L/nix/store/cswxlzvkanvri87lss28v8y4k5p6agby-libiconv-50/lib -L/nix/store/a1sp2lg1lc67kwgcq5iw6ar2wdjfqqcc-objc4-709.1/lib -L/nix/store/1vp7fr7dm20khrz9zhzbwq8h9k37phhc-Libsystem-1238.60.2/lib -L/nix/store/66crk4qirnyz08zw91nhlz4x8lrxprc0-clang-7.1.0-lib/lib -no_uuid -install_name @rpath/libHStestsZSlibrary-with-sysdepsZSlibrary-with-sysdeps-ghc8.10.7.dylib bazel-out/darwin-fastbuild/bin/tests/library-with-sysdeps/_obj/library-with-sysdeps/Lib.dyn_o -lHSbase-4.14.3.0-ghc8.10.7 -lHSinteger-gmp-1.0.3.0-ghc8.10.7 -lHSghc-prim-0.6.1-ghc8.10.7 -lz.1.2.11 -lz.1 -lz -liconv -lgmp -dead_strip_dylibs -headerpad 8000 -lc++ -lm -rpath @loader_path/../../_solib_darwin -arch x86_64 -lSystem -F/nix/store/nvn3ldqg86k6x05pbq6yblg39p469rfd-apple-framework-CoreFoundation/Library/Frameworks -F/nix/store/8qc9kz9h8paijgxnhap7gslvvsyvry0f-apple-framework-CoreServices/Library/Frameworks -F/nix/store/f64r0ps4phxl3cnar09gi9y4pp1q5n7h-apple-framework-Security/Library/Frameworks -F/nix/store/pk9s46a3pi32h6x99k6ipyj8qy5gvrzj-apple-framework-Foundation/Library/Frameworks

So clang is adding in the libclang_rt.profile_osx.a static library since it expects it to be located inside of the "InstallationDir". Whereas the library is actually part of the compiler-rt-libc package.

avdv commented 2 years ago

The cause for this is again that wrapCCWith et. al. are not composable. All the flags added to the nix-support/* files of the original wrapper are gone, and we have to replicate all of them into the new wrapper (or source them ?).

In this case, the llvm tools are adding in the -resource-dir flag so that clang can find its resources, such as the profiling RT libraries: https://github.com/NixOS/nixpkgs/blob/bad39490e9f108654904872d00ccb1a22d75d450/pkgs/development/compilers/llvm/11/default.nix#L45