Skip to content

Commit 4f0577c

Browse files
authored
allow use of warnings-as-errors while dependencies have supressed warning (#5613)
motivation: warnings-as-errors and suppress-warnings are mutually exclusive changes: * do not pass warnings-as-errors for remote dependencies which use suppress-warnings * add test
1 parent 635d70b commit 4f0577c

File tree

8 files changed

+94
-3
lines changed

8 files changed

+94
-3
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// swift-tools-version: 5.7
2+
import PackageDescription
3+
4+
let package = Package(
5+
name: "app",
6+
products: [
7+
.executable(name: "app", targets: ["app"])
8+
],
9+
dependencies: [
10+
.package(url: "../dep1", from: "1.0.0"),
11+
.package(url: "../dep2", from: "1.0.0"),
12+
],
13+
targets: [
14+
.executableTarget(
15+
name: "app",
16+
dependencies: [
17+
.product(name: "dep1", package: "dep1"),
18+
.product(name: "dep2", package: "dep2")
19+
],
20+
path: "./"
21+
)
22+
]
23+
)
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import dep1
2+
import dep2
3+
4+
@main
5+
struct App {
6+
public static func main() {
7+
print("hello, world!")
8+
}
9+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// swift-tools-version: 5.7
2+
import PackageDescription
3+
4+
let package = Package(
5+
name: "dep1",
6+
products: [
7+
.library(name: "dep1", targets: ["dep1"])
8+
],
9+
targets: [
10+
.target(name: "dep1", path: "./")
11+
]
12+
)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
struct Dep1 {
2+
var deprecated: Deprecated1
3+
}
4+
5+
@available(*, deprecated)
6+
struct Deprecated1 {
7+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// swift-tools-version: 5.7
2+
import PackageDescription
3+
4+
let package = Package(
5+
name: "dep2",
6+
products: [
7+
.library(name: "dep2", targets: ["dep2"])
8+
],
9+
targets: [
10+
.target(name: "dep2", path: "./")
11+
]
12+
)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
struct Dep2 {
2+
var deprecated: Deprecated2
3+
}
4+
5+
@available(*, deprecated)
6+
struct Deprecated2 {
7+
}

Sources/Build/BuildPlan.swift

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -873,14 +873,19 @@ public final class SwiftTargetBuildDescription {
873873
args += ["-emit-module-interface-path", parseableModuleInterfaceOutputPath.pathString]
874874
}
875875

876+
args += buildParameters.toolchain.extraSwiftCFlags
877+
// User arguments (from -Xswiftc) should follow generated arguments to allow user overrides
878+
args += buildParameters.swiftCompilerFlags
879+
876880
// suppress warnings if the package is remote
877881
if self.package.isRemote {
878882
args += ["-suppress-warnings"]
883+
// suppress-warnings and warnings-as-errors are mutually exclusive
884+
if let index = args.firstIndex(of: "-warnings-as-errors") {
885+
args.remove(at: index)
886+
}
879887
}
880888

881-
args += buildParameters.toolchain.extraSwiftCFlags
882-
// User arguments (from -Xswiftc) should follow generated arguments to allow user overrides
883-
args += buildParameters.swiftCompilerFlags
884889
return args
885890
}
886891

Tests/FunctionalTests/MiscellaneousTests.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -844,4 +844,20 @@ class MiscellaneousTestCase: XCTestCase {
844844
XCTAssertNoMatch(stdout + stderr, .contains("'Deprecated2' is deprecated"))
845845
}
846846
}
847+
848+
func testNoWarningFromRemoteDependenciesWithWarningsAsErrors() throws {
849+
try fixture(name: "Miscellaneous/DependenciesWarnings2") { path in
850+
// prepare the deps as git sources
851+
let dependency1Path = path.appending(component: "dep1")
852+
initGitRepo(dependency1Path, tag: "1.0.0")
853+
let dependency2Path = path.appending(component: "dep2")
854+
initGitRepo(dependency2Path, tag: "1.0.0")
855+
856+
let appPath = path.appending(component: "app")
857+
let (stdout, stderr) = try SwiftPMProduct.SwiftBuild.execute(["-Xswiftc", "-warnings-as-errors"], packagePath: appPath)
858+
XCTAssertDirectoryExists(appPath.appending(component: ".build"))
859+
XCTAssertNoMatch(stdout + stderr, .contains("'Deprecated1' is deprecated"))
860+
XCTAssertNoMatch(stdout + stderr, .contains("'Deprecated2' is deprecated"))
861+
}
862+
}
847863
}

0 commit comments

Comments
 (0)