Skip to content

Commit cca73b2

Browse files
authored
swift sdk: print warnings on stderr instead of stdout (#7532)
Tools and scripts may rely on command output to remain parseable, so printing these warnings on `stdout` can break such workflows. We should output these warnings on `stderr` instead. As these warnings are emitted before any logging is available, we're using `fputs` for output on `stderr`
1 parent 32442c6 commit cca73b2

File tree

3 files changed

+13
-13
lines changed

3 files changed

+13
-13
lines changed

Sources/swift-experimental-sdk/Entrypoint.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,12 @@
1111
//===----------------------------------------------------------------------===//
1212

1313
import SwiftSDKCommand
14+
import Foundation
1415

1516
@main
1617
struct Entrypoint {
1718
static func main() async {
18-
print("warning: `swift experimental-sdk` command is deprecated and will be removed in a future version of SwiftPM. Use `swift sdk` instead.")
19+
fputs("warning: `swift experimental-sdk` command is deprecated and will be removed in a future version of SwiftPM. Use `swift sdk` instead.", stderr)
1920
await SwiftSDKCommand.main()
2021
}
2122
}

Sources/swift-package-manager/SwiftPM.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
import Basics
1414

1515
import Commands
16-
16+
import Foundation
1717
import SwiftSDKCommand
1818
import PackageCollectionsCommand
1919
import PackageRegistryCommand
@@ -42,7 +42,7 @@ struct SwiftPM {
4242
case "swift-build":
4343
await SwiftBuildCommand.main()
4444
case "swift-experimental-sdk":
45-
print("warning: `swift experimental-sdk` command is deprecated and will be removed in a future version of SwiftPM. Use `swift sdk` instead.")
45+
fputs("warning: `swift experimental-sdk` command is deprecated and will be removed in a future version of SwiftPM. Use `swift sdk` instead.", stderr)
4646
fallthrough
4747
case "swift-sdk":
4848
await SwiftSDKCommand.main()

Tests/CommandsTests/SDKCommandTests.swift

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ final class SDKCommandTests: CommandsTestCase {
4848
)
4949

5050
if command == .experimentalSDK {
51-
XCTAssertMatch(stdout, .contains(deprecationWarning))
51+
XCTAssertMatch(stderr, .contains(deprecationWarning))
52+
XCTAssertNoMatch(stdout, .contains(deprecationWarning))
5253
}
5354

5455
// We only expect tool's output on the stdout stream.
@@ -57,18 +58,16 @@ final class SDKCommandTests: CommandsTestCase {
5758
.contains("\(bundle)` successfully installed as test-sdk.artifactbundle.")
5859
)
5960

60-
XCTAssertEqual(stderr.count, 0)
61-
6261
(stdout, stderr) = try command.execute(
6362
["list", "--swift-sdks-path", fixturePath.pathString])
6463

6564
if command == .experimentalSDK {
66-
XCTAssertMatch(stdout, .contains(deprecationWarning))
65+
XCTAssertMatch(stderr, .contains(deprecationWarning))
66+
XCTAssertNoMatch(stdout, .contains(deprecationWarning))
6767
}
6868

6969
// We only expect tool's output on the stdout stream.
7070
XCTAssertMatch(stdout, .contains("test-artifact"))
71-
XCTAssertEqual(stderr.count, 0)
7271

7372
XCTAssertThrowsError(try command.execute(
7473
[
@@ -91,30 +90,30 @@ final class SDKCommandTests: CommandsTestCase {
9190
}
9291

9392
if command == .experimentalSDK {
94-
XCTAssertMatch(stdout, .contains(deprecationWarning))
93+
XCTAssertMatch(stderr, .contains(deprecationWarning))
9594
}
9695

9796
(stdout, stderr) = try command.execute(
9897
["remove", "--swift-sdks-path", fixturePath.pathString, "test-artifact"])
9998

10099
if command == .experimentalSDK {
101-
XCTAssertMatch(stdout, .contains(deprecationWarning))
100+
XCTAssertMatch(stderr, .contains(deprecationWarning))
101+
XCTAssertNoMatch(stdout, .contains(deprecationWarning))
102102
}
103103

104104
// We only expect tool's output on the stdout stream.
105105
XCTAssertMatch(stdout, .contains("test-sdk.artifactbundle` was successfully removed from the file system."))
106-
XCTAssertEqual(stderr.count, 0)
107106

108107
(stdout, stderr) = try command.execute(
109108
["list", "--swift-sdks-path", fixturePath.pathString])
110109

111110
if command == .experimentalSDK {
112-
XCTAssertMatch(stdout, .contains(deprecationWarning))
111+
XCTAssertMatch(stderr, .contains(deprecationWarning))
112+
XCTAssertNoMatch(stdout, .contains(deprecationWarning))
113113
}
114114

115115
// We only expect tool's output on the stdout stream.
116116
XCTAssertNoMatch(stdout, .contains("test-artifact"))
117-
XCTAssertEqual(stderr.count, 0)
118117
}
119118
}
120119
}

0 commit comments

Comments
 (0)