Skip to content

Commit a56c33d

Browse files
committed
[Basic] Convert precondition in determineTmpDir into a runtime error
This assumption is not always true and we shouldn't be crashing in such cases. <rdar://problem/45198013> Turn precondition in determineTempDirectory into a runtime throw
1 parent d6a8ba3 commit a56c33d

File tree

10 files changed

+33
-30
lines changed

10 files changed

+33
-30
lines changed

Sources/Basic/TemporaryFile.swift

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,13 @@ import class Foundation.FileManager
1616
public enum TempFileError: Swift.Error {
1717
/// Could not create a unique temporary filename.
1818
case couldNotCreateUniqueName
19+
1920
/// Some error thrown defined by posix's open().
2021
// FIXME: This should be factored out into a open error enum.
2122
case other(Int32)
23+
24+
/// Couldn't find a temporary directory.
25+
case couldNotFindTmpDir
2226
}
2327

2428
private extension TempFileError {
@@ -39,11 +43,12 @@ private extension TempFileError {
3943
/// - dir: If present this will be the temporary directory.
4044
///
4145
/// - Returns: Path to directory in which temporary file should be created.
42-
public func determineTempDirectory(_ dir: AbsolutePath? = nil) -> AbsolutePath {
46+
public func determineTempDirectory(_ dir: AbsolutePath? = nil) throws -> AbsolutePath {
4347
// FIXME: Add other platform specific locations.
4448
let tmpDir = dir ?? cachedTempDirectory
45-
// FIXME: This is a runtime condition, so it should throw and not crash.
46-
precondition(localFileSystem.isDirectory(tmpDir))
49+
guard localFileSystem.isDirectory(tmpDir) else {
50+
throw TempFileError.couldNotFindTmpDir
51+
}
4752
return tmpDir
4853
}
4954

@@ -95,7 +100,7 @@ public final class TemporaryFile {
95100
self.prefix = prefix
96101
self.deleteOnClose = deleteOnClose
97102
// Determine in which directory to create the temporary file.
98-
self.dir = determineTempDirectory(dir)
103+
self.dir = try determineTempDirectory(dir)
99104
// Construct path to the temporary file.
100105
let path = self.dir.appending(RelativePath(prefix + ".XXXXXX" + suffix))
101106

@@ -193,7 +198,7 @@ public final class TemporaryDirectory {
193198
self.shouldRemoveTreeOnDeinit = removeTreeOnDeinit
194199
self.prefix = prefix
195200
// Construct path to the temporary directory.
196-
let path = determineTempDirectory(dir).appending(RelativePath(prefix + ".XXXXXX"))
201+
let path = try determineTempDirectory(dir).appending(RelativePath(prefix + ".XXXXXX"))
197202

198203
// Convert path to a C style string terminating with null char to be an valid input
199204
// to mkdtemp method. The XXXXXX in this string will be replaced by a random string

Sources/Build/BuildPlan.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ import func POSIX.getenv
1818
public struct BuildParameters {
1919

2020
/// Path to the module cache directory to use for SwiftPM's own tests.
21-
public static let swiftpmTestCache = resolveSymlinks(determineTempDirectory()).appending(component: "org.swift.swiftpm.tests-3")
21+
// FIXME: Error handling.
22+
public static let swiftpmTestCache = resolveSymlinks(try! determineTempDirectory()).appending(component: "org.swift.swiftpm.tests-3")
2223

2324
/// Returns the directory to be used for module cache.
2425
fileprivate var moduleCache: AbsolutePath {

Sources/Commands/SwiftTool.swift

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -620,7 +620,7 @@ public class SwiftTool<Options: ToolOptions> {
620620
func runLLBuildAsExecutable(manifest: AbsolutePath, llbuildTarget: String) throws {
621621
// Create a temporary directory for the build process.
622622
let tempDirName = "org.swift.swiftpm.\(NSUserName())"
623-
let tempDir = Basic.determineTempDirectory().appending(component: tempDirName)
623+
let tempDir = try determineTempDirectory().appending(component: tempDirName)
624624
try localFileSystem.createDirectory(tempDir, recursive: true)
625625

626626
// Run the swift-build-tool with the generated manifest.
@@ -717,10 +717,9 @@ public class SwiftTool<Options: ToolOptions> {
717717
return Result(anyError: {
718718
try ManifestLoader(
719719
// Always use the host toolchain's resources for parsing manifest.
720-
resources: self._hostToolchain.dematerialize().manifestResources,
720+
manifestResources: self._hostToolchain.dematerialize().manifestResources,
721721
isManifestSandboxEnabled: !self.options.shouldDisableSandbox,
722-
isManifestCachingEnabled: !self.options.shouldDisableManifestCaching,
723-
cacheDir: self.buildPath
722+
cacheDir: self.options.shouldDisableManifestCaching ? nil : self.buildPath
724723
)
725724
})
726725
}()

Sources/PackageLoading/ManifestLoader.swift

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -131,20 +131,20 @@ public final class ManifestLoader: ManifestLoaderProtocol {
131131

132132
let resources: ManifestResourceProvider
133133
let isManifestSandboxEnabled: Bool
134-
let isManifestCachingEnabled: Bool
135-
let cacheDir: AbsolutePath
134+
var isManifestCachingEnabled: Bool {
135+
return cacheDir != nil
136+
}
137+
let cacheDir: AbsolutePath!
136138
let delegate: ManifestLoaderDelegate?
137139

138140
public init(
139-
resources: ManifestResourceProvider,
141+
manifestResources: ManifestResourceProvider,
140142
isManifestSandboxEnabled: Bool = true,
141-
isManifestCachingEnabled: Bool = true,
142-
cacheDir: AbsolutePath = determineTempDirectory(),
143+
cacheDir: AbsolutePath? = nil,
143144
delegate: ManifestLoaderDelegate? = nil
144145
) {
145-
self.resources = resources
146+
self.resources = manifestResources
146147
self.isManifestSandboxEnabled = isManifestSandboxEnabled
147-
self.isManifestCachingEnabled = isManifestCachingEnabled
148148
self.delegate = delegate
149149
self.cacheDir = cacheDir
150150
}
@@ -155,10 +155,8 @@ public final class ManifestLoader: ManifestLoaderProtocol {
155155
isManifestSandboxEnabled: Bool = true
156156
) {
157157
self.init(
158-
resources: resources,
159-
isManifestSandboxEnabled: isManifestSandboxEnabled,
160-
isManifestCachingEnabled: false,
161-
cacheDir: determineTempDirectory()
158+
manifestResources: resources,
159+
isManifestSandboxEnabled: isManifestSandboxEnabled
162160
)
163161
}
164162

Sources/Utility/Platform.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public enum Platform {
4444
var directories = [AbsolutePath]()
4545
// Compute the directories.
4646
directories.append(AbsolutePath("/private/var/tmp"))
47-
directories.append(Basic.determineTempDirectory())
47+
(try? Basic.determineTempDirectory()).map{ directories.append($0) }
4848
#if os(macOS)
4949
getConfstr(_CS_DARWIN_USER_TEMP_DIR).map({ directories.append($0) })
5050
getConfstr(_CS_DARWIN_USER_CACHE_DIR).map({ directories.append($0) })

Tests/PackageGraphPerformanceTests/DependencyResolverPerfTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ class DependencyResolverPerfTests: XCTestCasePerf {
9797

9898
let containerProvider = RepositoryPackageContainerProvider(
9999
repositoryManager: repositoryManager,
100-
manifestLoader: ManifestLoader(resources: Resources.default, isManifestCachingEnabled: false, cacheDir: path))
100+
manifestLoader: ManifestLoader(manifestResources: Resources.default))
101101

102102
let resolver = DependencyResolver(containerProvider, GitRepositoryResolutionHelper.DummyResolverDelegate())
103103
let container = PackageReference(identity: "dep", path: dep.asString)

Tests/PackageLoadingPerformanceTests/ManifestLoadingTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import TestSupport
1515
import PackageLoading
1616

1717
class ManifestLoadingPerfTests: XCTestCasePerf {
18-
let manifestLoader = ManifestLoader(resources: Resources.default, isManifestCachingEnabled: false)
18+
let manifestLoader = ManifestLoader(manifestResources: Resources.default)
1919

2020
func write(_ bytes: ByteString, body: (AbsolutePath) -> ()) {
2121
mktmpdir { path in

Tests/PackageLoadingTests/PD4LoadingTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import TestSupport
1919
import PackageLoading
2020

2121
class PackageDescription4LoadingTests: XCTestCase {
22-
let manifestLoader = ManifestLoader(resources: Resources.default, isManifestCachingEnabled: false)
22+
let manifestLoader = ManifestLoader(manifestResources: Resources.default)
2323

2424
private func loadManifestThrowing(
2525
_ contents: ByteString,

Tests/PackageLoadingTests/PD4_2LoadingTests.swift

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import PackageLoading
1818

1919
// FIXME: We should share the infra with other loading tests.
2020
class PackageDescription4_2LoadingTests: XCTestCase {
21-
let manifestLoader = ManifestLoader(resources: Resources.default, isManifestCachingEnabled: false)
21+
let manifestLoader = ManifestLoader(manifestResources: Resources.default)
2222

2323
private func loadManifestThrowing(
2424
_ contents: ByteString,
@@ -378,8 +378,9 @@ class PackageDescription4_2LoadingTests: XCTestCase {
378378
}
379379

380380
let delegate = ManifestTestDelegate()
381+
381382
let manifestLoader = ManifestLoader(
382-
resources: Resources.default, cacheDir: path, delegate: delegate)
383+
manifestResources: Resources.default, cacheDir: path, delegate: delegate)
383384

384385
func check(loader: ManifestLoader, expectCached: Bool) {
385386
delegate.clear()
@@ -422,7 +423,7 @@ class PackageDescription4_2LoadingTests: XCTestCase {
422423
}
423424

424425
let noCacheLoader = ManifestLoader(
425-
resources: Resources.default, isManifestCachingEnabled: false, delegate: delegate)
426+
manifestResources: Resources.default, delegate: delegate)
426427
for _ in 0..<2 {
427428
check(loader: noCacheLoader, expectCached: false)
428429
}

Tests/WorkspaceTests/WorkspaceTests.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,7 @@ final class WorkspaceTests: XCTestCase {
119119
"""
120120
}
121121

122-
let manifestLoader = ManifestLoader(
123-
resources: Resources.default, isManifestCachingEnabled: false)
122+
let manifestLoader = ManifestLoader(manifestResources: Resources.default)
124123

125124
let sandbox = path.appending(component: "ws")
126125
let ws = Workspace(

0 commit comments

Comments
 (0)