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

Fixes for gazelle rule #324

Merged
merged 1 commit into from
Sep 20, 2018
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
15 changes: 7 additions & 8 deletions def.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ def _gazelle_runner_impl(ctx):
)
runfiles = ctx.runfiles(files = [
ctx.executable.gazelle,
ctx.file.workspace,
go.go,
])
return [DefaultInfo(
Expand Down Expand Up @@ -88,11 +87,6 @@ _gazelle_runner = _go_rule(
"build_tags": attr.string_list(),
"prefix": attr.string(),
"extra_args": attr.string_list(),
"workspace": attr.label(
mandatory = True,
allow_single_file = True,
cfg = "data",
),
"_template": attr.label(
default = "@bazel_gazelle//internal:gazelle.bash.in",
allow_single_file = True,
Expand All @@ -109,10 +103,15 @@ def gazelle(name, **kwargs):
fail("{}: both args and extra_args were provided".format(name))
kwargs["extra_args"] = kwargs["args"]
kwargs.pop("args")
runner_name = name + "-runner"
_gazelle_runner(
name = runner_name,
tags = ["manual"],
**kwargs
)
native.sh_binary(
name = name,
srcs = [runner_name],
args = ["-bazel_run"],
workspace = "//:WORKSPACE",
tags = ["manual"],
**kwargs
)
54 changes: 24 additions & 30 deletions internal/gazelle.bash.in
Original file line number Diff line number Diff line change
Expand Up @@ -25,25 +25,20 @@ GAZELLE_LABEL=@@GAZELLE_LABEL@@
ARGS=@@ARGS@@
GOTOOL=@@GOTOOL@@

# find_repo_root prints the absolute path to the repository root. This is
# discovered by locating the WORKSPACE file in a parent directory, and
# deferencing symlinks. If no WORKSPACE file is found, this function prints
# an error and exits.
function find_repo_root {
pushd . &>/dev/null
while [ $(pwd) != / -a ! -f WORKSPACE ]; do
cd ..
done
if [ ! -f WORKSPACE ]; then
echo "error: could not find WORKSPACE file in any parent directory" >&2
exit 1
# find_runfile prints the location of a runfile in the source workspace,
# either by reading the symbolic link or reading the runfiles manifest.
function find_runfile {
local runfile=$1
if [ -f "$runfile" ]; then
readlink "$runfile"
return
fi
if [ "$is_bazel_run" = true ]; then
dirname $(readlink WORKSPACE)
else
pwd
runfile=$(echo "$runfile" | sed -e 's!^\(\.\./\|external/\)!!')
if grep -q "^$runfile" MANIFEST; then
grep "^$runfile" MANIFEST | head -n 1 | cut -d' ' -f2
return
fi
popd &>/dev/null
# printing nothing indicates failure
}

# bazel_build_get_path builds a given target and prints the absolute path
Expand All @@ -61,18 +56,10 @@ function bazel_build_get_path {
# use the SDK used by the workspace in case the Go SDK is not installed
# on the host system or is a different version.
function set_goroot {
local gotool=
if [ -e "$GOTOOL" ]; then
# Normally, GOTOOL will be a symlinked runfile.
gotool=$GOTOOL
fi
if [ -z "$gotool" -a -e MANIFEST ]; then
# On Windows, runfiles aren't symlinked, but they're listed in MANIFEST.
gotool=$(grep bin/go MANIFEST | head -n 1 | cut -d' ' -f2)
fi
local gotool=$(find_runfile "$GOTOOL")
if [ -z "$gotool" ]; then
echo "$0: warning: could not locate GOROOT used by rules_go" >&2
return 1
return
fi
export GOROOT=$(cd "$(dirname "$gotool")/.."; pwd)
if type cygpath >/dev/null 2>&1; then
Expand All @@ -99,9 +86,16 @@ if [ "$is_bazel_run" = true ]; then
# If the script was invoked by "bazel run", jump out of the execroot, into
# the workspace before running Gazelle.
# TODO(jayconrod): detect when a command can't be run this way.
set_goroot
gazelle_short_path=$(readlink "$GAZELLE_SHORT_PATH")
cd $(find_repo_root)
gazelle_short_path=$(find_runfile "$GAZELLE_SHORT_PATH")
if [ -z "$gazelle_short_path" ]; then
echo "error: could not locate gazelle binary" >&2
exit 1
fi
if [ -z "${BUILD_WORKSPACE_DIRECTORY-}" ]; then
echo "error: BUILD_WORKSPACE_DIRECOTRY not set" >&2
exit 1
fi
cd "$BUILD_WORKSPACE_DIRECTORY"
"$gazelle_short_path" "${ARGS[@]}"
else
# If the script was invoked directly, check whether the script is out of
Expand Down