From 654c0fd61270722157eba8846c991be2c856735a Mon Sep 17 00:00:00 2001 From: Dan Federman Date: Mon, 11 Dec 2023 07:54:42 -0800 Subject: [PATCH] Do not parse third-party code in Swift Package plugin --- .../SafeDIGenerateDependencyTree.swift | 24 ++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/Plugins/SafeDIGenerateDependencyTree/SafeDIGenerateDependencyTree.swift b/Plugins/SafeDIGenerateDependencyTree/SafeDIGenerateDependencyTree.swift index 40d5ca2b..2a05e429 100644 --- a/Plugins/SafeDIGenerateDependencyTree/SafeDIGenerateDependencyTree.swift +++ b/Plugins/SafeDIGenerateDependencyTree/SafeDIGenerateDependencyTree.swift @@ -44,12 +44,30 @@ struct SafeDIGenerateDependencyTree: BuildToolPlugin { extension Target { - var sourceModuleRecursiveDependencies: [SourceModuleTarget] { + var sourceModuleRecursiveDependencies: [SwiftSourceModuleTarget] { recursiveTargetDependencies.compactMap { - $0 as? SourceModuleTarget + // Since we only understand Swift files, we only care about SwiftSourceModuleTargets. + guard let swiftModule = $0 as? SwiftSourceModuleTarget else { + return nil + } + + // We only care about first-party code. Ignore third-party dependencies. + guard + swiftModule + .directory + // Removing the module name. + .removingLastComponent() + // Removing 'Sources'. + .removingLastComponent() + // Removing the package name. + .removingLastComponent() + .lastComponent != "checkouts" + else { + return nil + } + return swiftModule } } - } #if canImport(XcodeProjectPlugin)