Skip to content

Commit 61a750a

Browse files
committed
getcwd() no longer throws
This is an example of too-edge-case a runtime error to throw IMO. Instead there is a safe way to manage the process working directory so that the single error that is possible, ie. there is no working directory, cannot happen. So we document that you should follow that methodology and instead fatalError in the event that the error does occur. The consequences here are many situations where try was added to simple functions, and those trys have been removed.
1 parent 7661b27 commit 61a750a

File tree

8 files changed

+35
-32
lines changed

8 files changed

+35
-32
lines changed

Sources/Multitool/directories.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public func directories() throws -> (root: String, build: String) {
2020
}
2121

2222
private func packageRoot() throws -> String {
23-
var rootd = try getcwd()
23+
var rootd = getcwd()
2424
while !Path.join(rootd, Manifest.filename).isFile {
2525
rootd = rootd.parentDirectory
2626
guard rootd != "/" else {

Sources/POSIX/getcwd.swift

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,33 @@
88
See http://swift.org/CONTRIBUTORS.txt for Swift project authors
99
*/
1010

11-
12-
import var libc.errno
13-
import func libc.free
1411
import func libc.getcwd
12+
import func libc.free
13+
import func libc.exit
1514
import var libc.PATH_MAX
15+
import var libc.stderr
16+
import var libc.errno
1617

1718
/**
1819
- Returns: The absolute pathname of the current working directory.
20+
- Note: If the current directory does not exist, aborts program,
21+
to deal with this you should `opendir(getcwd())` as soon as your
22+
program starts and then not `chdir()`, `chdir` is an anti-pattern
23+
in tooling anyway.
24+
- Warning: As a result of the above note use of POSIX demands that
25+
the working directory not change during execution. This requires
26+
you to have control over the purity of your dependencies.
1927
*/
20-
public func getcwd() throws -> String {
28+
public func getcwd() -> String {
29+
30+
@noreturn func error() {
31+
try! fputs("error: no current directory\n", libc.stderr)
32+
exit(2)
33+
}
34+
2135
let cwd = libc.getcwd(nil, Int(PATH_MAX))
22-
if cwd == nil { throw SystemError.getcwd(errno) }
36+
if cwd == nil { error() }
2337
defer { free(cwd) }
24-
guard let path = String(validatingUTF8: cwd) else { throw SystemError.getcwd(-1) }
38+
guard let path = String(validatingUTF8: cwd) else { error() }
2539
return path
2640
}

Sources/POSIX/mkdir.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public func mkdir(path: [String]) throws -> String {
2828
let parts = path.flatMap{ $0.characters.split(separator: "/") }
2929
var prefix = path.first!.hasPrefix("/")
3030
? ""
31-
: try getcwd()
31+
: getcwd()
3232

3333
for dir in parts {
3434
prefix = "\(prefix)/\(String(dir))"

Sources/Utility/Path.swift

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -116,12 +116,8 @@ public struct Path {
116116
// The above function requires both paths to be either relative
117117
// or absolute. So if they differ we make them both absolute.
118118
if abs.0 != abs.1 {
119-
do {
120-
if !abs.path { path = try path.abspath() }
121-
if !abs.pivot { pivot = try pivot.abspath() }
122-
} catch {
123-
return path.normpath
124-
}
119+
if !abs.path { path = path.abspath() }
120+
if !abs.pivot { pivot = pivot.abspath() }
125121
}
126122

127123
return go(clean(string), clean(pivot))
@@ -201,10 +197,10 @@ extension String {
201197
/**
202198
Return a normalized absolutized version of this path. Equivalent to:
203199

204-
Path.join(try getcwd(), self).normpath
200+
Path.join(getcwd(), self).normpath
205201
*/
206-
public func abspath() throws -> String {
207-
return Path.join(try getcwd(), self).normpath
202+
public func abspath() -> String {
203+
return Path.join(getcwd(), self).normpath
208204
}
209205

210206
/// - Returns: true if the string looks like an absolute path
@@ -295,12 +291,6 @@ extension String {
295291
not changing during execution.
296292
*/
297293
public var prettyPath: String {
298-
if let wd = try? getcwd() {
299-
return Path(self).relative(to: wd)
300-
} else if let abspath = try? abspath() {
301-
return abspath
302-
} else {
303-
return self
304-
}
294+
return Path(self).relative(to: getcwd())
305295
}
306296
}

Sources/swift-build/initPackage.swift

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,10 @@ final class InitPackage {
2222

2323
let mode: InitMode
2424
let pkgname: String
25-
let rootd: String
25+
let rootd = POSIX.getcwd()
2626

27-
init(mode: InitMode) throws {
27+
init(mode: InitMode) {
2828
self.mode = mode
29-
rootd = try POSIX.getcwd()
3029
pkgname = rootd.basename
3130
}
3231

Sources/swift-build/main.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import Build
2222
import Get
2323

2424

25-
private let origwd = (try? getcwd()) ?? "/"
25+
private let origwd = getcwd()
2626

2727
extension String {
2828
private var prettied: String {
@@ -72,7 +72,7 @@ do {
7272
try build(YAMLPath: yaml, target: "default")
7373

7474
case .Init(let initMode):
75-
let initPackage = try InitPackage(mode: initMode)
75+
let initPackage = InitPackage(mode: initMode)
7676
try initPackage.writePackageStructure()
7777

7878
case .Fetch:

Tests/Functional/Utilities.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ func swiftBuildPath() -> String {
8787
}
8888
fatalError()
8989
#else
90-
return Path.join(try! Process.arguments.first!.abspath().parentDirectory, "swift-build")
90+
return Path.join(Process.arguments.first!.abspath().parentDirectory, "swift-build")
9191
#endif
9292
}
9393

Tests/Utility/PathTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ class StatTests: XCTestCase {
184184
}
185185

186186
func test_realpath() {
187-
XCTAssertEqual(try! realpath("."), try! getcwd())
187+
XCTAssertEqual(try! realpath("."), getcwd())
188188
}
189189

190190
func test_basename() {
@@ -208,7 +208,7 @@ class RelativePathTests: XCTestCase {
208208
}
209209

210210
func testMixed() {
211-
XCTAssertEqual("3/4", Path(try! getcwd() + "/1/2/3/4").relative(to: "1/2"))
211+
XCTAssertEqual("3/4", Path(getcwd() + "/1/2/3/4").relative(to: "1/2"))
212212
}
213213

214214
func testRelativeCommonSubprefix() {

0 commit comments

Comments
 (0)