bazelbuild / intellij

IntelliJ plugin for Bazel projects
https://ij.bazel.build/
Apache License 2.0
761 stars 303 forks source link

CLion C++ '-oso_prefix,.' "path trimming" flag injected by the debug launcher fails linking (Linux/clang-17/mold) #6410

Open jayv opened 5 months ago

jayv commented 5 months ago

Whle trying to learn Bazel with a simple project setup with Bazel/Clion I ran into this issue.

update: oso_prefix seems a non-standard Apple/Darwin-only linker flag https://github.com/bazelbuild/intellij/issues/6410#issuecomment-2080239502 introduced to fix debugging on MacOS

A normal build/run of the main bazel target works fine, but the debug build fails to link because of the "path trimming" flags that get injected by default by the plugin. When I run the bazel command without the -oso_prefix,. part it links just fine, so when I enabled bazel.trim.absolute.path.disabled in the registry, my project builds/runs/hits breakpoints per usual 🤷🏻‍♂️, ~so I'm not sure why this is done by default.~

This is on Ubuntu Linux with clang-17 and mold (as linker) configured as a custom blaze toolchain to leverage C++23 and libc++. That said ld, and gold also struggle with this linker flag, suggesting -oso_prefix,. is not correct or universally supported. https://github.com/bazelbuild/intellij/issues/6410#issuecomment-2080239502 ~Or is there perhaps something wrong with my bazel project setup?~

https://github.com/bazelbuild/intellij/blob/86e31292434875ca1f86a02230cde5a3de5a70cf/clwb/src/com/google/idea/blaze/clwb/run/BlazeCidrRunConfigurationRunner.java#L120-L124

bazel command:

bazel build --tool_tag=ijwb:CLion --compilation_mode=dbg --copt=-O0 --copt=-g --strip=never --dynamic_mode=off --fission=yes --copt=-fdebug-compilation-dir=/home/jayv/devperso/serc --linkopt=-Wl,-oso_prefix,. --curses=no --color=yes --progress_in_terminal_title=no --build_event_binary_file=/tmp/intellij-bep-7aab6642-3b55-406f-bf24-7492807af451 --nobuild_event_binary_file_path_conversion -- //:serc

clang command:

ERROR: /home/jayv/devperso/serc/BUILD:28:10: Linking serc failed: (Exit 1): clang++-17 failed: error executing command (from target //:serc) /usr/bin/clang++-17 -o bazel-out/k8-dbg/bin/serc bazel-out/k8-dbg/bin/_objs/serc/main.o bazel-out/k8-dbg/bin/external/fmt/libfmt.a -Wl,-oso_prefix,. -lc++ -lm '-fuse-ld=ld'

Use --sandbox_debug to see verbose messages from the sandbox and retain the sandbox build root for debugging
/usr/bin/ld: cannot find .: file format not recognized
clang++-17: error: linker command failed with exit code 1 (use -v to see invocation)
Target //:serc failed to build
jayv commented 5 months ago

As I dig more into this, it seems like a non-standard Apple only linker flag?

image

I can't find any evidence of other linkers supporting this... impemented only for lld with mach-o target.

https://bugs.llvm.org/show_bug.cgi?id=50229

If that's the case the condition should not just check isClangBuild() but rather check if the Darwin ld64 linker is being invoked or lld with mach-o target.

jayv commented 5 months ago

This is the relevant change "fix debugging on Mac OS" that introduced the new flag, and probably broke non-Apple users:

https://github.com/bazelbuild/intellij/pull/4003

/cc @tpasternak

tpasternak commented 5 months ago

I think it's rather triggered by the fact you are using mold instead of lld. We are planning to reorganize some things in this area and we will take this issue into account.

jayv commented 5 months ago

Hmm, did you try? I tried mold, gold, and gnu ld, nothing worked, and then I found the PR/commit on llvm https://github.com/llvm/llvm-project/commit/236197e2d026193111580bd145f00367ba0c1afb AFAICT this flag was only added for “mach-o” (mac) binaries, meaning lld for linux (elf) binaries does not support this flag.

tpasternak commented 5 months ago

Thanks for clarification then