Skip to content

Commit 2215d09

Browse files
authored
Fix: preserve symlinks in ZipArchive compress function (#8250)
This PR fixes #8248, by adding the `y` flag to the `zip` command that is executed when archiving a package's sources for publication to a registry on a non-Windows OS. ### Motivation: Without this change, packages which contain symlinks in their sources will produce invalid and potentially non-buildable archives when publishing to a registry, since any symlinks are currently transformed into duplicate files during the archive creation, instead of being preserved. ### Modifications: I've added the following flag to the `zip` call: > -y store symbolic links as the link instead of the referenced file I've additionally modified the associated `testCompress()` test case, adding logic to ensure that symlinks are preserved correctly. ### Result: With this change, the resulting zipfile that is uploaded to a registry will contain symlinks consistent with the original package sources.
1 parent 57a8f70 commit 2215d09

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

Sources/Basics/Archiver/ZipArchiver.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ public struct ZipArchiver: Archiver, Cancellable {
120120
arguments: [
121121
"/bin/sh",
122122
"-c",
123-
"cd \(directory.parentDirectory.underlying.pathString) && zip -r \(destinationPath.pathString) \(directory.basename)",
123+
"cd \(directory.parentDirectory.underlying.pathString) && zip -ry \(destinationPath.pathString) \(directory.basename)",
124124
]
125125
)
126126
#endif

Tests/BasicsTests/Archiver/ZipArchiverTests.swift

+8-1
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ final class ZipArchiverTests: XCTestCase {
118118
try localFileSystem.createDirectory(dir2)
119119
try localFileSystem.writeFileContents(dir2.appending("file3.txt"), string: "Hello World 3!")
120120
try localFileSystem.writeFileContents(dir2.appending("file4.txt"), string: "Hello World 4!")
121+
try localFileSystem.createSymbolicLink(dir2.appending("file5.txt"), pointingAt: dir1.appending("file2.txt"), relative: true)
121122

122123
let archivePath = tmpdir.appending(component: UUID().uuidString + ".zip")
123124
try await archiver.compress(directory: rootDir, to: archivePath)
@@ -154,6 +155,12 @@ final class ZipArchiverTests: XCTestCase {
154155
try? localFileSystem.readFileContents(extractedDir2.appending("file4.txt")),
155156
"Hello World 4!"
156157
)
158+
159+
XCTAssertTrue(localFileSystem.isSymlink(extractedDir2.appending("file5.txt")))
160+
XCTAssertEqual(
161+
try? localFileSystem.readFileContents(extractedDir2.appending("file5.txt")),
162+
try? localFileSystem.readFileContents(extractedDir1.appending("file2.txt"))
163+
)
157164
}
158165
}
159-
}
166+
}

0 commit comments

Comments
 (0)