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

feat: Improve error message #22

Merged
merged 2 commits into from
Apr 12, 2024
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
9 changes: 7 additions & 2 deletions build_script/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,16 @@ package(
)

sh_binary(
name = "conditionally_execute",
srcs = ["conditionally_execute.bash"],
name = "check_each_message",
srcs = ["check_each_message.bash"],
)

sh_binary(
name = "check_emptiness",
srcs = ["check_emptiness.bash"],
)

sh_binary(
name = "try_build",
srcs = ["try_build.bash"],
)
53 changes: 53 additions & 0 deletions build_script/check_each_message.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#!/usr/bin/env bash

set -euo pipefail

usage() {
cat <<EOS >&2
Check each message file.

Exits with an error if the pattern is not found in the message file.

Usage: $0 MATCHER PATTERN MESSAGE_FILE [MARKER_FILE ...]

MATCHER
Executable to check if the pattern string is inside the message file

PATTERN
Pattern string

MESSAGE_FILE
Text file containing a message

MARKER_FILE ...
Empty text files are created at the specified paths before exiting the script
EOS
}

if [ "$#" -lt 3 ]; then
echo "ERROR: Incorrect number of arguments" >&2
usage
exit 1
fi

matcher=$1
pattern=$2
message_file=$3
shift 3

files_to_touch=("$@")

# Make sure the required files are touched before exiting
if [[ "${#files_to_touch[@]}" -gt 0 ]]; then
trap 'touch "${files_to_touch[@]}"' EXIT
fi

if ! "${matcher}" "${pattern}" "${message_file}" ; then
echo "Pattern '${pattern}' is not found in the message file '${message_file}' with the matcher '${matcher}'." >&2
echo "" >&2
echo "---------- Message: BEGIN ----------" >&2
cat "${message_file}" >&2
echo "---------- Message: END ----------" >&2
echo "" >&2
exit 1
fi
2 changes: 1 addition & 1 deletion build_script/check_emptiness.bash
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ OPTIONS
-m MESSAGE
Error message when no files are empty
-n NEW_FILE_PATH
If specified, create a new empty file
If specified, create a new empty file before exiting the script
EOS
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@ set -euo pipefail

usage() {
cat <<EOS >&2
Wrapper to conditionally execute the command.
Try building the code.

With the option '-f', this script can receive arbitrary number of files.
If any of them is an empty file, this script exits successfully
without executing the command.
without executing the build command.

Even if the build command fails, this script won't exit with an error.

Usage: $0 [OPTIONS] COMMAND

OPTIONS
-i
Ignore error when executing the command.
-f FILE_TO_CHECK
Successfully exit if the file is empty
-h
Expand All @@ -23,12 +23,11 @@ OPTIONS
A file path to write stderr message
-o STDOUT_FILE
A file path to write stdout message
-m MESSAGE
Message when the command fails
-n NEW_FILE_PATH
If specified, create a new empty file before executing the command
If specified, create a new empty file with 'touch' before exiting the script.

COMMAND
Build command.
Executed if there's no empty file given with '-f'.
EOS
}
Expand All @@ -53,10 +52,10 @@ exit_if_empty_file() {

files_to_check=()
files_to_touch=()
ignore_error="false"
error_message="ERROR: execution failed"
stdout_file="/dev/null"
stderr_file="/dev/null"

while getopts "e:f:him:n:o:" opt; do
while getopts "e:f:hn:o:" opt; do
case "${opt}" in
e)
stderr_file="${OPTARG}"
Expand All @@ -68,12 +67,6 @@ while getopts "e:f:him:n:o:" opt; do
usage
exit 0
;;
i)
ignore_error="true"
;;
m)
error_message="${OPTARG}"
;;
n)
files_to_touch+=("${OPTARG}")
;;
Expand All @@ -95,11 +88,4 @@ if [[ "${#files_to_check[@]}" -gt 0 ]]; then
done
fi

if [[ "${ignore_error}" == "true" ]]; then
"$@" >"${stdout_file:-"/dev/null"}" 2>"${stderr_file:-"/dev/null"}" || true
else
if ! "$@" >"${stdout_file:-"/dev/null"}" 2>"${stderr_file:-"/dev/null"}" ; then
echo "${error_message}"
exit 1
fi
fi
"$@" >"${stdout_file}" 2>"${stderr_file}" || true
30 changes: 10 additions & 20 deletions lang/cc/build_error.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,8 @@ def _try_compile(ctx):
# Input files for executing the action
inputs = [ctx.file.src]

# Arguments for `conditionally_execute`
# Arguments for `try_build.bash`
args = ctx.actions.args()
args.add("-i")
args.add("-e", compile_stderr)
args.add("-o", compile_stdout)
args.add("-n", compile_output)
Expand Down Expand Up @@ -169,7 +168,7 @@ def _try_compile(ctx):
ctx.actions.run(
outputs = [compile_output, compile_stdout, compile_stderr],
inputs = inputs,
executable = _get_executable_file(ctx.attr._conditionally_execute),
executable = _get_executable_file(ctx.attr._try_build),
arguments = [args],
tools = cc_toolchain.all_files,
)
Expand Down Expand Up @@ -228,9 +227,8 @@ def _try_link(ctx, compile_output):

inputs = [compile_output] + ctx.attr.additional_linker_inputs

# Arguments for `conditionally_execute`
# Arguments for `try_build.bash`
args = ctx.actions.args()
args.add("-i")
args.add("-e", link_stderr)
args.add("-o", link_stdout)
args.add("-n", link_output)
Expand Down Expand Up @@ -262,7 +260,7 @@ def _try_link(ctx, compile_output):
ctx.actions.run(
outputs = [link_output, link_stdout, link_stderr],
inputs = inputs,
executable = _get_executable_file(ctx.attr._conditionally_execute),
executable = _get_executable_file(ctx.attr._try_build),
arguments = [args],
tools = cc_toolchain.all_files,
)
Expand Down Expand Up @@ -383,8 +381,8 @@ _try_build = rule(
"_check_emptiness": attr.label(
default = Label("//build_script:check_emptiness"),
),
"_conditionally_execute": attr.label(
default = Label("//build_script:conditionally_execute"),
"_try_build": attr.label(
default = Label("//build_script:try_build"),
),
},
fragments = ["cpp"],
Expand Down Expand Up @@ -436,20 +434,12 @@ def _check_each_message(ctx, message_file, matcher, pattern):
ctx.actions.run(
outputs = [marker_file],
inputs = [message_file],
executable = _get_executable_file(ctx.attr._conditionally_execute),
executable = _get_executable_file(ctx.attr._check_each_message),
arguments = [
"-m",
"Pattern '{pattern}' is not found in '{message_file}' with the matcher '{matcher}'".format(
pattern = pattern,
message_file = message_file.path,
matcher = matcher.path,
),
"-n",
marker_file.path,
# From here, the matcher command starts
matcher.path,
pattern,
message_file.path,
marker_file.path,
],
tools = [matcher],
)
Expand Down Expand Up @@ -551,8 +541,8 @@ _check_messages = rule(
doc = "Pattern string for stdout while linking",
mandatory = False,
),
"_conditionally_execute": attr.label(
default = Label("//build_script:conditionally_execute"),
"_check_each_message": attr.label(
default = Label("//build_script:check_each_message"),
),
},
provides = [CcBuildErrorInfo, DefaultInfo],
Expand Down