Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tweaks to scalafmt rules/script to support bazel test wrappers #1344

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions scala/private/phases/phase_scalafmt.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,25 @@ load(

def phase_scalafmt(ctx, p):
if ctx.attr.format:
manifest, files = _build_format(ctx)
manifest, files, srcs = _build_format(ctx)
_formatter(ctx, manifest, files, ctx.file._runner, ctx.outputs.scalafmt_runner)
_formatter(ctx, manifest, files, ctx.file._testrunner, ctx.outputs.scalafmt_testrunner)

# Return a depset containing all the relevant files, so a wrapping `sh_test` can successfully access them.
return struct(runfiles = depset([manifest] + files + srcs))
else:
_write_empty_content(ctx, ctx.outputs.scalafmt_runner)
_write_empty_content(ctx, ctx.outputs.scalafmt_testrunner)
return None

def _build_format(ctx):
files = []
srcs = []
manifest_content = []
for src in ctx.files.srcs:
# only format scala source files, not generated files
if src.path.endswith(_scala_extension) and src.is_source:
srcs.append(src)
file = ctx.actions.declare_file("{}.fmt.output".format(src.short_path))
files.append(file)
ctx.actions.run(
Expand All @@ -40,7 +46,7 @@ def _build_format(ctx):
manifest = ctx.actions.declare_file("format/{}/manifest.txt".format(ctx.label.name))
ctx.actions.write(manifest, "\n".join(manifest_content) + "\n")

return manifest, files
return manifest, files, srcs

def _formatter(ctx, manifest, files, template, output_runner):
ctx.actions.run_shell(
Expand Down
15 changes: 12 additions & 3 deletions scala/scalafmt/private/format-test.template.sh
Original file line number Diff line number Diff line change
@@ -1,15 +1,24 @@
#!/bin/bash -e
WORKSPACE_ROOT="${1:-$BUILD_WORKSPACE_DIRECTORY}"

# Explanation: IF $BUILD_WORKSPACE_DIRECTORY is set to something (as it would be during a
# `bazel run`), then append a trailing `/`. If it's not set (as it wouldn't be during
# a `bazel test` invocation in a wrapping `sh_test` rule), then elide the trailing `/`, and
# instead rely upon a relative path from the test's runtrees. The corresponding change
# to `phase_scalafmt` places the source files into the `runfiles` set, so they'll be symlinked
# correctly in the appropriate relative location.
WORKSPACE_ROOT="${1:-${BUILD_WORKSPACE_DIRECTORY}${BUILD_WORKSPACE_DIRECTORY:+/}}"

RUNPATH="${TEST_SRCDIR-$0.runfiles}"/%workspace%
RUNPATH=(${RUNPATH//bin/ })
RUNPATH="${RUNPATH[0]}"bin

EXIT=0

while read original formatted; do
if [[ ! -z "$original" ]] && [[ ! -z "$formatted" ]]; then
if ! cmp -s "$WORKSPACE_ROOT/$original" "$RUNPATH/$formatted"; then
if ! cmp -s "${WORKSPACE_ROOT}$original" "$RUNPATH/$formatted"; then
echo $original
diff "$WORKSPACE_ROOT/$original" "$RUNPATH/$formatted" || true
diff "${WORKSPACE_ROOT}$original" "$RUNPATH/$formatted" || true
EXIT=1
fi
fi
Expand Down