diff --git a/cue/cue-run-from-archived-runfiles b/cue/cue-run-from-archived-runfiles index f9e55f5..1b10988 100755 --- a/cue/cue-run-from-archived-runfiles +++ b/cue/cue-run-from-archived-runfiles @@ -10,18 +10,20 @@ source "${RUNFILES_DIR:-/dev/null}/$f" 2>/dev/null || \ { echo>&2 "ERROR: cannot find $f"; exit 1; }; f=; set -e function usage() { - printf "usage: %s [-i instance_path] [-m module_file] [-p package_name] cue_tool cue_subcommand extra_args_file packageless_files_file output_file [args...]\n" "$(basename "${0}")" 1>&2 + printf "usage: %s [-d cue_debug] [-i instance_path] [-m module_file] [-p package_name] cue_tool cue_subcommand extra_args_file packageless_files_file output_file [args...]\n" "$(basename "${0}")" 1>&2 exit 2 } +cue_debug= instance_path= module_file= package_name= function parse_args() { - while getopts i:m:p: name + while getopts d:i:m:p: name do case "${name}" in + d) cue_debug="${OPTARG}";; i) instance_path="${OPTARG}";; h) usage;; m) module_file="${OPTARG}";; @@ -101,6 +103,10 @@ if [ -n "${module_file}" ]; then cd "${module_path}" fi +if [ -n "${cue_debug}" ]; then + export CUE_DEBUG="${cue_debug}" +fi + # NB: See https://stackoverflow.com/questions/7577052 for the odd # treatment of the "packageless_file_args" array variable here, # handling the case where the array winds up empty for lack of diff --git a/cue/cue-run-from-runfiles b/cue/cue-run-from-runfiles index 8a41568..8509182 100755 --- a/cue/cue-run-from-runfiles +++ b/cue/cue-run-from-runfiles @@ -10,20 +10,22 @@ source "${RUNFILES_DIR:-/dev/null}/$f" 2>/dev/null || \ { echo>&2 "ERROR: cannot find $f"; exit 1; }; f=; set -e function usage() { - printf "usage: %s [-c cue_cache_directory] [-i instance_path] [-m module_file] [-p package_name] cue_tool cue_subcommand extra_args_file packageless_files_file output_file [args...]\n" "$(basename "${0}")" 1>&2 + printf "usage: %s [-c cue_cache_directory] [-d cue_debug] [-i instance_path] [-m module_file] [-p package_name] cue_tool cue_subcommand extra_args_file packageless_files_file output_file [args...]\n" "$(basename "${0}")" 1>&2 exit 2 } cue_cache_directory= +cue_debug= instance_path= module_file= package_name= function parse_args() { - while getopts c:i:m:p: name + while getopts c:d:i:m:p: name do case "${name}" in c) cue_cache_directory="${OPTARG}";; + d) cue_debug="${OPTARG}";; i) instance_path="${OPTARG}";; h) usage;; m) module_file="${OPTARG}";; @@ -103,6 +105,10 @@ elif [ -z "${HOME:-}" ]; then export CUE_CACHE_DIR="${cue_cache_directory}" fi +if [ -n "${cue_debug}" ]; then + export CUE_DEBUG="${cue_debug}" +fi + # NB: See https://stackoverflow.com/questions/7577052 for the odd # treatment of the "packageless_file_args" array variable here, # handling the case where the array winds up empty for lack of diff --git a/cue/cue.bzl b/cue/cue.bzl index f73c4a8..f93b47d 100644 --- a/cue/cue.bzl +++ b/cue/cue.bzl @@ -71,6 +71,13 @@ def _add_common_output_producing_attrs_to(attrs): "concatenate_objects": attr.bool( doc = "Concatenate multiple objects into a list.", ), + "cue_debug": attr.string( + doc = """Debug flags to pass to CUE via the CUE_DEBUG environment variable. + +The value is a comma-separated list of debug flags. See CUE documentation +for available debug flags.""", + default = "", + ), # Unfortunately, we can't use a private attribute for an # implicit dependency here, because we can't fix the default # label value. @@ -517,6 +524,8 @@ _cue_toolchain_type = "//tools/cue:toolchain_type" def _make_output_producing_action(ctx, cue_subcommand, mnemonic, description, augment_args = None, module_file = None, instance_directory_path = None, instance_package_name = None, cue_cache_directory_path = None): cue_tool = ctx.toolchains[_cue_toolchain_type].cueinfo.tool args = ctx.actions.args() + if ctx.attr.cue_debug: + args.add("-d", ctx.attr.cue_debug) if module_file: args.add("-m", _runfile_path(ctx, module_file)) if instance_directory_path: diff --git a/test/BUILD.bazel b/test/BUILD.bazel index ac0a49a..e1e1d3b 100644 --- a/test/BUILD.bazel +++ b/test/BUILD.bazel @@ -72,3 +72,13 @@ cue_test( name = "stamp", generated_output_file = "//test/testdata/stamp:stamp.json", ) + +cue_test( + name = "debug", + generated_output_file = "//test/testdata/debug:debug.json", +) + +cue_test( + name = "debug_unsorted", + generated_output_file = "//test/testdata/debug:debug_unsorted.json", +) diff --git a/test/testdata/debug/BUILD.bazel b/test/testdata/debug/BUILD.bazel new file mode 100644 index 0000000..fb43042 --- /dev/null +++ b/test/testdata/debug/BUILD.bazel @@ -0,0 +1,24 @@ +load( + "//cue:cue.bzl", + "cue_exported_standalone_files", +) + +exports_files([ + "debug-golden.json", + "debug_unsorted-golden.json", +]) + +# Without cue_debug - fields stay in original order +cue_exported_standalone_files( + name = "debug_unsorted", + srcs = ["debug.cue"], + visibility = ["//test:__subpackages__"], +) + +# With cue_debug = "sortfields" - fields are sorted alphabetically +cue_exported_standalone_files( + name = "debug", + srcs = ["debug.cue"], + cue_debug = "sortfields", + visibility = ["//test:__subpackages__"], +) diff --git a/test/testdata/debug/debug-golden.json b/test/testdata/debug/debug-golden.json new file mode 100644 index 0000000..8cd7669 --- /dev/null +++ b/test/testdata/debug/debug-golden.json @@ -0,0 +1,6 @@ +{ + "apple": "first fruit", + "banana": "second fruit", + "mango": "middle fruit", + "zebra": "last animal" +} diff --git a/test/testdata/debug/debug.cue b/test/testdata/debug/debug.cue new file mode 100644 index 0000000..bedeb5e --- /dev/null +++ b/test/testdata/debug/debug.cue @@ -0,0 +1,6 @@ +// Fields in non-alphabetical order to test sortfields debug flag +zebra: "last animal" +apple: "first fruit" +mango: "middle fruit" +banana: "second fruit" + diff --git a/test/testdata/debug/debug_unsorted-golden.json b/test/testdata/debug/debug_unsorted-golden.json new file mode 100644 index 0000000..1961289 --- /dev/null +++ b/test/testdata/debug/debug_unsorted-golden.json @@ -0,0 +1,6 @@ +{ + "zebra": "last animal", + "apple": "first fruit", + "mango": "middle fruit", + "banana": "second fruit" +}