Skip to content

Commit 3c2ed6a

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 5b88aef commit 3c2ed6a

File tree

2 files changed

+14
-23
lines changed

2 files changed

+14
-23
lines changed

Sources/PackageLoading/ManifestLoader.swift

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -442,29 +442,19 @@ public final class ManifestLoader: ManifestLoaderProtocol {
442442
completion: @escaping (Result<EvaluationResult, Error>) -> Void
443443
) {
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
self.evaluateManifest(
447-
at: manifestPath,
448+
at: tempFile.path,
449+
originalPath: manifestPath,
448450
packageIdentity: packageIdentity,
449451
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-
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)
466-
completion(result)
467-
}
452+
delegateQueue: delegateQueue,
453+
callbackQueue: callbackQueue
454+
) { result in
455+
dispatchPrecondition(condition: .onQueue(callbackQueue))
456+
cleanupTempFile(tempFile)
457+
completion(result)
468458
}
469459
}
470460
} catch {
@@ -477,6 +467,7 @@ public final class ManifestLoader: ManifestLoaderProtocol {
477467
/// Helper method for evaluating the manifest.
478468
func evaluateManifest(
479469
at manifestPath: AbsolutePath,
470+
originalPath: AbsolutePath,
480471
packageIdentity: PackageIdentity,
481472
toolsVersion: ToolsVersion,
482473
delegateQueue: DispatchQueue,
@@ -597,7 +588,7 @@ public final class ManifestLoader: ManifestLoaderProtocol {
597588
let compilerResult : ProcessResult
598589
do {
599590
compilerResult = try result.get()
600-
evaluationResult.compilerOutput = try (compilerResult.utf8Output() + compilerResult.utf8stderrOutput()).spm_chuzzle()
591+
evaluationResult.compilerOutput = try (compilerResult.utf8Output() + compilerResult.utf8stderrOutput()).spm_chuzzle()?.replacingOccurrences(of: manifestPath.pathString, with: originalPath.pathString)
601592
} catch {
602593
return completion(.failure(error))
603594
}

Tests/PackageLoadingTests/PD_4_2_LoadingTests.swift

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

590590
XCTAssertNoDiagnostics(observability.diagnostics)
591591
XCTAssertEqual(try delegate.loaded(timeout: .now() + 1), [manifestPath])
592-
XCTAssertEqual(try delegate.parsed(timeout: .now() + 1), expectCached ? [] : [manifestPath])
592+
XCTAssertEqual(try delegate.parsed(timeout: .now() + 1).count, expectCached ? 0 : 1)
593593
XCTAssertEqual(manifest.displayName, "Trivial")
594594
XCTAssertEqual(manifest.targets[0].name, "foo")
595595
}
@@ -649,7 +649,7 @@ class PackageDescription4_2LoadingTests: PackageDescriptionLoadingTests {
649649

650650
XCTAssertNoDiagnostics(observability.diagnostics)
651651
XCTAssertEqual(try delegate.loaded(timeout: .now() + 1), [manifestPath])
652-
XCTAssertEqual(try delegate.parsed(timeout: .now() + 1), expectCached ? [] : [manifestPath])
652+
XCTAssertEqual(try delegate.parsed(timeout: .now() + 1).count, expectCached ? 0 : 1)
653653
XCTAssertEqual(manifest.displayName, "Trivial")
654654
XCTAssertEqual(manifest.targets[0].name, "foo")
655655
}

0 commit comments

Comments
 (0)