net.revelc.code.formatter equivalent for bazel?

48 views
Skip to first unread message

Robert Cauble

unread,
Sep 18, 2022, 9:51:37 AM9/18/22
to bazel-discuss
Hi,

Am trying to migrate from maven to bazel and I am looking for the equivalent of net.revelc.code.formatter. It's currently setup to automatically execute the "validate" goal in the validate phase and the "format" goal, on-demand in the format phase.

Any pointers?

Thanks,
Rob

Alex Eagle

unread,
Sep 19, 2022, 11:07:26 AM9/19/22
to bazel-discuss
Bazel doesn't have plugins, so there isn't a direct analog to that format plugin.

Aspect CLI does have plugins, so you could teach it to do `bazel format`. I think this is a plugin we'll probably introduce ourselves soon.

The idiomatic Bazel answer is just "what formatting tool does that format plugin use", then just `bazel run` that tool. You might want to look at https://github.com/aspect-build/bazel-super-formatter which is quite new but aims to solve this problem.

-Alex

Robert Cauble

unread,
Sep 19, 2022, 2:24:04 PM9/19/22
to Alex Eagle, bazel-discuss
I found this example which I was able to adapt to the java-formatter: https://ltekieli.com/creating-and-using-bazel-aspects/

I basically have an aspect that collects lists of files (like in the example) and a run_shell action within my custom rule that calls java-formatter. For the run_shell I needed to tell it to disable the sandbox so that it can modify the source files. Mildly sketchy but it seems to work.

Like in the example I have a macro that needs a one-liner to be added to each BUILD file to basically generate calls to the format rule for each target. 

I was hoping there might be a better way to do this like where the aspect is enabled from the command line and then we don't require that each build file have a call to the rule. I was able to get close with that approach but the problem I ran into was that the aspect needs to declare outputs and if there's nothing depending on the outputs it never gets executed. I was trying to think if there's a way that I can transform the underlying target so that the aspect's output becomes an input to them, thus establishing a dependency of the target on the aspect. That'd avoid the need of the macro whose sole purpose is to create an extra target for each of the underlying targets. Doesn't seem to be the way aspects work, though?


--
You received this message because you are subscribed to the Google Groups "bazel-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bazel-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/bazel-discuss/a5ba8294-edcb-4030-91fa-fb55804e271cn%40googlegroups.com.

Robert Cauble

unread,
Sep 19, 2022, 2:50:02 PM9/19/22
to Alex Eagle, bazel-discuss
Superformatter seems to have done the trick. Thank you!

One thing that was surprising is that we have a submodule referenced as srcs in our BUILD file but the formatter didn't seem to format anything in there. That's actually the behavior I want but was surprised cause I thought I'd need to do something explicit. Is the default not to navigate sub-modules?

On Mon, Sep 19, 2022 at 10:07 AM Alex Eagle <al...@aspect.dev> wrote:
--

Alex Eagle

unread,
Sep 19, 2022, 4:17:02 PM9/19/22
to bazel-discuss
Yes, if run with no positional arguments (filenames), super-formatter uses `git ls-files` to figure out which files ought to be formatted, so that it doesn't walk into bazel-ignore'd places. Take a look at format.sh in that repo.

All that complexity with using BUILD files and java_library targets and aspects just to locate .java files seems pretty overkill now that you see this approach, right? :)

Robert Cauble

unread,
Sep 19, 2022, 7:23:45 PM9/19/22
to Alex Eagle, bazel-discuss
Yeah, thanks again, this is much better! 

One question, is it expected that BUILD files don't get formatted? .bzl files do but I haven't seen any BUILD files. I haven't thoroughly tested it -- could just be that I am extremely lucky to have BUILD files already formatted.

Alex Eagle

unread,
Sep 19, 2022, 9:09:25 PM9/19/22
to Robert Cauble, bazel-discuss
Looks like the current release assumes `BUILD.bazel` rather than bare `BUILD` https://github.com/aspect-build/bazel-super-formatter/blob/main/format/format.sh#L70-L74

Robert Cauble

unread,
Sep 20, 2022, 9:54:17 AM9/20/22
to Alex Eagle, bazel-discuss
Thanks, renaming did the trick!

On Sep 19, 2022, at 8:09 PM, Alex Eagle <al...@aspect.dev> wrote:


Reply all
Reply to author
Forward
0 new messages