Repository: jqlang/jq

Problem statement:
halt_error prints "jq: error:" in master
<!--
READ THIS FIRST!

If you have a usage question, please ask us on either Stack Overflow (https://stackoverflow.com/questions/tagged/jq) or in the #jq channel (http://irc.lc/freenode/%23jq/) on Freenode (https://webchat.freenode.net/).

-->

**Describe the bug**

In MSYS2 nightly builds `halt_error` prints "jq: error:" before any output. Can "jq: error:" be removed from output? The 1.6 release build from the official MSYS2 package repo doesn't print "jq: error:", and niether do the Linux builds.

**To Reproduce**

1. Download a Windows nightly.
2. `jq -n '"test" | halt_error(1)'`

```sh
$ nightly/win64/jq --version
jq-1.6-94-gaaf200f
$ nightly/win64/jq.exe -n '"test" | halt_error(1)'   # wrong: extra "jq: error: "
jq: error: test                                 
```

**Expected behavior**

```sh
$ /mingw64/nightly/jq --version
jq-1.6
$ /mingw64/bin/jq -n '"test" | halt_error(1)'    # expected output: next line
test                                            
```

**Environment (please complete the following information):**

 - Windows 10 Enterprise x64
 - jq-1.6-94-gaaf200f 2019-05-12


halt_error prints "jq: error:" in master
<!--
READ THIS FIRST!

If you have a usage question, please ask us on either Stack Overflow (https://stackoverflow.com/questions/tagged/jq) or in the #jq channel (http://irc.lc/freenode/%23jq/) on Freenode (https://webchat.freenode.net/).

-->

**Describe the bug**

In MSYS2 nightly builds `halt_error` prints "jq: error:" before any output. Can "jq: error:" be removed from output? The 1.6 release build from the official MSYS2 package repo doesn't print "jq: error:", and niether do the Linux builds.

**To Reproduce**

1. Download a Windows nightly.
2. `jq -n '"test" | halt_error(1)'`

```sh
$ nightly/win64/jq --version
jq-1.6-94-gaaf200f
$ nightly/win64/jq.exe -n '"test" | halt_error(1)'   # wrong: extra "jq: error: "
jq: error: test                                 
```

**Expected behavior**

```sh
$ /mingw64/nightly/jq --version
jq-1.6
$ /mingw64/bin/jq -n '"test" | halt_error(1)'    # expected output: next line
test                                            
```

**Environment (please complete the following information):**

 - Windows 10 Enterprise x64
 - jq-1.6-94-gaaf200f 2019-05-12




Hints:
Hmm, ok.  This was the result of a fix for a number of cases where errors were not identified as coming from jq, but I think you're right and `halt_error`'s message should not be prefixed with `jq: error: `.
FYI, MSYS has nothing to do with it.
Hmm, ok.  This was the result of a fix for a number of cases where errors were not identified as coming from jq, but I think you're right and `halt_error`'s message should not be prefixed with `jq: error: `.
FYI, MSYS has nothing to do with it.

Patch:
diff --git a/src/main.c b/src/main.c
index 2d202bd6a1..1cc6542558 100644
--- a/src/main.c
+++ b/src/main.c
@@ -216,12 +216,14 @@ static int process(jq_state *jq, jv value, int flags, int dumpopts) {
     jv_free(exit_code);
     jv error_message = jq_get_error_message(jq);
     if (jv_get_kind(error_message) == JV_KIND_STRING) {
-      fprintf(stderr, "jq: error: %s", jv_string_value(error_message));
+      // No prefix should be added to the output of `halt_error`.
+      fwrite(jv_string_value(error_message), 1,
+          jv_string_length_bytes(jv_copy(error_message)), stderr);
     } else if (jv_get_kind(error_message) == JV_KIND_NULL) {
       // Halt with no output
     } else if (jv_is_valid(error_message)) {
       error_message = jv_dump_string(jv_copy(error_message), 0);
-      fprintf(stderr, "jq: error: %s\n", jv_string_value(error_message));
+      fprintf(stderr, "%s\n", jv_string_value(error_message));
     } // else no message on stderr; use --debug-trace to see a message
     fflush(stderr);
     jv_free(error_message);


Test patch:
diff --git a/tests/shtest b/tests/shtest
index 2606e1760e..4a4f855a64 100755
--- a/tests/shtest
+++ b/tests/shtest
@@ -275,11 +275,19 @@ if [ -n "$($VALGRIND $Q $JQ -n 'halt_error(1)' 2>&1)" ]; then
     echo "jq halt_error(1) had unexpected output" 1>&2
     exit 1
 fi
-if [ -n "$($VALGRIND $Q $JQ -n '"xyz\n"|halt_error(1)' 2>/dev/null)" ]; then
+if [ -n "$($VALGRIND $Q $JQ -n '"xyz\n" | halt_error(1)' 2>/dev/null)" ]; then
     echo "jq halt_error(1) had unexpected output on stdout" 1>&2
     exit 1
 fi
-if [ "$($VALGRIND $Q $JQ -n '"xyz\n"|halt_error(1)' 2>&1)" != "jq: error: xyz" ]; then
+if [ "$($VALGRIND $Q $JQ -n '"xy" | halt_error(1)' 2>&1 || echo "z")" != "xyz" ]; then
+    echo "jq halt_error(1) had unexpected output" 1>&2
+    exit 1
+fi
+if [ "$($VALGRIND $Q $JQ -n '"x\u0000y\u0000z" | halt_error(1)' 2>&1 | tr '\0' '.')" != "x.y.z" ]; then
+    echo "jq halt_error(1) had unexpected output" 1>&2
+    exit 1
+fi
+if [ "$($VALGRIND $Q $JQ -n '{"a":"xyz"} | halt_error(1)' 2>&1)" != '{"a":"xyz"}' ]; then
     echo "jq halt_error(1) had unexpected output" 1>&2
     exit 1
 fi