Fix system_include_paths with textual_hdrs/hdrs and strip_include_prefix

Copybara Import from https://github.com/bazelbuild/rules_cc/pull/800

BEGIN_PUBLIC
Fix system_include_paths with textual_hdrs/hdrs and strip_include_prefix (#800)

From https://github.com/bazelbuild/rules_cc/pull/537

Closes #800
END_PUBLIC

COPYBARA_INTEGRATE_REVIEW=https://github.com/bazelbuild/rules_cc/pull/800 from keith:ks/fix-system_include_paths-with-textual_hdrs 75b34063d9122a4d744050df9432ce858fc9433a
PiperOrigin-RevId: 960374021
Change-Id: I793f9f75aa670ffc78121bd5b9ff9965b3dbb109
diff --git a/cc/private/compile/cc_compilation_helper.bzl b/cc/private/compile/cc_compilation_helper.bzl
index fa23603..9e3d205 100644
--- a/cc/private/compile/cc_compilation_helper.bzl
+++ b/cc/private/compile/cc_compilation_helper.bzl
@@ -496,6 +496,8 @@
     if public_headers.virtual_include_path:
         if external:
             external_include_dirs.append(public_headers.virtual_include_path)
+        elif feature_configuration.is_requested("system_include_paths"):
+            system_include_dirs_for_context.append(public_headers.virtual_include_path)
         else:
             include_dirs_for_context.append(public_headers.virtual_include_path)
 
@@ -514,8 +516,10 @@
         must_use_strip_prefix = False,
     )
     if textual_headers.virtual_include_path:
-        if external or feature_configuration.is_requested("system_include_paths"):
+        if external:
             external_include_dirs.append(textual_headers.virtual_include_path)
+        elif feature_configuration.is_requested("system_include_paths"):
+            system_include_dirs_for_context.append(textual_headers.virtual_include_path)
         else:
             include_dirs_for_context.append(textual_headers.virtual_include_path)
 
diff --git a/tests/cc/common/cc_common_test.bzl b/tests/cc/common/cc_common_test.bzl
index 21399a5..15af500 100644
--- a/tests/cc/common/cc_common_test.bzl
+++ b/tests/cc/common/cc_common_test.bzl
@@ -142,6 +142,20 @@
         )],
     )
 
+def _uses_system_virtual_includes_impl(env, target):
+    subject = cc_info_subject.from_target(env, target)
+    virtual_includes = matching.custom(
+        "contains '_virtual_includes'",
+        lambda s: "_virtual_includes" in s,
+    )
+
+    subject.compilation_context().system_include_dirs().contains_at_least_predicates([
+        virtual_includes,
+    ])
+    subject.compilation_context().external_include_dirs().contains_none_of([
+        virtual_includes,
+    ])
+
 def _test_strip_include_prefix_uses_virtual_includes_by_default(name):
     """Tests that strip_include_prefix uses _virtual_includes by default (skip_virtual_includes off)."""
     util.helper_target(
@@ -190,6 +204,40 @@
         test_features = ["skip_virtual_includes"],
     )
 
+def _test_strip_include_prefix_for_public_headers_uses_system_include_paths(name):
+    util.helper_target(
+        cc_library,
+        name = name + "/lib",
+        hdrs = ["v1/foo.h"],
+        strip_include_prefix = "v1",
+    )
+
+    cc_analysis_test(
+        name = name,
+        impl = _uses_system_virtual_includes_impl,
+        target = name + "/lib",
+        config_settings = {
+            "//command_line_option:features": ["system_include_paths"],
+        },
+    )
+
+def _test_strip_include_prefix_for_textual_headers_uses_system_include_paths(name):
+    util.helper_target(
+        cc_library,
+        name = name + "/lib",
+        strip_include_prefix = "v1",
+        textual_hdrs = ["v1/foo.h"],
+    )
+
+    cc_analysis_test(
+        name = name,
+        impl = _uses_system_virtual_includes_impl,
+        target = name + "/lib",
+        config_settings = {
+            "//command_line_option:features": ["system_include_paths"],
+        },
+    )
+
 def _test_strip_include_prefix_error_not_under_prefix(name):
     """Tests that headers not under strip_include_prefix produce an error."""
     util.helper_target(
@@ -687,6 +735,8 @@
             _test_strip_include_prefix_uses_virtual_includes_by_default,
             _test_strip_include_prefix_no_virtual_includes_when_enabled,
             _test_strip_include_prefix_with_include_prefix_uses_virtual_includes,
+            _test_strip_include_prefix_for_public_headers_uses_system_include_paths,
+            _test_strip_include_prefix_for_textual_headers_uses_system_include_paths,
             _test_strip_include_prefix_error_not_under_prefix,
         ])
     test_suite(