Skip to content

Commit 8032dc7

Browse files
committed
[PackageLoading] Diagnose invalid values of build settings
<rdar://problem/52395869>
1 parent baa6dfa commit 8032dc7

File tree

3 files changed

+41
-1
lines changed

3 files changed

+41
-1
lines changed

Sources/PackageLoading/PackageDescription4Loader.swift

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,13 +135,27 @@ extension ManifestBuilder {
135135
condition = try parseCondition(conditionJSON)
136136
}
137137

138+
let value = try json.get([String].self, forKey: "value")
139+
140+
// Diagnose invalid values.
141+
for item in value {
142+
let groups = Self.invalidValueRegex.matchGroups(in: item).flatMap{ $0 }
143+
if !groups.isEmpty {
144+
let error = "the build setting '\(name)' contains invalid component(s): \(groups.joined(separator: " "))"
145+
throw ManifestParseError.runtimeManifestErrors([error])
146+
}
147+
}
148+
138149
return .init(
139150
tool: tool, name: name,
140-
value: try json.get("value"),
151+
value: value,
141152
condition: condition
142153
)
143154
}
144155

156+
/// Looks for Xcode-style build setting macros "$()".
157+
private static let invalidValueRegex = try! RegEx(pattern: #"(\$\(.*?\))"#)
158+
145159
func parseCondition(_ json: JSON) throws -> TargetBuildSettingDescription.Condition {
146160
let platformNames: [String]? = try? json.getArray("platforms").map({ try $0.get("name") })
147161
return .init(

Tests/PackageLoadingTests/PD5LoadingTests.swift

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,4 +369,29 @@ class PackageDescription5LoadingTests: XCTestCase {
369369
XCTAssertNotNil(contents)
370370
}
371371
}
372+
373+
func testInvalidBuildSettings() throws {
374+
let stream = BufferedOutputByteStream()
375+
stream <<< """
376+
import PackageDescription
377+
let package = Package(
378+
name: "Foo",
379+
targets: [
380+
.target(
381+
name: "Foo",
382+
cSettings: [
383+
.headerSearchPath("$(BYE)/path/to/foo/$(SRCROOT)/$(HELLO)"),
384+
]
385+
),
386+
]
387+
)
388+
"""
389+
390+
do {
391+
try loadManifestThrowing(stream.bytes) { _ in }
392+
XCTFail("Unexpected success")
393+
} catch ManifestParseError.runtimeManifestErrors(let errors) {
394+
XCTAssertEqual(errors, ["the build setting 'headerSearchPath' contains invalid component(s): $(BYE) $(SRCROOT) $(HELLO)"])
395+
}
396+
}
372397
}

Tests/PackageLoadingTests/XCTestManifests.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ extension PackageDescription5LoadingTests {
110110
static let __allTests__PackageDescription5LoadingTests = [
111111
("testBasics", testBasics),
112112
("testBuildSettings", testBuildSettings),
113+
("testInvalidBuildSettings", testInvalidBuildSettings),
113114
("testPlatforms", testPlatforms),
114115
("testSerializedDiagnostics", testSerializedDiagnostics),
115116
("testSwiftLanguageVersion", testSwiftLanguageVersion),

0 commit comments

Comments
 (0)