Skip to content

dont emit resources or excludes warnings for dependencies #3857

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
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
17 changes: 14 additions & 3 deletions Sources/PackageLoading/TargetSourcesBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public struct TargetSourcesBuilder {
self.declaredSources = declaredSources?.spm_uniqueElements()

self.excludedPaths.forEach { exclude in
if let message = validTargetPath(at: exclude) {
if let message = validTargetPath(at: exclude), self.packageKind.emitAuthorWarnings {
let warning = "Invalid Exclude '\(exclude)': \(message)."
self.observabilityScope.emit(warning: warning)
}
Expand Down Expand Up @@ -168,7 +168,7 @@ public struct TargetSourcesBuilder {
var others: [AbsolutePath] = []
if toolsVersion >= .v5_3 {
let filesWithNoRules = pathToRule.filter { $0.value.rule == .none }
if !filesWithNoRules.isEmpty {
if !filesWithNoRules.isEmpty, self.packageKind.emitAuthorWarnings {
var warning = "found \(filesWithNoRules.count) file(s) which are unhandled; explicitly declare them as resources or exclude from the target\n"
for (file, _) in filesWithNoRules {
warning += " " + file.pathString + "\n"
Expand Down Expand Up @@ -369,7 +369,7 @@ public struct TargetSourcesBuilder {
private func diagnoseInvalidResource(in resources: [TargetDescription.Resource]) {
resources.forEach { resource in
let resourcePath = self.targetPath.appending(RelativePath(resource.path))
if let message = validTargetPath(at: resourcePath) {
if let message = validTargetPath(at: resourcePath), self.packageKind.emitAuthorWarnings {
let warning = "Invalid Resource '\(resource.path)': \(message)."
self.observabilityScope.emit(warning: warning)
}
Expand Down Expand Up @@ -708,3 +708,14 @@ extension ObservabilityMetadata {
typealias Value = String
}
}

fileprivate extension PackageReference.Kind {
var emitAuthorWarnings: Bool {
switch self {
case .remoteSourceControl, .registry:
return false
default:
return true
}
}
}
196 changes: 154 additions & 42 deletions Tests/PackageLoadingTests/TargetSourcesBuilderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
*/

import Basics
import Foundation
import PackageModel
import PackageLoading
import SPMTestSupport
Expand Down Expand Up @@ -655,31 +656,56 @@ class TargetSourcesBuilderTests: XCTestCase {
"/Bar.swift"
])

let observability = ObservabilitySystem.makeForTesting()

let builder = TargetSourcesBuilder(
packageIdentity: .plain("test"),
packageKind: .root(.init("/test")),
packagePath: .init("/test"),
target: target,
path: .root,
defaultLocalization: nil,
toolsVersion: .v5,
fileSystem: fs,
observabilityScope: observability.topScope
)
do {
let observability = ObservabilitySystem.makeForTesting()

let builder = TargetSourcesBuilder(
packageIdentity: .plain("test"),
packageKind: .root(.init("/test")),
packagePath: .init("/test"),
target: target,
path: .root,
defaultLocalization: nil,
toolsVersion: .v5,
fileSystem: fs,
observabilityScope: observability.topScope
)
_ = try builder.run()

testDiagnostics(observability.diagnostics) { result in
var diagnosticsFound = [Basics.Diagnostic?]()
diagnosticsFound.append(result.checkUnordered(diagnostic: "Invalid Exclude '/fileOutsideRoot.py': File not found.", severity: .warning))
diagnosticsFound.append(result.checkUnordered(diagnostic: "Invalid Exclude '/fakeDir': File not found.", severity: .warning))
testDiagnostics(observability.diagnostics) { result in
var diagnosticsFound = [Basics.Diagnostic?]()
diagnosticsFound.append(result.checkUnordered(diagnostic: "Invalid Exclude '/fileOutsideRoot.py': File not found.", severity: .warning))
diagnosticsFound.append(result.checkUnordered(diagnostic: "Invalid Exclude '/fakeDir': File not found.", severity: .warning))

for diagnostic in diagnosticsFound {
XCTAssertEqual(diagnostic?.metadata?.packageIdentity, builder.packageIdentity)
XCTAssertEqual(diagnostic?.metadata?.packageKind, builder.packageKind)
XCTAssertEqual(diagnostic?.metadata?.targetName, target.name)
for diagnostic in diagnosticsFound {
XCTAssertEqual(diagnostic?.metadata?.packageIdentity, builder.packageIdentity)
XCTAssertEqual(diagnostic?.metadata?.packageKind, builder.packageKind)
XCTAssertEqual(diagnostic?.metadata?.targetName, target.name)
}
}
}

// should not emit for "remote" packages

do {
let observability = ObservabilitySystem.makeForTesting()

let builder = TargetSourcesBuilder(
packageIdentity: .plain("test"),
packageKind: .remoteSourceControl(URL(string: "https://some.where/foo/bar")!),
packagePath: .init("/test"),
target: target,
path: .root,
defaultLocalization: nil,
toolsVersion: .v5,
fileSystem: fs,
observabilityScope: observability.topScope
)
_ = try builder.run()

XCTAssertNoDiagnostics(observability.diagnostics)
}
}

func testMissingResource() throws {
Expand All @@ -700,33 +726,57 @@ class TargetSourcesBuilderTests: XCTestCase {
"/Bar.swift"
])

let observability = ObservabilitySystem.makeForTesting()

let builder = TargetSourcesBuilder(
packageIdentity: .plain("test"),
packageKind: .root(.root),
packagePath: .root,
target: target,
path: .root,
defaultLocalization: nil,
toolsVersion: .v5,
fileSystem: fs,
observabilityScope: observability.topScope
)
_ = try builder.run()
do {
let observability = ObservabilitySystem.makeForTesting()

let builder = TargetSourcesBuilder(
packageIdentity: .plain("test"),
packageKind: .root(.root),
packagePath: .root,
target: target,
path: .root,
defaultLocalization: nil,
toolsVersion: .v5,
fileSystem: fs,
observabilityScope: observability.topScope
)
_ = try builder.run()

testDiagnostics(observability.diagnostics) { result in
var diagnosticsFound = [Basics.Diagnostic?]()
diagnosticsFound.append(result.checkUnordered(diagnostic: "Invalid Resource '../../../Fake.txt': File not found.", severity: .warning))
diagnosticsFound.append(result.checkUnordered(diagnostic: "Invalid Resource 'NotReal': File not found.", severity: .warning))
testDiagnostics(observability.diagnostics) { result in
var diagnosticsFound = [Basics.Diagnostic?]()
diagnosticsFound.append(result.checkUnordered(diagnostic: "Invalid Resource '../../../Fake.txt': File not found.", severity: .warning))
diagnosticsFound.append(result.checkUnordered(diagnostic: "Invalid Resource 'NotReal': File not found.", severity: .warning))

for diagnostic in diagnosticsFound {
XCTAssertEqual(diagnostic?.metadata?.packageIdentity, builder.packageIdentity)
XCTAssertEqual(diagnostic?.metadata?.packageKind, builder.packageKind)
XCTAssertEqual(diagnostic?.metadata?.targetName, target.name)
for diagnostic in diagnosticsFound {
XCTAssertEqual(diagnostic?.metadata?.packageIdentity, builder.packageIdentity)
XCTAssertEqual(diagnostic?.metadata?.packageKind, builder.packageKind)
XCTAssertEqual(diagnostic?.metadata?.targetName, target.name)
}
}
}

// should not emit for "remote" packages

do {
let observability = ObservabilitySystem.makeForTesting()

let builder = TargetSourcesBuilder(
packageIdentity: .plain("test"),
packageKind: .remoteSourceControl(URL(string: "https://some.where/foo/bar")!),
packagePath: .root,
target: target,
path: .root,
defaultLocalization: nil,
toolsVersion: .v5,
fileSystem: fs,
observabilityScope: observability.topScope
)
_ = try builder.run()

XCTAssertNoDiagnostics(observability.diagnostics)
}
}


func testMissingSource() throws {
let target = try TargetDescription(
Expand Down Expand Up @@ -814,6 +864,68 @@ class TargetSourcesBuilderTests: XCTestCase {
}
}

func testUnhandledResources() throws {
let target = try TargetDescription(
name: "Foo",
path: nil,
exclude: [],
sources: ["File.swift"],
resources: [],
publicHeadersPath: nil,
type: .regular
)

let fs = InMemoryFileSystem()
fs.createEmptyFiles(at: .root, files: [
"/File.swift",
"/foo.bar"
])

do {
let observability = ObservabilitySystem.makeForTesting()

let builder = TargetSourcesBuilder(
packageIdentity: .plain("test"),
packageKind: .root(.init("/test")),
packagePath: .root,
target: target,
path: .root,
defaultLocalization: nil,
toolsVersion: .v5_5,
fileSystem: fs,
observabilityScope: observability.topScope
)
_ = try builder.run()

testDiagnostics(observability.diagnostics) { result in
let diagnostic = result.check(diagnostic: "found 1 file(s) which are unhandled; explicitly declare them as resources or exclude from the target\n /foo.bar\n", severity: .warning)
XCTAssertEqual(diagnostic?.metadata?.packageIdentity, builder.packageIdentity)
XCTAssertEqual(diagnostic?.metadata?.targetName, target.name)
}
}

// should not emit for "remote" packages

do {
let observability = ObservabilitySystem.makeForTesting()

let builder = TargetSourcesBuilder(
packageIdentity: .plain("test"),
packageKind: .remoteSourceControl(URL(string: "https://some.where/foo/bar")!),
packagePath: .root,
target: target,
path: .root,
defaultLocalization: nil,
toolsVersion: .v5_5,
fileSystem: fs,
observabilityScope: observability.topScope
)
_ = try builder.run()

XCTAssertNoDiagnostics(observability.diagnostics)
}
}

func testDocCFilesDoNotCauseWarningOutsideXCBuild() throws {
let target = try TargetDescription(
name: "Foo",
Expand Down