Getting Emacs' compile-mode to play nice with Gradle
A workaround for a minor annoyance when working with Gradle via compile-modePublished on Sat April 26, 2025 with tags: java, emacs, hacks, milliblog.
So, I am still not free of Java, but I started writing it in GNU Emacs
recently. It works surprisingly well with Eglot, JDTLS, java-ts-mode
(sadly, CC Mode Java support is too far behind and I haven’t been able to
allocate time to updating it), and a few hacks I had to make to deal with some
oddities of JDTLS.
I’ve been using compilation-mode
through project-compile
to
build my Java code and look for errors, but when building something with Gradle
8.12.1 (and possibly other versions), Gradle outputs diagnostics twice: once
when they actually occur, and again later as a recap, but this time with two
spaces prefixed on each line of the diagnostic.
GNU Emacs supports parsing these diagnostics out of the box (which came as a surprise to me), but these double diagnostics keep confusing it, by telling it that an error happened in some file whose path starts with a double space, and by making it seem like there’s twice the number of errors there are in reality.
This can be hacked around using compilation-transform-file-match-alist
like so:
'compilation-transform-file-match-alist '("\\` " nil)) (add-to-list
The above tells compile-mode
to ignore filenames starting with a double
space (which is the prefix Gradle is using in the recap).
Sadly, I don’t see a way to make this apply only to Gradle. Perhaps updating
compilation-error-regexp-alist-alist
entries for Gradle not to match
filenames starting with is wiser, but I haven’t tried that yet (for a lack of
time).