Skip to content

Add diagnostics for unsupported plugin dependency #5831

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions Sources/PackageGraph/PackageGraph+Loading.swift
Original file line number Diff line number Diff line change
Expand Up @@ -792,8 +792,10 @@ private final class ResolvedTargetBuilder: ResolvedBuilder<ResolvedTarget> {
let dependencies = try self.dependencies.map { dependency -> ResolvedTarget.Dependency in
switch dependency {
case .target(let targetBuilder, let conditions):
try self.target.validateDependency(target: targetBuilder.target)
return .target(try targetBuilder.construct(), conditions: conditions)
case .product(let productBuilder, let conditions):
try self.target.validateDependency(product: productBuilder.product, productPackage: productBuilder.packageBuilder.package.identity)
let product = try productBuilder.construct()
if !productBuilder.packageBuilder.isAllowedToVendUnsafeProducts {
try self.diagnoseInvalidUseOfUnsafeFlags(product)
Expand All @@ -811,6 +813,19 @@ private final class ResolvedTargetBuilder: ResolvedBuilder<ResolvedTarget> {
}
}

extension Target {

func validateDependency(target: Target) throws {
if self.type == .plugin && target.type == .library {
throw PackageGraphError.unsupportedPluginDependency(targetName: self.name, dependencyName: target.name, dependencyType: target.type.rawValue, dependencyPackage: nil)
}
}
func validateDependency(product: Product, productPackage: PackageIdentity) throws {
if self.type == .plugin && product.type.isLibrary {
throw PackageGraphError.unsupportedPluginDependency(targetName: self.name, dependencyName: product.name, dependencyType: product.type.description, dependencyPackage: productPackage.description)
}
}
}
/// Builder for resolved package.
private final class ResolvedPackageBuilder: ResolvedBuilder<ResolvedPackage> {

Expand Down
9 changes: 8 additions & 1 deletion Sources/PackageGraph/PackageGraph.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ enum PackageGraphError: Swift.Error {
targetName: String,
packageIdentifier: String
)

/// Dependency between a plugin and a dependent target/product of a given type is unsupported
case unsupportedPluginDependency(targetName: String, dependencyName: String, dependencyType: String, dependencyPackage: String?)
/// A product was found in multiple packages.
case duplicateProduct(product: String, packages: [String])

Expand Down Expand Up @@ -254,6 +255,12 @@ extension PackageGraphError: CustomStringConvertible {
return "multiple aliases: ['\(aliases.joined(separator: "', '"))'] found for target '\(target)' in product '\(product)' from package '\(package)'"
case .invalidSourcesForModuleAliasing(let target, let product, let package):
return "module aliasing can only be used for Swift based targets; non-Swift sources found in target '\(target)' for product '\(product)' from package '\(package)'"
case .unsupportedPluginDependency(let targetName, let dependencyName, let dependencyType, let dependencyPackage):
var trailingMsg = ""
if let depPkg = dependencyPackage {
trailingMsg = " from package '\(depPkg)'"
}
return "plugin '\(targetName)' cannot depend on '\(dependencyName)' of type '\(dependencyType)'\(trailingMsg); this dependency is unsupported"
}
}
}
180 changes: 178 additions & 2 deletions Tests/SPMBuildCoreTests/PluginInvocationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
//===----------------------------------------------------------------------===//

import Basics
import PackageGraph
@testable import PackageGraph
import PackageLoading
import PackageModel
@testable import SPMBuildCore
Expand Down Expand Up @@ -584,6 +584,183 @@ class PluginInvocationTests: XCTestCase {
}
}

func testUnsupportedDependencyProduct() throws {
// Only run the test if the environment in which we're running actually supports Swift concurrency (which the plugin APIs require).
try XCTSkipIf(!UserToolchain.default.supportsSwiftConcurrency(), "skipping because test environment doesn't support concurrency")

try testWithTemporaryDirectory { tmpPath in
// Create a sample package with a library product and a plugin.
let packageDir = tmpPath.appending(components: "MyPackage")
try localFileSystem.createDirectory(packageDir, recursive: true)
try localFileSystem.writeFileContents(packageDir.appending(component: "Package.swift"), string: """
// swift-tools-version: 5.7
import PackageDescription
let package = Package(
name: "MyPackage",
dependencies: [
.package(path: "../FooPackage"),
],
targets: [
.plugin(
name: "MyPlugin",
capability: .buildTool(),
dependencies: [
.product(name: "FooLib", package: "FooPackage"),
]
),
]
)
""")

let myPluginTargetDir = packageDir.appending(components: "Plugins", "MyPlugin")
try localFileSystem.createDirectory(myPluginTargetDir, recursive: true)
try localFileSystem.writeFileContents(myPluginTargetDir.appending(component: "plugin.swift"), string: """
import PackagePlugin
import Foo
@main struct MyBuildToolPlugin: BuildToolPlugin {
func createBuildCommands(
context: PluginContext,
target: Target
) throws -> [Command] { }
}
""")

let fooPkgDir = tmpPath.appending(components: "FooPackage")
try localFileSystem.createDirectory(fooPkgDir, recursive: true)
try localFileSystem.writeFileContents(fooPkgDir.appending(component: "Package.swift"), string: """
// swift-tools-version: 5.7
import PackageDescription
let package = Package(
name: "FooPackage",
products: [
.library(name: "FooLib",
targets: ["Foo"]),
],
targets: [
.target(
name: "Foo",
dependencies: []
),
]
)
""")
let fooTargetDir = fooPkgDir.appending(components: "Sources", "Foo")
try localFileSystem.createDirectory(fooTargetDir, recursive: true)
try localFileSystem.writeFileContents(fooTargetDir.appending(component: "file.swift"), string: """
public func foo() { }
""")

// Load a workspace from the package.
let observability = ObservabilitySystem.makeForTesting()
let workspace = try Workspace(
fileSystem: localFileSystem,
forRootPackage: packageDir,
customManifestLoader: ManifestLoader(toolchain: UserToolchain.default),
delegate: MockWorkspaceDelegate()
)

// Load the root manifest.
let rootInput = PackageGraphRootInput(packages: [packageDir], dependencies: [])
let rootManifests = try tsc_await {
workspace.loadRootManifests(
packages: rootInput.packages,
observabilityScope: observability.topScope,
completion: $0
)
}
XCTAssert(rootManifests.count == 1, "\(rootManifests)")

// Load the package graph.
XCTAssertThrowsError(try workspace.loadPackageGraph(rootInput: rootInput, observabilityScope: observability.topScope)) { error in
var diagnosed = false
if let realError = error as? PackageGraphError,
realError.description == "plugin 'MyPlugin' cannot depend on 'FooLib' of type 'library' from package 'foopackage'; this dependency is unsupported" {
diagnosed = true
}
XCTAssertTrue(diagnosed)
}
}
}

func testUnsupportedDependencyTarget() throws {
// Only run the test if the environment in which we're running actually supports Swift concurrency (which the plugin APIs require).
try XCTSkipIf(!UserToolchain.default.supportsSwiftConcurrency(), "skipping because test environment doesn't support concurrency")

try testWithTemporaryDirectory { tmpPath in
// Create a sample package with a library target and a plugin.
let packageDir = tmpPath.appending(components: "MyPackage")
try localFileSystem.createDirectory(packageDir, recursive: true)
try localFileSystem.writeFileContents(packageDir.appending(component: "Package.swift"), string: """
// swift-tools-version: 5.7
import PackageDescription
let package = Package(
name: "MyPackage",
targets: [
.target(
name: "MyLibrary",
dependencies: []
),
.plugin(
name: "MyPlugin",
capability: .buildTool(),
dependencies: [
"MyLibrary"
]
),
]
)
""")

let myLibraryTargetDir = packageDir.appending(components: "Sources", "MyLibrary")
try localFileSystem.createDirectory(myLibraryTargetDir, recursive: true)
try localFileSystem.writeFileContents(myLibraryTargetDir.appending(component: "library.swift"), string: """
public func hello() { }
""")
let myPluginTargetDir = packageDir.appending(components: "Plugins", "MyPlugin")
try localFileSystem.createDirectory(myPluginTargetDir, recursive: true)
try localFileSystem.writeFileContents(myPluginTargetDir.appending(component: "plugin.swift"), string: """
import PackagePlugin
import MyLibrary
@main struct MyBuildToolPlugin: BuildToolPlugin {
func createBuildCommands(
context: PluginContext,
target: Target
) throws -> [Command] { }
}
""")

// Load a workspace from the package.
let observability = ObservabilitySystem.makeForTesting()
let workspace = try Workspace(
fileSystem: localFileSystem,
forRootPackage: packageDir,
customManifestLoader: ManifestLoader(toolchain: UserToolchain.default),
delegate: MockWorkspaceDelegate()
)

// Load the root manifest.
let rootInput = PackageGraphRootInput(packages: [packageDir], dependencies: [])
let rootManifests = try tsc_await {
workspace.loadRootManifests(
packages: rootInput.packages,
observabilityScope: observability.topScope,
completion: $0
)
}
XCTAssert(rootManifests.count == 1, "\(rootManifests)")

// Load the package graph.
XCTAssertThrowsError(try workspace.loadPackageGraph(rootInput: rootInput, observabilityScope: observability.topScope)) { error in
var diagnosed = false
if let realError = error as? PackageGraphError,
realError.description == "plugin 'MyPlugin' cannot depend on 'MyLibrary' of type 'library'; this dependency is unsupported" {
diagnosed = true
}
XCTAssertTrue(diagnosed)
}
}
}

func testPrebuildPluginShouldNotUseExecTarget() throws {
try testWithTemporaryDirectory { tmpPath in
// Create a sample package with a library target and a plugin.
Expand Down Expand Up @@ -656,7 +833,6 @@ class PluginInvocationTests: XCTestCase {
)
]
}

}
""")

Expand Down