Use getPackageIdentifier to tell if a DLL should be copied or not.

Previously, we were using package name, that is incorrect when the DLL and the binary have the same package name but come from different repositories.

Fixes https://github.com/bazelbuild/bazel/issues/8820

RELNOTES: None
PiperOrigin-RevId: 258348380
diff --git a/src/main/java/com/google/devtools/build/lib/rules/cpp/CcBinary.java b/src/main/java/com/google/devtools/build/lib/rules/cpp/CcBinary.java
index ee2b510..af2f72f 100644
--- a/src/main/java/com/google/devtools/build/lib/rules/cpp/CcBinary.java
+++ b/src/main/java/com/google/devtools/build/lib/rules/cpp/CcBinary.java
@@ -1002,7 +1002,10 @@
       RuleContext ruleContext, Iterable<Artifact> dynamicLibrariesForRuntime) {
     NestedSetBuilder<Artifact> result = NestedSetBuilder.stableOrder();
     for (Artifact target : dynamicLibrariesForRuntime) {
-      if (!ruleContext.getLabel().getPackageName().equals(target.getOwner().getPackageName())) {
+      if (!ruleContext
+          .getLabel()
+          .getPackageIdentifier()
+          .equals(target.getOwner().getPackageIdentifier())) {
         // SymlinkAction on file is actually copy on Windows.
         Artifact copy = ruleContext.getBinArtifact(target.getFilename());
         ruleContext.registerAction(SymlinkAction.toArtifact(
diff --git a/src/test/py/bazel/bazel_windows_cpp_test.py b/src/test/py/bazel/bazel_windows_cpp_test.py
index 3ca5be0..274779f 100644
--- a/src/test/py/bazel/bazel_windows_cpp_test.py
+++ b/src/test/py/bazel/bazel_windows_cpp_test.py
@@ -286,6 +286,60 @@
         'ERROR: file \'main/A.dll\' is generated by these conflicting '
         'actions:', ''.join(stderr))
 
+  def testDLLIsCopiedFromExternalRepo(self):
+    self.ScratchFile('ext_repo/WORKSPACE')
+    self.ScratchFile('ext_repo/BUILD', [
+        'cc_library(',
+        '  name = "A",',
+        '  srcs = ["a.cc"],',
+        '  features = ["windows_export_all_symbols"],',
+        '  visibility = ["//visibility:public"],',
+        ')',
+    ])
+    self.ScratchFile('ext_repo/a.cc', [
+        '#include <stdio.h>',
+        'void hello_A() {',
+        '  printf("Hello A\\n");',
+        '}',
+    ])
+    self.ScratchFile('WORKSPACE', [
+        'local_repository(',
+        '  name = "ext_repo",',
+        '  path = "ext_repo",',
+        ')',
+    ])
+    self.ScratchFile('BUILD', [
+        'cc_binary(',
+        '  name = "main",',
+        '  srcs = ["main.cc"],',
+        '  deps = ["@ext_repo//:A"],',
+        '  linkstatic = 0,',
+        ')',
+    ])
+    self.ScratchFile('main.cc', [
+        'extern void hello_A();',
+        '',
+        'int main() {',
+        '  hello_A();',
+        '  return 0;',
+        '}',
+    ])
+
+    bazel_bin = self.getBazelInfo('bazel-bin')
+
+    exit_code, _, stderr = self.RunBazel(['build', '//:main'])
+    self.AssertExitCode(exit_code, 0, stderr)
+
+    # Test if A.dll is copied to the directory of main.exe
+    main_bin = os.path.join(bazel_bin, 'main.exe')
+    self.assertTrue(os.path.exists(main_bin))
+    self.assertTrue(os.path.exists(os.path.join(bazel_bin, 'A.dll')))
+
+    # Run the binary to see if it runs successfully
+    exit_code, stdout, stderr = self.RunProgram([main_bin])
+    self.AssertExitCode(exit_code, 0, stderr)
+    self.assertEqual(['Hello A'], stdout)
+
   def testDynamicLinkingMSVCRT(self):
     self.createProjectFiles()
     bazel_output = self.getBazelInfo('output_path')