aspect-build / bazel-super-formatter Goto Github PK
View Code? Open in Web Editor NEWHermetic meta-formatter to format code in most languages
License: Apache License 2.0
Hermetic meta-formatter to format code in most languages
License: Apache License 2.0
No bzlmod support, so it has to be installed outside of MODULE.bazel
This repo got forked from bazel-contrib/rules-template before MODULE.bazel support was added, and so it lacks from such features, does it make sense to add it? Would you mind if I contribute it?
Some tools like pre-commit expect formatter tools to be called like formatter file.fmt
.
This is nice because that tool has already determined which files are modified by the user in a given context, so we won't have to wait to "re-format the world".
Trying to run v0.2.2 with
python_register_toolchains(
name = "python3",
python_version = "3.10",
)
fails with
Traceback (most recent call last):
File "/home/ph03/.cache/bazel/_bazel_ph03/2db8cbce6fe365e36a9715d20aab49e9/execroot/dsai/bazel-out/k8-opt/bin/external/aspect_rules_format_pypi_black/rules_python_wheel_entry_point_black.runfiles/aspect_rules_format_pypi_black/rules_python_wheel_entry_point_black.py", line 3, in <module>
from black import patched_main
File "/home/ph03/.cache/bazel/_bazel_ph03/2db8cbce6fe365e36a9715d20aab49e9/execroot/dsai/bazel-out/k8-opt/bin/external/aspect_rules_format_pypi_black/rules_python_wheel_entry_point_black.runfiles/aspect_rules_format_pypi_black/site-packages/black/__init__.py", line 42, in <module>
from black.nodes import STARS, syms, is_simple_decorator_expression
File "/home/ph03/.cache/bazel/_bazel_ph03/2db8cbce6fe365e36a9715d20aab49e9/execroot/dsai/bazel-out/k8-opt/bin/external/aspect_rules_format_pypi_black/rules_python_wheel_entry_point_black.runfiles/aspect_rules_format_pypi_black/site-packages/black/nodes.py", line 24, in <module>
from typing_extensions import TypeGuard
ModuleNotFoundError: No module named 'typing_extensions'
FAILED: A formatter tool exited with code 123
Try running 'bazel run @aspect_rules_format//format' to fix this.
while running with
python_register_toolchains(
name = "python3",
python_version = "3.10",
)
already fails at the configuration step with
INFO: Invocation ID: c11114e4-935a-4a07-b80a-3b9cddb4bf13
ERROR: /home/ph03/.cache/bazel/_bazel_ph03/2db8cbce6fe365e36a9715d20aab49e9/external/aspect_rules_format_pypi_black/BUILD.bazel:22:11: no such package '@aspect_rules_format_pypi_typing_extensions//': The repository '@aspect_rules_format_pypi_typing_extensions' could not be resolved: Repository '@aspect_rules_format_pypi_typing_extensions' is not defined and referenced by '@aspect_rules_format_pypi_black//:pkg'
ERROR: Analysis of target '@aspect_rules_format//format:format' failed; build aborted:
which looks like an inconsistency to me. Note that I have a python toolchain registered before, i.e.,
python_register_toolchains(
name = "python39",
# Available versions are listed in @rules_python//python:versions.bzl
python_version = "3.9",
)
but that one is not picked up by bazel-super-formatter
as it's expecting the python toolchain to be called python3
not python39
(could that be the source of the issue?)
With build --@aspect_rules_format//format:python_enabled=false
set in .bazelrc
, I'm getting this error message:
ERROR: Failed to load Starlark extension '@python3//:defs.bzl'.
Cycle in the workspace file detected. This indicates that a repository is used prior to being defined.
The following chain of repository dependencies lead to the missing definition.
- @python3
This could either mean you have to add the '@python3' repository with a statement like `http_archive` in your WORKSPACE file (note that transitive dependencies are not added automatically
), or move an existing definition earlier in your WORKSPACE file.
INFO: Reading rc options for 'run' from /Users/ewhauser/working/cadencerpm/monorepo/.bazelrc:
Inherited 'build' options: --sandbox_default_allow_network=false --incompatible_allow_tags_propagation --modify_execution_info=PackageTar=+no-remote --nolegacy_external_runfiles --works
pace_status_command tools/workspace_status.sh --build_metadata=REPO_URL=https://github.com/cadencerpm/monorepo --noexperimental_convenience_symlinks --noremote_upload_local_results --expe
rimental_remote_merkle_tree_cache=true --experimental_remote_merkle_tree_cache_size=1000 --remote_local_fallback --incompatible_remote_results_ignore_disk --noexperimental_check_output_fi
les --experimental_reuse_sandbox_directories --incompatible_remote_results_ignore_disk --aspects //bazel/api_linter:defs.bzl%api_linter_aspect --output_groups=+api_linter --@aspect_rules_
format//format:python_enabled=false
ERROR: @aspect_rules_format//format:python_enabled :: Error loading option @aspect_rules_format//format:python_enabled: cycles detected during target parsing
Based on the notes in releases, it seems like adding the Python toolchain is optional if you aren't using black
.
@maven//:com_facebook_ktfmt_jar_with_dependencies
I set up the super-formatter in our .pre-commit-config.yaml [1] in VideoAmp/central, a repo with a lot of files (>3500). I observe that when I run pre-commit run --all-files bazel-super-formatter
, which is supposed to run the pre-commit on all files in the repo, many files are missed. By contrast, everything works as expected when I run bazel run @aspect_rules_format//format
(for example, docs/go/README.md
currently needs formatting at HEAD).
My theory is that pre-commit's batching mechanism isn't working properly with the super formatter.
[1] used this config block (same as from this repo's README):
- id: bazel-super-formatter
name: run bazel super formatter
language: system
entry: bazel run @aspect_rules_format//format
files: .*
stages: [commit]
Development (host) and target OS/architectures:
Output of bazel --version
: 6.0.0 (bazelisk)
Language(s) and/or frameworks involved:
markdown, shell, js
The "what happened" description lays it out pretty well. For Alex: there are currently 6 files in the repo (which you can access) that need formatting at HEAD, and I'll leave those there for you to see.
No response
The custom configuration flags in the README to disable formatters cause warning log spew as build
is inherrited by verbs that don't support custom flags, eg:
bazel output_base
WARNING: info command does not support starlark options. Ignoring options: [--@aspect_rules_format//format:java_enabled=false]
For a monorepo managed by Bazel and with bazel super formatter as a pre-commit
plugin, we are getting wrong binaries for its dependencies. Specifically when the pre-commit
hooks for bazel super formatter are applied, wrong binaries for buf
and buildifier
are being downloaded.
As a consequence, the formatting process ends in a broken state.
It is expected the formatting process to work correctly if the aarch64
binaries for those tools are downloaded to the system.
NOTE: for example when the bazel super formatter pre-commit
hook runs, the buildifier
binary is being downloaded to ~/.cache/pre-commit/buildifier/linux-amd64-5.1.0/buildifier/buildifier
. Notice the linux-amd64-5.1.0
directory in the download path.
Development (host) and target OS/architectures:
aarch64 GNU/Linux
Output of bazel --version
:
bazel 6.0.0
Version of the Aspect rules, or other relevant rules from your
WORKSPACE
or MODULE.bazel
file:
1.0.0
(Bazel Super Formatteer 1.0.0 version)
Language(s) and/or frameworks involved:
Protocol Buffers and Bazel Build files, but the consequence is that the whole formatting process fails.
Working in a Linux environment that runs in a aarch64
architecture if you try to apply the formatter in a repository with modified Protocol Buffers or Bazel BUILD files (BUILD
or BUILD.bazel
), the formatting process will fail since Bazel Super Formatter downloads buf
and buildifier
amd64
binaries for a aarch64
architecture.
No response
Format fails with the error:
[2023-04-19T23:58:05Z] Running Prettier...
[2023-04-19T23:58:05Z] [error] Cannot find module 'mvdan-sh'
Development (host) and target OS/architectures:
Output of bazel --version
: 6.1.2
Version of the Aspect rules, or other relevant rules from your
WORKSPACE
or MODULE.bazel
file: 2.0.0 using WORKSPACE
Language(s) and/or frameworks involved:
prettier / sh
No response
No response
I'm quite new to Bazel so please point out anything that doesn't make sense or isn't actually possible ๐
What?
Would it be possible to add a formatting rule that can then be used from BUILD.bazel
files?
e.g.
# greeting/BUILD.bazel
format(
name = "format_greeting_lib",
srcs = glob("*.ts"),
)
# BUILD.bazel
format(
name = "format_hello_lib",
srcs = glob("*.ts"),
deps = ["//greeting:format_greeting_lib"],
)
And then it could be run like this:
bazel run //src/format_hello_lib
As the //greeting:format_greeting_lib
target is a dependency, it would also be formatted but could potentially be cached
Why?
As I understand it, currently bazel-super-formatter either formats all files in a workspace (with some exceptions based on .gitignore or other global-to-the-workspace config) or it needs to be told on the command line which files to format.
Formatting all files in a workspace could get quite heavy and seems a shame not to take advantage of Bazel's build cache in this situation.
rules_python has changed the naming scheme for its toolchain repos and hence the load of interpreter
from @python3//:defs.bzl
fails with the latest versions of rules_python.
Development (host) and target OS/architectures:
Output of bazel --version
: 6.3.2
Language(s) and/or frameworks involved: python
No response
No response
No response
I'm currently investigating how our team should use Bazel to lint/format files and came across this amazing work! I'm curious how you are imagining formatters built with Bazel to be configured with an editor like VSCode so you get behaviour like "format on save" and inline suggestions.
Running latest version formatting checks with bazel run @aspect_rules_format//format -- --mode check
fail in prettifier
with
Checking formatting...
All matched files use Prettier code style!
FATAL ERROR: v8::FromJust Maybe value is Nothing.
1: 0xb09c10 node::Abort() [/home/developer/.cache/bazel/_bazel_developer/6fc7dd3d681346fb996e4b7beb1cd826/execroot/tair-project-template/bazel-out/k8-opt/bin/external/aspect_rules_format/format/format.runfiles/tair-project-template/../node_linux_amd64/bin/nodejs/bin/node]
2: 0xa1c193 node::FatalError(char const*, char const*) [/home/developer/.cache/bazel/_bazel_developer/6fc7dd3d681346fb996e4b7beb1cd826/execroot/tair-project-template/bazel-out/k8-opt/bin/external/aspect_rules_format/format/format.runfiles/tair-project-template/../node_linux_amd64/bin/nodejs/bin/node]
3: 0xcf8afa v8::Utils::ReportApiFailure(char const*, char const*) [/home/developer/.cache/bazel/_bazel_developer/6fc7dd3d681346fb996e4b7beb1cd826/execroot/tair-project-template/bazel-out/k8-opt/bin/external/aspect_rules_format/format/format.runfiles/tair-project-template/../node_linux_amd64/bin/nodejs/bin/node]
4: 0xb0da7d node::fs::FileHandle::CloseReq::Resolve() [/home/developer/.cache/bazel/_bazel_developer/6fc7dd3d681346fb996e4b7beb1cd826/execroot/tair-project-template/bazel-out/k8-opt/bin/external/aspect_rules_format/format/format.runfiles/tair-project-template/../node_linux_amd64/bin/nodejs/bin/node]
5: 0xb0dbd9 [/home/developer/.cache/bazel/_bazel_developer/6fc7dd3d681346fb996e4b7beb1cd826/execroot/tair-project-template/bazel-out/k8-opt/bin/external/aspect_rules_format/format/format.runfiles/tair-project-template/../node_linux_amd64/bin/nodejs/bin/node]
6: 0x156015d [/home/developer/.cache/bazel/_bazel_developer/6fc7dd3d681346fb996e4b7beb1cd826/execroot/tair-project-template/bazel-out/k8-opt/bin/external/aspect_rules_format/format/format.runfiles/tair-project-template/../node_linux_amd64/bin/nodejs/bin/node]
7: 0x1564936 [/home/developer/.cache/bazel/_bazel_developer/6fc7dd3d681346fb996e4b7beb1cd826/execroot/tair-project-template/bazel-out/k8-opt/bin/external/aspect_rules_format/format/format.runfiles/tair-project-template/../node_linux_amd64/bin/nodejs/bin/node]
8: 0x1577064 [/home/developer/.cache/bazel/_bazel_developer/6fc7dd3d681346fb996e4b7beb1cd826/execroot/tair-project-template/bazel-out/k8-opt/bin/external/aspect_rules_format/format/format.runfiles/tair-project-template/../node_linux_amd64/bin/nodejs/bin/node]
9: 0x1565288 uv_run [/home/developer/.cache/bazel/_bazel_developer/6fc7dd3d681346fb996e4b7beb1cd826/execroot/tair-project-template/bazel-out/k8-opt/bin/external/aspect_rules_format/format/format.runfiles/tair-project-template/../node_linux_amd64/bin/nodejs/bin/node]
10: 0xa43dd5 node::SpinEventLoop(node::Environment*) [/home/developer/.cache/bazel/_bazel_developer/6fc7dd3d681346fb996e4b7beb1cd826/execroot/tair-project-template/bazel-out/k8-opt/bin/external/aspect_rules_format/format/format.runfiles/tair-project-template/../node_linux_amd64/bin/nodejs/bin/node]
11: 0xbd3792 node::worker::Worker::Run() [/home/developer/.cache/bazel/_bazel_developer/6fc7dd3d681346fb996e4b7beb1cd826/execroot/tair-project-template/bazel-out/k8-opt/bin/external/aspect_rules_format/format/format.runfiles/tair-project-template/../node_linux_amd64/bin/nodejs/bin/node]
12: 0xbd3f28 [/home/developer/.cache/bazel/_bazel_developer/6fc7dd3d681346fb996e4b7beb1cd826/execroot/tair-project-template/bazel-out/k8-opt/bin/external/aspect_rules_format/format/format.runfiles/tair-project-template/../node_linux_amd64/bin/nodejs/bin/node]
13: 0x7ff0340d5b43 [/lib/x86_64-linux-gnu/libc.so.6]
14: 0x7ff034167a00 [/lib/x86_64-linux-gnu/libc.so.6]
/home/developer/.cache/bazel/_bazel_developer/6fc7dd3d681346fb996e4b7beb1cd826/execroot/tair-project-template/bazel-out/k8-opt/bin/external/aspect_rules_format/format/prettier.sh: line 441: 3394 Aborted (core dumped) "$JS_BINARY__NODE_WRAPPER" ${NODE_OPTIONS[@]+"${NODE_OPTIONS[@]}"} -- "$entry_point" ${ARGS[@]+"${ARGS[@]}"} 0<&0
FAILED: A formatter tool exited with code 123
I can see YAML formatting was disabled in https://github.com/aspect-build/bazel-super-formatter/releases/tag/v0.3.1 - are there perhaps some other extension failing with similar issues (e.g., markdown?).
Is there an easy way to use the latest node / prettier versions maybe?
Development (host) and target OS/architectures: Linux
Output of bazel --version
:
bazel 5.3.1
Version of the Aspect rules, or other relevant rules from your
WORKSPACE
or MODULE.bazel
file:
0.3.1
No response
No response
The tool does not seem to handle the case where the file path has spaces in. There are errors like the following:
michaelboyd bazel-experiment $ bazel run @aspect_rules_format//format
INFO: Analyzed target @aspect_rules_format//format:format (0 packages loaded, 0 targets configured).
INFO: Found 1 target...
Target @aspect_rules_format//format:format up-to-date:
bazel-bin/external/aspect_rules_format/format/format
INFO: Elapsed time: 0.257s, Critical Path: 0.01s
INFO: 1 process: 1 internal.
INFO: Build completed successfully, 1 total action
INFO: Build completed successfully, 1 total action
Running Buildifier...
Running Prettier...
...
[error] No files matching the pattern were found: "path/first".
[error] No files matching the pattern were found: "word.json".
Unfortunately, even trying to cover the problematic files with .prettierignore
does not seem to work.
I have looked into this and the problem seems to be in format.sh.
A few languages have some (relatively) standard tools for sorting imports (https://github.com/PyCQA/isort, https://pkg.go.dev/golang.org/x/tools/cmd/goimports) that are separate from the formatters. Is there openness to adding those as a part of super formatter or considered out of scope?
The tool only does formatting.
I wonder why not introduce linting as well? Or is there an existing tool which does "super-linting" and integrates with Bazel?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.