diff --git a/scala/private/phases/phase_scalafmt.bzl b/scala/private/phases/phase_scalafmt.bzl index a42410980e..0f8f19728f 100644 --- a/scala/private/phases/phase_scalafmt.bzl +++ b/scala/private/phases/phase_scalafmt.bzl @@ -10,19 +10,23 @@ 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) 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( @@ -40,7 +44,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( diff --git a/scala/scalafmt/private/format-test.template.sh b/scala/scalafmt/private/format-test.template.sh index 0ca9d99b90..0648ff52d7 100644 --- a/scala/scalafmt/private/format-test.template.sh +++ b/scala/scalafmt/private/format-test.template.sh @@ -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