Skip to content

Commit 14ceb3e

Browse files
committed
Remove slightly dangerous optimization
If there happens to be a file at the location passed into the `ManifestLoader, we were using that to compile the manifest, instead of the actual contents. This is dangerous, because the public API of `ManifestLoader` accepts a custom filesystem parameter that is not taken into account at all here, so the file being used might not be the right one, it just has the same path on the local filesystem. We could have done some more work to preserve the optimization in the case where the used filesystem is the local one and the correct file is being used, but that doesn't seem worth it since the vast majority of loads are being done for remote dependencies (this we may load multiple times as we are resolving) which are never using the local filesystem.
1 parent 256c682 commit 14ceb3e

File tree

2 files changed

+17
-20
lines changed

2 files changed

+17
-20
lines changed

Sources/PackageLoading/ManifestLoader.swift

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -442,27 +442,24 @@ public final class ManifestLoader: ManifestLoaderProtocol {
442442
completion: @escaping (Result<EvaluationResult, Error>) -> Void
443443
) throws {
444444
do {
445-
if localFileSystem.isFile(manifestPath) {
445+
try withTemporaryFile(suffix: ".swift") { tempFile, cleanupTempFile in
446+
try localFileSystem.writeFileContents(tempFile.path, bytes: ByteString(manifestContents))
446447
try self.evaluateManifest(
447-
at: manifestPath,
448+
at: tempFile.path,
448449
packageIdentity: packageIdentity,
449450
toolsVersion: toolsVersion,
450-
delegateQueue: delegateQueue,
451-
callbackQueue: callbackQueue,
452-
completion: completion
453-
)
454-
} else {
455-
try withTemporaryFile(suffix: ".swift") { tempFile, cleanupTempFile in
456-
try localFileSystem.writeFileContents(tempFile.path, bytes: ByteString(manifestContents))
457-
try self.evaluateManifest(
458-
at: tempFile.path,
459-
packageIdentity: packageIdentity,
460-
toolsVersion: toolsVersion,
461-
delegateQueue: delegateQueue,
462-
callbackQueue: callbackQueue
463-
) { result in
464-
dispatchPrecondition(condition: .onQueue(callbackQueue))
465-
cleanupTempFile(tempFile)
451+
delegateQueue: delegateQueue,
452+
callbackQueue: callbackQueue
453+
) { result in
454+
dispatchPrecondition(condition: .onQueue(callbackQueue))
455+
cleanupTempFile(tempFile)
456+
457+
switch result {
458+
case .success(var evaluationResult):
459+
// Replace the temporary path with the orgiginal one in compiler output.
460+
evaluationResult.compilerOutput = evaluationResult.compilerOutput?.replacingOccurrences(of: tempFile.path.pathString, with: manifestPath.pathString)
461+
completion(.success(evaluationResult))
462+
case .failure:
466463
completion(result)
467464
}
468465
}

Tests/PackageLoadingTests/PD_4_2_LoadingTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -609,7 +609,7 @@ class PackageDescription4_2LoadingTests: PackageDescriptionLoadingTests {
609609

610610
XCTAssertNoDiagnostics(observability.diagnostics)
611611
XCTAssertEqual(try delegate.loaded(timeout: .now() + 1), [manifestPath])
612-
XCTAssertEqual(try delegate.parsed(timeout: .now() + 1), expectCached ? [] : [manifestPath])
612+
XCTAssertEqual(try delegate.parsed(timeout: .now() + 1).count, expectCached ? 0 : 1)
613613
XCTAssertEqual(manifest.displayName, "Trivial")
614614
XCTAssertEqual(manifest.targets[0].name, "foo")
615615
}
@@ -669,7 +669,7 @@ class PackageDescription4_2LoadingTests: PackageDescriptionLoadingTests {
669669

670670
XCTAssertNoDiagnostics(observability.diagnostics)
671671
XCTAssertEqual(try delegate.loaded(timeout: .now() + 1), [manifestPath])
672-
XCTAssertEqual(try delegate.parsed(timeout: .now() + 1), expectCached ? [] : [manifestPath])
672+
XCTAssertEqual(try delegate.parsed(timeout: .now() + 1).count, expectCached ? 0 : 1)
673673
XCTAssertEqual(manifest.displayName, "Trivial")
674674
XCTAssertEqual(manifest.targets[0].name, "foo")
675675
}

0 commit comments

Comments
 (0)