Skip to content

Commit

Permalink
[lint] Allow running buildifier via bazel run (RobotLocomotion#21329)
Browse files Browse the repository at this point in the history
  • Loading branch information
jwnimmer-tri authored Apr 22, 2024
1 parent d18e8f1 commit 1843eaa
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 16 deletions.
11 changes: 4 additions & 7 deletions tools/lint/buildifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@ def main(workspace_name="drake"):
sys.exit(1)
argv.extend(found)

# Make cwd be what the user expected, not the runfiles tree.
if "BUILD_WORKING_DIRECTORY" in os.environ:
os.chdir(os.environ["BUILD_WORKING_DIRECTORY"])

# Provide helpful diagnostics when in check mode. Buildifier's -mode=check
# uses exitcode 0 even when lint exists; we use whether or not its output
# was empty to tell whether there was lint.
Expand All @@ -134,13 +138,6 @@ def main(workspace_name="drake"):
print("NOTE: see https://drake.mit.edu/bazel.html#buildifier")
return 1

# In fix mode, disallow running from within the Bazel sandbox.
if "-mode=diff" not in argv and "--mode=diff" not in argv:
if os.getcwd().endswith(".runfiles/drake"):
print("ERROR: do not use 'bazel run' for buildifier in fix mode")
print("ERROR: use bazel-bin/tools/lint/buildifier instead")
return 1

# In fix or diff mode, just let buildifier do its thing.
return subprocess.call(tool_cmds + argv)

Expand Down
9 changes: 0 additions & 9 deletions tools/lint/test/buildifier_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,3 @@ def test_mode_check(self):
"bazel build //tools/lint/..."),
"NOTE: see https://drake.mit.edu/bazel.html#buildifier"
])

def test_mode_fix(self):
returncode, output = self._call_buildifier(
self._make_build_testdata(add_error=False),
["-mode=fix"])
self.assertEqual(returncode, 1, output)
self.assertIn(
"do not use 'bazel run' for buildifier in fix mode",
output)

0 comments on commit 1843eaa

Please sign in to comment.