Windows: Add gcc_env feature for msys gcc and mingw gcc CROSSTOOL

This set the PATH environment explicitly so that it's not affected by client PATH.

RELNOTES: None
PiperOrigin-RevId: 222048721
diff --git a/src/test/shell/bazel/bazel_windows_example_test.sh b/src/test/shell/bazel/bazel_windows_example_test.sh
index c28dcd1..a8ccb89 100755
--- a/src/test/shell/bazel/bazel_windows_example_test.sh
+++ b/src/test/shell/bazel/bazel_windows_example_test.sh
@@ -92,20 +92,25 @@
 
 function test_cpp_with_mingw_gcc() {
   local cpp_pkg=examples/cpp
-  ( # mingw gcc should be in PATH
+  ( # /mingw64/bin should be in PATH because during runtime the built binary
+    # depends on some dynamic libraries in this directory.
   export PATH="/mingw64/bin:$PATH"
   assert_build_output \
     ./bazel-bin/${cpp_pkg}/libhello-lib.a ${cpp_pkg}:hello-world \
-    --compiler=mingw-gcc
+    --compiler=mingw-gcc --experimental_strict_action_env
   assert_build_output \
     ./bazel-bin/${cpp_pkg}/libhello-lib.so ${cpp_pkg}:hello-lib\
-    --compiler=mingw-gcc --output_groups=dynamic_library
-  assert_build ${cpp_pkg}:hello-world --compiler=mingw-gcc
+    --compiler=mingw-gcc --output_groups=dynamic_library \
+    --experimental_strict_action_env
+  assert_build ${cpp_pkg}:hello-world --compiler=mingw-gcc \
+    --experimental_strict_action_env
   ./bazel-bin/${cpp_pkg}/hello-world foo >& $TEST_log \
     || fail "./bazel-bin/${cpp_pkg}/hello-world foo execution failed"
   expect_log "Hello foo"
-  assert_test_ok "//examples/cpp:hello-success_test" --compiler=mingw-gcc
-  assert_test_fails "//examples/cpp:hello-fail_test" --compiler=mingw-gcc )
+  assert_test_ok "//examples/cpp:hello-success_test" --compiler=mingw-gcc \
+    --experimental_strict_action_env --test_env=PATH
+  assert_test_fails "//examples/cpp:hello-fail_test" --compiler=mingw-gcc \
+    --experimental_strict_action_env --test_env=PATH)
 }
 
 function test_cpp_alwayslink() {
diff --git a/tools/cpp/windows_cc_configure.bzl b/tools/cpp/windows_cc_configure.bzl
index 25afccf..54a3a8c 100644
--- a/tools/cpp/windows_cc_configure.bzl
+++ b/tools/cpp/windows_cc_configure.bzl
@@ -41,11 +41,12 @@
         msys_root = tokens[0][:len(tokens[0]) - len("bin")]
     prefix = "mingw64" if use_mingw else "usr"
     tool_path_prefix = escape_string(msys_root) + prefix
+    tool_bin_path = tool_path_prefix + "/bin"
     tool_path = {}
 
     for tool in ["ar", "compat-ld", "cpp", "dwp", "gcc", "gcov", "ld", "nm", "objcopy", "objdump", "strip"]:
         if msys_root:
-            tool_path[tool] = tool_path_prefix + "/bin/" + tool
+            tool_path[tool] = tool_bin_path + "/" + tool
         else:
             tool_path[tool] = "msys_gcc_installation_error.bat"
 
@@ -77,8 +78,29 @@
             '   linker_flag: "-lstdc++"\n' +
             '   objcopy_embed_flag: "-I"\n' +
             '   objcopy_embed_flag: "binary"\n' +
-            '   feature { name: "targets_windows" implies: "copy_dynamic_libraries_to_binary" enabled: true }' +
-            '   feature { name: "copy_dynamic_libraries_to_binary" }')
+            '   feature { name: "targets_windows" implies: "copy_dynamic_libraries_to_binary" enabled: true }\n' +
+            '   feature { name: "copy_dynamic_libraries_to_binary" }\n' +
+            "   feature {\n" +
+            '    name: "gcc_env"\n' +
+            "    enabled: true\n" +
+            "    env_set {\n" +
+            '      action: "c-compile"\n' +
+            '      action: "c++-compile"\n' +
+            '      action: "c++-module-compile"\n' +
+            '      action: "c++-module-codegen"\n' +
+            '      action: "c++-header-parsing"\n' +
+            '      action: "assemble"\n' +
+            '      action: "preprocess-assemble"\n' +
+            '      action: "c++-link-executable"\n' +
+            '      action: "c++-link-dynamic-library"\n' +
+            '      action: "c++-link-nodeps-dynamic-library"\n' +
+            '      action: "c++-link-static-library"\n' +
+            "      env_entry {\n" +
+            '        key: "PATH"\n' +
+            '        value: "%s"\n' % tool_bin_path +
+            "      }\n" +
+            "    }\n" +
+            "  }")
 
 def _get_system_root(repository_ctx):
     """Get System root path on Windows, default is C:\\\Windows. Doesn't %-escape the result."""