Fix Python path handling across Windows drives

Check whether the rebased interpreter path is absolute before marking
it as relative, avoiding an incorrect build directory prefix.

Add tests for build directories outside the source tree on the same
drive and on a different drive.

Bug: 587455002
Change-Id: Id85bda13f59b86070a9e4396bc35a31102394e63
Reviewed-on: https://gn-review.googlesource.com/c/gn/+/29180
Reviewed-by: Andrew Grieve <agrieve@google.com>
Reviewed-by: Takuto Ikuta <tikuta@google.com>
Commit-Queue: Takuto Ikuta <tikuta@google.com>
diff --git a/build/gen.py b/build/gen.py
index ce0f157..30ebff8 100755
--- a/build/gen.py
+++ b/build/gen.py
@@ -910,6 +910,7 @@
         'src/gn/action_target_generator_unittest.cc',
         'src/gn/analyzer_unittest.cc',
         'src/gn/args_unittest.cc',
+        'src/gn/build_settings_unittest.cc',
         'src/gn/builder_record_map_unittest.cc',
         'src/gn/builder_unittest.cc',
         'src/gn/binary_target_generator_unittest.cc',
diff --git a/src/gn/build_settings.cc b/src/gn/build_settings.cc
index 7a5d19d..9869ea3 100644
--- a/src/gn/build_settings.cc
+++ b/src/gn/build_settings.cc
@@ -56,7 +56,9 @@
   if (python_path_.IsAbsolute() && root_path_.IsParent(python_path_)) {
     python_path_ = UTF8ToFilePath(
         RebasePath(FilePathToUTF8(python_path_), build_dir_, root_path_utf8_));
-    python_path_is_relative_to_build_dir_ = true;
+    // Rebasing across Windows drives leaves the path absolute. Do not prepend
+    // the build directory when invoking the interpreter in that case.
+    python_path_is_relative_to_build_dir_ = !python_path_.IsAbsolute();
   }
 }
 
diff --git a/src/gn/build_settings_unittest.cc b/src/gn/build_settings_unittest.cc
new file mode 100644
index 0000000..9985725
--- /dev/null
+++ b/src/gn/build_settings_unittest.cc
@@ -0,0 +1,41 @@
+// Copyright 2026 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "gn/build_settings.h"
+
+#include "base/files/file_path.h"
+#include "gn/source_dir.h"
+#include "util/build_config.h"
+#include "util/test/test.h"
+
+TEST(BuildSettingsTest, PythonPathWithBuildDirOutsideSourceRoot) {
+  BuildSettings settings;
+#if defined(OS_WIN)
+  settings.SetRootPath(base::FilePath(FILE_PATH_LITERAL("C:/src")));
+  settings.SetBuildDir(SourceDir("/C:/out/"));
+#else
+  settings.SetRootPath(base::FilePath(FILE_PATH_LITERAL("/src")));
+  settings.SetBuildDir(SourceDir("/out/"));
+#endif
+  settings.SetPythonPath(settings.root_path()
+                             .Append(FILE_PATH_LITERAL("bin/python3"))
+                             .NormalizePathSeparatorsTo('/'));
+
+  EXPECT_EQ(base::FilePath(FILE_PATH_LITERAL("../src/bin/python3")),
+            settings.python_path());
+  EXPECT_TRUE(settings.python_path_is_relative_to_build_dir());
+}
+
+#if defined(OS_WIN)
+TEST(BuildSettingsTest, PythonPathWithBuildDirOnDifferentDrive) {
+  BuildSettings settings;
+  settings.SetRootPath(base::FilePath(FILE_PATH_LITERAL("D:/src")));
+  settings.SetBuildDir(SourceDir("/C:/out/"));
+  const base::FilePath python_path(FILE_PATH_LITERAL("D:/src/bin/python3.exe"));
+  settings.SetPythonPath(python_path);
+
+  EXPECT_EQ(python_path, settings.python_path());
+  EXPECT_FALSE(settings.python_path_is_relative_to_build_dir());
+}
+#endif  // defined(OS_WIN)