If you have a rule for creating the source search path, you can add it as advice to the function that compile uses for finding paths. For example, the default directory for building with bazel is ~/.cache/bazel. So I want to find all those and strip out that part of the file. I added an "advice" to the compilation-find-file
function so that it will rename the file before calling the function. That elisp is:
;; Rewrite the names of bazel error filenames.
(defun rename-bazel (orig-fun marker filename &rest args)
(let ((new-filename (or (and (string-match "^\\(.*\\)/.cache/bazel/.*/__main__/\\(.*\\)" filename)
(concat (match-string 1 filename) "/" (match-string 2 filename)))
filename)))
(apply orig-fun marker new-filename args)))
(advice-add 'compilation-find-file :around #'rename-bazel)
Now when I click on errors in the compilation buffer, it goes to the right place.