Skip to content

Commit d7f8f70

Browse files
committed
Throw an error if a URL's reported path is not absolute
1 parent 1d66c4f commit d7f8f70

File tree

2 files changed

+13
-6
lines changed

2 files changed

+13
-6
lines changed

Sources/SWBBuildService/BuildService.swift

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,20 +90,24 @@ open class BuildService: Service, @unchecked Sendable {
9090
let fs = SWBUtil.localFS
9191
var binariesToCheck = [Path]()
9292
// Check our own bundle's executable.
93-
if let executablePath = try Bundle.main.executableURL?.filePath {
94-
if fs.exists(executablePath) {
95-
binariesToCheck.append(executablePath)
93+
do {
94+
if let executablePath = try Bundle.main.executableURL?.filePath {
95+
if fs.exists(executablePath) {
96+
binariesToCheck.append(executablePath)
97+
}
9698
}
99+
} catch {
100+
throw StubError.error("Couldn't get main bundle executable URL. Error: \(error), bundle: \(Bundle.main), url: \(String(describing: Bundle.main.executableURL)), path: \(String(describing: try? Bundle.main.executableURL?.filePath))")
97101
}
98102
// Check all the binaries of frameworks in the Frameworks folder.
99103
// Note that this does not recurse into the frameworks for other items nested inside them. We also presently don't check the PlugIns folder.
100-
if let frameworksPath = try Bundle.main.privateFrameworksURL?.filePath {
104+
if let frameworksPath = try? Bundle.main.privateFrameworksURL?.filePath {
101105
do {
102106
for subpath in try fs.listdir(frameworksPath) {
103107
let frameworkPath = frameworksPath.join(subpath)
104108
// Load a bundle at this path. This means we'll skip things which aren't bundles, such as the Swift standard libraries.
105109
if let bundle = Bundle(path: frameworkPath.str) {
106-
if let unresolvedExecutablePath = try bundle.executableURL?.filePath {
110+
if let unresolvedExecutablePath = try? bundle.executableURL?.filePath {
107111
let executablePath = try fs.realpath(unresolvedExecutablePath)
108112
if fs.exists(executablePath) {
109113
binariesToCheck.append(executablePath)

Sources/SWBUtil/URL.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ extension URL {
2828
throw FileURLError.notRepresentable(self)
2929
}
3030
let fp = Path(String(cString: cString))
31-
precondition(fp.isAbsolute, "path '\(fp.str)' is not absolute")
31+
guard fp.isAbsolute else {
32+
throw FileURLError.pathNotAbsolute(fp)
33+
}
3234
return fp
3335
}
3436
}
@@ -37,4 +39,5 @@ extension URL {
3739

3840
fileprivate enum FileURLError: Error {
3941
case notRepresentable(URL)
42+
case pathNotAbsolute(Path)
4043
}

0 commit comments

Comments
 (0)