Skip to content

Commit 1990046

Browse files
authored
Fix Package.swift diagnostics line off by 1 for tools versions <= 5.7 (swiftlang#7795)
Fixes issue swiftlang#7688 SwiftPM was implicitly prepending the manifest with 'import Foundation' for toolchains using Swift toolchain versions 5.7 and under. The original file itself was never overwritten, thus the diagnostics being reported would always present on the next line. This is a quick fix that appends the same line to the end of the manifest; doing so assures that the intended behaviour doesn't change, but allows for correct diagnostics to be reported. ### Motivation: Diagnostics would present on the incorrect line for Package.swift files using tools version 5.7 and under. ### Modifications: When evaluating the manifest, a temporary directory copies the contents of the manifest to a temporary file; rather than prepending the import to Foundation at the top of the file (causing all lines in the diagnostic reports to be off by one), append the import to the bottom of the temporary manifest when dealing with tools versions 5.7 and under. ### Result: The intended behaviour that requires users to explicitly import Foundation doesn't change for Swift versions 5.8 and up; diagnostics reported for Package.swift files for those using swift <= 5.7 will now show on the correct line.
1 parent 10d2098 commit 1990046

File tree

2 files changed

+5
-3
lines changed

2 files changed

+5
-3
lines changed

Sources/PackageLoading/ManifestLoader.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -768,13 +768,15 @@ public final class ManifestLoader: ManifestLoaderProtocol {
768768
if toolsVersion >= .v5_8 {
769769
manifestPreamble = ByteString()
770770
} else {
771-
manifestPreamble = ByteString("import Foundation\n")
771+
manifestPreamble = ByteString("\nimport Foundation")
772772
}
773773

774774
do {
775775
try withTemporaryDirectory { tempDir, cleanupTempDir in
776776
let manifestTempFilePath = tempDir.appending("manifest.swift")
777-
try localFileSystem.writeFileContents(manifestTempFilePath, bytes: ByteString(manifestPreamble.contents + manifestContents))
777+
// Since this isn't overwriting the original file, append Foundation library
778+
// import to avoid having diagnostics being displayed on the incorrect line.
779+
try localFileSystem.writeFileContents(manifestTempFilePath, bytes: ByteString(manifestContents + manifestPreamble.contents))
778780

779781
let vfsOverlayTempFilePath = tempDir.appending("vfs.yaml")
780782
try VFSOverlay(roots: [

Tests/WorkspaceTests/WorkspaceTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ final class WorkspaceTests: XCTestCase {
252252
testDiagnostics(observability.diagnostics) { result in
253253
let diagnostic = result.check(
254254
diagnostic: .contains(
255-
"\(pkgDir.appending("Package.swift")):4:8: error: An error in MyPkg"
255+
"\(pkgDir.appending("Package.swift")):3:8: error: An error in MyPkg"
256256
),
257257
severity: .error
258258
)

0 commit comments

Comments
 (0)