From 94abc604bfb6a0af14dc5d3e297cb5b12959fdfc Mon Sep 17 00:00:00 2001 From: Mattias Wadman Date: Sun, 30 Jun 2024 16:30:04 +0200 Subject: [PATCH] Include filename in input parse error message Fixes #1553 --- src/main.c | 7 +++++-- tests/shtest | 32 ++++++++++++++++++++++++-------- 2 files changed, 29 insertions(+), 10 deletions(-) diff --git a/src/main.c b/src/main.c index 2f26ff2bfe..5ee1317602 100644 --- a/src/main.c +++ b/src/main.c @@ -731,14 +731,17 @@ int main(int argc, char* argv[]) { // Parse error jv msg = jv_invalid_get_msg(value); + jv input_pos = jq_util_input_get_position(jq); if (!(options & SEQ)) { ret = JQ_ERROR_UNKNOWN; - fprintf(stderr, "jq: parse error: %s\n", jv_string_value(msg)); + fprintf(stderr, "jq: parse error (at %s): %s\n", jv_string_value(input_pos), jv_string_value(msg)); + jv_free(input_pos); jv_free(msg); break; } // --seq -> errors are not fatal - fprintf(stderr, "jq: ignoring parse error: %s\n", jv_string_value(msg)); + fprintf(stderr, "jq: ignoring parse error (at %s): %s\n", jv_string_value(input_pos), jv_string_value(msg)); + jv_free(input_pos); jv_free(msg); } } diff --git a/tests/shtest b/tests/shtest index 03fbf665e5..9971ab2455 100755 --- a/tests/shtest +++ b/tests/shtest @@ -103,20 +103,36 @@ if [ $n -ne $nref ]; then exit 1 fi +## Test parse error message +echo 123 > $d/input_valid +echo 456 > $d/input_error_line2 +echo asd >> $d/input_error_line2 + +cat > $d/expected_out < $d/expected_err < $d/out 2> $d/err || true +cmp $d/out $d/expected_out +cmp $d/err $d/expected_err + ## Test JSON sequence support cat > $d/expected <:2): Truncated value at line 2, column 5 +jq: ignoring parse error (at :2): Truncated value at line 2, column 25 +jq: ignoring parse error (at :2): Truncated value at line 2, column 41 EOF printf '1\0362 3\n[0,1\036[4,5]true"ab"{"c":4\036{}{"d":5,"e":6"\036false\n'|$VALGRIND $Q $JQ -ces --seq '. == [2,3,[4,5],true,"ab",{},false]' > /dev/null 2> $d/out cmp $d/out $d/expected cat > $d/expected <:2): Truncated value at line 2, column 5 +jq: ignoring parse error (at :2): Truncated value at line 2, column 25 +jq: ignoring parse error (at :2): Truncated value at line 3, column 1 EOF printf '1\0362 3\n[0,1\036[4,5]true"ab"{"c":4\036{}{"d":5,"e":6"false\n\036null'|$VALGRIND $Q $JQ -ces --seq '. == [2,3,[4,5],true,"ab",{},null]' > /dev/null 2> $d/out cmp $d/out $d/expected @@ -124,7 +140,7 @@ cmp $d/out $d/expected # Note that here jq sees no inputs at all but it still succeeds because # --seq ignores parse errors cat > $d/expected <:0): Unfinished abandoned text at EOF at line 1, column 4 EOF printf '"foo' | $JQ -c --seq . > $d/out 2>&1 cmp $d/out $d/expected @@ -137,7 +153,7 @@ cmp $d/out $d/expected # Numeric values truncated by EOF are ignored cat > $d/expected <:0): Unfinished abandoned text at EOF at line 1, column 1 EOF printf '1' | $JQ -c --seq . > $d/out 2>&1 cmp $d/out $d/expected