From 17edb2cb6c370676469b37b2cae73376469f0c3b Mon Sep 17 00:00:00 2001 From: Kare Morstol Date: Wed, 1 Jul 2020 15:49:56 +0200 Subject: [PATCH] fix #134: crash for operator functions when no baseURL. Fixes crashes like this one: ```bash $ swift doc generate my-repo/Sources --module-name MyRepo 2020-06-30T17:00:13-0400 critical: Unable to construct path for /(lhs:rhs:) with baseURL / Fatal error: file /private/tmp/swift-doc-20200630-24417-jrxtfw/Sources/swift-doc/Supporting Types/Page.swift, line 55 zsh: illegal hardware instruction swift doc generate my-repo/Sources --module-name MyRepo ``` --- Sources/swift-doc/Supporting Types/Page.swift | 18 ++---------------- 1 file changed, 2 insertions(+), 16 deletions(-) diff --git a/Sources/swift-doc/Supporting Types/Page.swift b/Sources/swift-doc/Supporting Types/Page.swift index 14b20741..9f0dc031 100644 --- a/Sources/swift-doc/Supporting Types/Page.swift +++ b/Sources/swift-doc/Supporting Types/Page.swift @@ -48,9 +48,8 @@ func path(for symbol: Symbol, with baseURL: String) -> String { } func path(for identifier: CustomStringConvertible, with baseURL: String) -> String { - var urlComponents = URLComponents(string: baseURL) - urlComponents = urlComponents?.appendingPathComponent("\(identifier)") - guard let string = urlComponents?.string else { + let url = URL(string: baseURL)?.appendingPathComponent("\(identifier)") + guard let string = url?.path else { logger.critical("Unable to construct path for \(identifier) with baseURL \(baseURL)") fatalError() } @@ -65,16 +64,3 @@ func writeFile(_ data: Data, to url: URL) throws { try data.write(to: url) try fileManager.setAttributes([.posixPermissions: 0o744], ofItemAtPath: url.path) } - -// MARK: - - -fileprivate extension URLComponents { - func appendingPathComponent(_ component: String) -> URLComponents? { - var urlComponents = self - var pathComponents = urlComponents.path.split(separator: "/").map { "\($0)" } - pathComponents.append(component) - urlComponents.path = "/" + pathComponents.joined(separator: "/") - - return urlComponents - } -}