Skylark repos: for failed patch command, also report stdout

The patch(1) utility usually gives error messages on stdout. So it is
not useful to report only stderr in case a patch failed. Report both.

Fixes #5379.

Change-Id: Ief198849e29ca989dfdefe2fadf495a0b0949972
PiperOrigin-RevId: 200377306
diff --git a/src/test/shell/bazel/external_patching_test.sh b/src/test/shell/bazel/external_patching_test.sh
index b9d6fd7..ee3c938 100755
--- a/src/test/shell/bazel/external_patching_test.sh
+++ b/src/test/shell/bazel/external_patching_test.sh
@@ -110,6 +110,38 @@
   grep -q 'env' $foopath || fail "expected unpatched file"
 }
 
+test_patch_failed() {
+  EXTREPODIR=`pwd`
+
+  cat > my_patch_tool <<'EOF'
+#!/bin/sh
+
+echo Helpful message
+exit 1
+EOF
+  chmod u+x my_patch_tool
+
+  # Test that the patches attribute of http_archive is honored
+  mkdir main
+  cd main
+  echo "ignored anyway" > patch_foo.sh
+  cat > WORKSPACE <<EOF
+load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
+http_archive(
+  name="ext",
+  strip_prefix="ext-0.1.2",
+  urls=["file://${EXTREPODIR}/ext.zip"],
+  build_file_content="exports_files([\"foo.sh\"])",
+  patches = ["//:patch_foo.sh"],
+  patch_tool = "${EXTREPODIR}/my_patch_tool",
+)
+EOF
+  touch BUILD
+
+  bazel build @ext//... >"${TEST_log}" 2>&1 && fail "expected failure" || :
+  expect_log 'Helpful message'
+}
+
 test_patch_git() {
   EXTREPODIR=`pwd`
   export GIT_CONFIG_NOSYSTEM=YES
diff --git a/tools/build_defs/repo/utils.bzl b/tools/build_defs/repo/utils.bzl
index 4839b3e..e23556c 100644
--- a/tools/build_defs/repo/utils.bzl
+++ b/tools/build_defs/repo/utils.bzl
@@ -76,7 +76,8 @@
         )
         st = ctx.execute([bash_exe, "-c", command])
         if st.return_code:
-            fail("Error applying patch %s:\n%s" % (str(patchfile), st.stderr))
+            fail("Error applying patch %s:\n%s%s" %
+                 (str(patchfile), st.stderr, st.stdout))
     for cmd in ctx.attr.patch_cmds:
         st = ctx.execute([bash_exe, "-c", cmd])
         if st.return_code: