Skip to content

Commit 73a5bd9

Browse files
committed
Fix symlink resolution
1 parent ee778d9 commit 73a5bd9

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

Sources/Commands/SwiftScriptTool.swift

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
import ArgumentParser
1212
import Basics
13+
import Foundation
1314
import PackageModel
1415
import ScriptingCore
1516
import TSCBasic
@@ -310,21 +311,27 @@ extension SwiftScriptTool {
310311
func run() throws {
311312
let cacheDir = try localFileSystem.getOrCreateSwiftScriptCacheDirectory()
312313
let scripts = try localFileSystem.getDirectoryContents(cacheDir)
313-
// Walk through the cache and find origin script paths.
314-
let resolved = try scripts.compactMap { script -> (String, AbsolutePath)? in
314+
// Walk through the cache and find original script paths.
315+
let resolved = try scripts.compactMap { script -> (String, String)? in
315316
let sourceDir = cacheDir.appending(components: script, "Sources")
316317
guard localFileSystem.isDirectory(sourceDir),
317318
let name = try localFileSystem.getDirectoryContents(sourceDir).first,
318-
case let realpath = resolveSymlinks(sourceDir.appending(components: name, "main.swift")) else {
319+
case let path = sourceDir.appending(components: name, "main.swift"),
320+
let destination = try? FileManager.default.destinationOfSymbolicLink(atPath: path.pathString) else {
319321
return nil
320322
}
321-
return (script, realpath)
323+
// Check if the original script still exists.
324+
if localFileSystem.exists(path, followSymlink: true) {
325+
return (script, destination)
326+
} else {
327+
return (script, "\(destination) (removed)")
328+
}
322329
}
323330
// Print the resolved cache info.
324331
print("\(scripts.count) script\(scripts.count > 1 ? "s" : "") cached at \(cacheDir)")
325332
guard let maxLength = resolved.map(\.0.count).max() else { return }
326-
resolved.forEach { (name, path) in
327-
print(name + String(repeating: " ", count: maxLength - name.count + 2) + path.pathString)
333+
resolved.forEach { (name, desc) in
334+
print(name + String(repeating: " ", count: maxLength - name.count + 2) + desc)
328335
}
329336
}
330337
}

0 commit comments

Comments
 (0)