@@ -473,21 +473,24 @@ extension Driver {
473473 commandLine. appendFlag ( $0)
474474 }
475475
476+ let toolchainStdlibPath = VirtualPath . lookup ( frontendTargetInfo. runtimeResourcePath. path)
477+ . appending ( components: frontendTargetInfo. target. triple. platformName ( ) ?? " " , " Swift.swiftmodule " )
478+ let hasToolchainStdlib = try fileSystem. exists ( toolchainStdlibPath)
479+
480+ // If the resource directory has the standard library, prefer the toolchain's plugins
481+ // to the platform SDK plugins.
482+ if hasToolchainStdlib {
483+ try addPluginPathArguments ( commandLine: & commandLine)
484+ }
485+
476486 try toolchain. addPlatformSpecificCommonFrontendOptions ( commandLine: & commandLine,
477487 inputs: & inputs,
478488 frontendTargetInfo: frontendTargetInfo,
479489 driver: & self )
480490
481- // Platform-agnostic options that need to happen after platform-specific ones.
482- if isFrontendArgSupported ( . pluginPath) {
483- // Default paths for compiler plugins found within the toolchain
484- // (loaded as shared libraries).
485- let pluginPathRoot = VirtualPath . absolute ( try toolchain. executableDir. parentDirectory)
486- commandLine. appendFlag ( . pluginPath)
487- commandLine. appendPath ( pluginPathRoot. pluginPath)
488-
489- commandLine. appendFlag ( . pluginPath)
490- commandLine. appendPath ( pluginPathRoot. localPluginPath)
491+ // Otherwise, prefer the platform's plugins.
492+ if !hasToolchainStdlib {
493+ try addPluginPathArguments ( commandLine: & commandLine)
491494 }
492495 }
493496
@@ -774,6 +777,22 @@ extension Driver {
774777 try explicitDependencyBuildPlanner? . resolveBridgingHeaderDependencies ( inputs: & inputs, commandLine: & commandLine)
775778 }
776779
780+ mutating func addPluginPathArguments( commandLine: inout [ Job . ArgTemplate ] ) throws {
781+ guard isFrontendArgSupported ( . pluginPath) else {
782+ return
783+ }
784+
785+ // Default paths for compiler plugins found within the toolchain
786+ // (loaded as shared libraries).
787+ let pluginPathRoot = VirtualPath . absolute ( try toolchain. executableDir. parentDirectory)
788+ commandLine. appendFlag ( . pluginPath)
789+ commandLine. appendPath ( pluginPathRoot. pluginPath)
790+
791+ commandLine. appendFlag ( . pluginPath)
792+ commandLine. appendPath ( pluginPathRoot. localPluginPath)
793+ }
794+
795+
777796 /// If explicit dependency planner supports creating bridging header pch command.
778797 public func supportsBridgingHeaderPCHCommand( ) throws -> Bool {
779798 return try explicitDependencyBuildPlanner? . supportsBridgingHeaderPCHCommand ( ) ?? false
0 commit comments