Skip to content

Commit 4f58c55

Browse files
committed
Explicitly specify the encoding when loading a string with contents from a file
This fixes a deprecation warning on Linux about `String(contentsOf:)` being deprecated.
1 parent 6144b5f commit 4f58c55

File tree

6 files changed

+10
-9
lines changed

6 files changed

+10
-9
lines changed

Sources/Diagnose/MergeSwiftFiles.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ extension RequestInfo {
2323
progressUpdate: (_ progress: Double, _ message: String) -> Void
2424
) async throws -> RequestInfo? {
2525
let swiftFilePaths = compilerArgs.filter { $0.hasSuffix(".swift") }
26-
let mergedFile = try swiftFilePaths.map { try String(contentsOfFile: $0) }.joined(separator: "\n\n\n\n")
26+
let mergedFile = try swiftFilePaths.map { try String(contentsOfFile: $0, encoding: .utf8) }
27+
.joined(separator: "\n\n\n\n")
2728

2829
progressUpdate(0, "Merging all .swift files into a single file")
2930

Sources/Diagnose/ReduceCommand.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ package struct ReduceCommand: AsyncParsableCommand {
7878

7979
let progressBar = PercentProgressAnimation(stream: stderrStreamConcurrencySafe, header: "Reducing sourcekitd issue")
8080

81-
let request = try String(contentsOfFile: sourcekitdRequestPath)
81+
let request = try String(contentsOfFile: sourcekitdRequestPath, encoding: .utf8)
8282
let requestInfo = try RequestInfo(request: request)
8383

8484
let executor = OutOfProcessSourceKitRequestExecutor(

Sources/Diagnose/RequestInfo.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ package struct RequestInfo: Sendable {
104104

105105
self.requestTemplate = requestTemplate
106106

107-
fileContents = try String(contentsOf: URL(fileURLWithPath: sourceFilePath))
107+
fileContents = try String(contentsOf: URL(fileURLWithPath: sourceFilePath), encoding: .utf8)
108108
}
109109

110110
/// Create a `RequestInfo` that is used to reduce a `swift-frontend issue`
@@ -126,7 +126,7 @@ package struct RequestInfo: Sendable {
126126
guard let fileList = iterator.next() else {
127127
throw ReductionError("Expected file path after -filelist command line argument")
128128
}
129-
frontendArgsWithFilelistInlined += try String(contentsOfFile: fileList)
129+
frontendArgsWithFilelistInlined += try String(contentsOfFile: fileList, encoding: .utf8)
130130
.split(separator: "\n")
131131
.map { String($0) }
132132
default:

Sources/Diagnose/RunSourcekitdRequestCommand.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ package struct RunSourceKitdRequestCommand: AsyncParsableCommand {
4242
package init() {}
4343

4444
package func run() async throws {
45-
var requestString = try String(contentsOf: URL(fileURLWithPath: sourcekitdRequestPath))
45+
var requestString = try String(contentsOf: URL(fileURLWithPath: sourcekitdRequestPath), encoding: .utf8)
4646

4747
let installPath = try AbsolutePath(validating: Bundle.main.bundlePath)
4848
let sourcekitdPath =

Sources/SourceKitLSP/DocumentSnapshot+FromFileContents.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ package extension DocumentSnapshot {
2929
///
3030
/// Throws an error if the file could not be read.
3131
init(withContentsFromDisk url: URL, language: Language) throws {
32-
let contents = try String(contentsOf: url)
32+
let contents = try String(contentsOf: url, encoding: .utf8)
3333
self.init(uri: DocumentURI(url), language: language, version: 0, lineTable: LineTable(contents))
3434
}
3535
}

Tests/SourceKitLSPTests/BackgroundIndexingTests.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1199,7 +1199,7 @@ final class BackgroundIndexingTests: XCTestCase {
11991199
)
12001200
let packageResolvedURL = project.scratchDirectory.appendingPathComponent("Package.resolved")
12011201

1202-
let originalPackageResolvedContents = try String(contentsOf: packageResolvedURL)
1202+
let originalPackageResolvedContents = try String(contentsOf: packageResolvedURL, encoding: .utf8)
12031203

12041204
// First check our setup to see that we get the expected hover response before changing the dependency project.
12051205
let (uri, positions) = try project.openDocument("Test.swift")
@@ -1234,7 +1234,7 @@ final class BackgroundIndexingTests: XCTestCase {
12341234
])
12351235
)
12361236
try await project.testClient.send(PollIndexRequest())
1237-
XCTAssertEqual(try String(contentsOf: packageResolvedURL), originalPackageResolvedContents)
1237+
XCTAssertEqual(try String(contentsOf: packageResolvedURL, encoding: .utf8), originalPackageResolvedContents)
12381238

12391239
// Simulate a package update which goes as follows:
12401240
// - The user runs `swift package update`
@@ -1248,7 +1248,7 @@ final class BackgroundIndexingTests: XCTestCase {
12481248
],
12491249
workingDirectory: nil
12501250
)
1251-
XCTAssertNotEqual(try String(contentsOf: packageResolvedURL), originalPackageResolvedContents)
1251+
XCTAssertNotEqual(try String(contentsOf: packageResolvedURL, encoding: .utf8), originalPackageResolvedContents)
12521252
project.testClient.send(
12531253
DidChangeWatchedFilesNotification(changes: [
12541254
FileEvent(uri: DocumentURI(project.scratchDirectory.appendingPathComponent("Package.resolved")), type: .changed)

0 commit comments

Comments
 (0)