Skip to content

Commit 154fdea

Browse files
authored
Merge pull request #1017 from aciidb0mb3r/non-throwing-workspace
[Workspace] Make workspace init non throwing
2 parents 017149f + 7580146 commit 154fdea

File tree

3 files changed

+39
-38
lines changed

3 files changed

+39
-38
lines changed

Sources/Commands/SwiftTool.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ public class SwiftTool<Options: ToolOptions> {
206206
let delegate = ToolWorkspaceDelegate()
207207
let rootPackage = try getPackageRoot()
208208
let provider = GitRepositoryProvider(processSet: processSet)
209-
let workspace = try Workspace(
209+
let workspace = Workspace(
210210
dataPath: buildPath,
211211
editablesPath: rootPackage.appending(component: "Packages"),
212212
pinsFile: rootPackage.appending(component: "Package.pins"),

Sources/Workspace/Workspace.swift

+5-4
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ public class Workspace {
259259
fileSystem: FileSystem = localFileSystem,
260260
repositoryProvider: RepositoryProvider = GitRepositoryProvider(),
261261
enableResolverPrefetching: Bool = false
262-
) throws {
262+
) {
263263
self.rootPackages = []
264264
self.delegate = delegate
265265
self.dataPath = dataPath
@@ -283,9 +283,6 @@ public class Workspace {
283283
self.managedDependencies = ReloadableResult{
284284
try ManagedDependencies(dataPath: dataPath, fileSystem: fileSystem)
285285
}
286-
287-
// Ensure the cache path exists.
288-
try createCacheDirectories()
289286
}
290287

291288
/// Create the cache directories.
@@ -1012,6 +1009,10 @@ public class Workspace {
10121009
var errors: [Swift.Error] = []
10131010

10141011
do {
1012+
// FIXME: We need to avoid fetching dependencies if there is an
1013+
// error before dependency resolution.
1014+
// Ensure the cache path exists.
1015+
try createCacheDirectories()
10151016
// Validate that edited dependencies are still present.
10161017
try validateEditedPackages()
10171018
} catch {

Tests/WorkspaceTests/WorkspaceTests.swift

+33-33
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@ extension Workspace {
7070
delegate: WorkspaceDelegate = TestWorkspaceDelegate(),
7171
fileSystem: FileSystem = localFileSystem,
7272
repositoryProvider: RepositoryProvider = GitRepositoryProvider()
73-
) throws -> Workspace {
74-
let workspace = try Workspace(
73+
) -> Workspace {
74+
let workspace = Workspace(
7575
dataPath: path.appending(component: ".build"),
7676
editablesPath: path.appending(component: "Packages"),
7777
pinsFile: path.appending(component: "Package.pins"),
@@ -124,7 +124,7 @@ final class WorkspaceTests: XCTestCase {
124124

125125
// Create the initial workspace.
126126
do {
127-
let workspace = try Workspace.createWith(rootPackage: path)
127+
let workspace = Workspace.createWith(rootPackage: path)
128128
XCTAssertEqual(try workspace.managedDependencies.dematerialize().values.map{ $0.repository.url }, [])
129129

130130
// Do a low-level clone.
@@ -135,7 +135,7 @@ final class WorkspaceTests: XCTestCase {
135135

136136
// Re-open the workspace, and check we know the checkout version.
137137
do {
138-
let workspace = try Workspace.createWith(rootPackage: path)
138+
let workspace = Workspace.createWith(rootPackage: path)
139139
XCTAssertEqual(try workspace.managedDependencies.dematerialize().values.map{ $0.repository }, [testRepoSpec])
140140
if let dependency = try workspace.managedDependencies.dematerialize().values.first(where: { _ in true }) {
141141
XCTAssertEqual(dependency.repository, testRepoSpec)
@@ -151,7 +151,7 @@ final class WorkspaceTests: XCTestCase {
151151
// Re-check the persisted state.
152152
let statePath: AbsolutePath
153153
do {
154-
let workspace = try Workspace.createWith(rootPackage: path)
154+
let workspace = Workspace.createWith(rootPackage: path)
155155
statePath = try workspace.managedDependencies.dematerialize().statePath
156156
XCTAssertEqual(try workspace.managedDependencies.dematerialize().values.map{ $0.repository }, [testRepoSpec])
157157
if let dependency = try workspace.managedDependencies.dematerialize().values.first(where: { _ in true }) {
@@ -163,7 +163,7 @@ final class WorkspaceTests: XCTestCase {
163163
// Blow away the workspace state file, and check we can get back to a good state.
164164
try removeFileTree(statePath)
165165
do {
166-
let workspace = try Workspace.createWith(rootPackage: path)
166+
let workspace = Workspace.createWith(rootPackage: path)
167167
XCTAssertEqual(try workspace.managedDependencies.dematerialize().values.map{ $0.repository.url }, [])
168168
let state = CheckoutState(revision: currentRevision)
169169
_ = try workspace.clone(repository: testRepoSpec, at: state)
@@ -193,7 +193,7 @@ final class WorkspaceTests: XCTestCase {
193193
]
194194
)
195195
// Create the workspace.
196-
let workspace = try Workspace.createWith(rootPackage: path, manifestLoader: graph.manifestLoader, delegate: TestWorkspaceDelegate())
196+
let workspace = Workspace.createWith(rootPackage: path, manifestLoader: graph.manifestLoader, delegate: TestWorkspaceDelegate())
197197

198198
// Ensure we have checkouts for A & AA.
199199
for name in ["A", "AA"] {
@@ -232,7 +232,7 @@ final class WorkspaceTests: XCTestCase {
232232
)
233233

234234
// Create the workspace.
235-
let workspace = try Workspace.createWith(rootPackage: path, manifestLoader: manifestGraph.manifestLoader, delegate: TestWorkspaceDelegate())
235+
let workspace = Workspace.createWith(rootPackage: path, manifestLoader: manifestGraph.manifestLoader, delegate: TestWorkspaceDelegate())
236236

237237
// Ensure we have a checkout for A.
238238
for name in ["A"] {
@@ -263,7 +263,7 @@ final class WorkspaceTests: XCTestCase {
263263
],
264264
fs: fs
265265
)
266-
let workspace = try Workspace.createWith(rootPackage: path, manifestLoader: manifestGraph.manifestLoader, delegate: TestWorkspaceDelegate(), fileSystem: fs, repositoryProvider: manifestGraph.repoProvider!)
266+
let workspace = Workspace.createWith(rootPackage: path, manifestLoader: manifestGraph.manifestLoader, delegate: TestWorkspaceDelegate(), fileSystem: fs, repositoryProvider: manifestGraph.repoProvider!)
267267
let graph = workspace.loadPackageGraph()
268268
XCTAssertTrue(graph.errors.isEmpty)
269269
XCTAssertEqual(graph.packages.count, 2)
@@ -296,7 +296,7 @@ final class WorkspaceTests: XCTestCase {
296296
)
297297
// Create the workspace.
298298
let delegate = TestWorkspaceDelegate()
299-
let workspace = try Workspace.createWith(rootPackage: path, manifestLoader: manifestGraph.manifestLoader, delegate: delegate)
299+
let workspace = Workspace.createWith(rootPackage: path, manifestLoader: manifestGraph.manifestLoader, delegate: delegate)
300300

301301
// Ensure delegates haven't been called yet.
302302
XCTAssert(delegate.fetched.isEmpty)
@@ -355,7 +355,7 @@ final class WorkspaceTests: XCTestCase {
355355
let repoPath = AbsolutePath(manifestGraph.repo("A").url)
356356

357357
func createWorkspace() throws -> Workspace {
358-
return try Workspace.createWith(rootPackage: path, manifestLoader: manifestGraph.manifestLoader, delegate: delegate)
358+
return Workspace.createWith(rootPackage: path, manifestLoader: manifestGraph.manifestLoader, delegate: delegate)
359359
}
360360

361361
do {
@@ -427,7 +427,7 @@ final class WorkspaceTests: XCTestCase {
427427
try makeDirectories(testRepoPath)
428428
initGitRepo(testRepoPath, tag: "initial")
429429

430-
let workspace = try Workspace.createWith(rootPackage: path)
430+
let workspace = Workspace.createWith(rootPackage: path)
431431
let state = CheckoutState(revision: Revision(identifier: "initial"))
432432
let checkoutPath = try workspace.clone(repository: testRepoSpec, at: state)
433433
XCTAssertEqual(try workspace.managedDependencies.dematerialize().values.map{ $0.repository }, [testRepoSpec])
@@ -474,7 +474,7 @@ final class WorkspaceTests: XCTestCase {
474474
]
475475
)
476476
// Create the workspace.
477-
let workspace = try Workspace.createWith(rootPackage: path, manifestLoader: manifestGraph.manifestLoader, delegate: TestWorkspaceDelegate())
477+
let workspace = Workspace.createWith(rootPackage: path, manifestLoader: manifestGraph.manifestLoader, delegate: TestWorkspaceDelegate())
478478
// Load the package graph.
479479
let graph = workspace.loadPackageGraph()
480480
XCTAssertTrue(graph.errors.isEmpty)
@@ -524,7 +524,7 @@ final class WorkspaceTests: XCTestCase {
524524

525525
do {
526526
// Reopen workspace and check if we maintained the state.
527-
let workspace = try Workspace.createWith(rootPackage: path, manifestLoader: manifestGraph.manifestLoader, delegate: TestWorkspaceDelegate())
527+
let workspace = Workspace.createWith(rootPackage: path, manifestLoader: manifestGraph.manifestLoader, delegate: TestWorkspaceDelegate())
528528
let dependency = try workspace.managedDependencies.dematerialize()[RepositorySpecifier(url: aManifest.url)]!
529529
XCTAssert(dependency.state == .edited)
530530
}
@@ -549,7 +549,7 @@ final class WorkspaceTests: XCTestCase {
549549
]
550550
)
551551
// Create the workspace.
552-
let workspace = try Workspace.createWith(rootPackage: path, manifestLoader: manifestGraph.manifestLoader, delegate: TestWorkspaceDelegate())
552+
let workspace = Workspace.createWith(rootPackage: path, manifestLoader: manifestGraph.manifestLoader, delegate: TestWorkspaceDelegate())
553553
// Load the package graph.
554554
let graph = workspace.loadPackageGraph()
555555
XCTAssertTrue(graph.errors.isEmpty)
@@ -601,7 +601,7 @@ final class WorkspaceTests: XCTestCase {
601601
]
602602
)
603603
// Create the workspace.
604-
let workspace = try Workspace.createWith(rootPackage: path, manifestLoader: manifestGraph.manifestLoader, delegate: TestWorkspaceDelegate())
604+
let workspace = Workspace.createWith(rootPackage: path, manifestLoader: manifestGraph.manifestLoader, delegate: TestWorkspaceDelegate())
605605
// Load the package graph.
606606
let graph = workspace.loadPackageGraph()
607607
XCTAssertTrue(graph.errors.isEmpty)
@@ -669,7 +669,7 @@ final class WorkspaceTests: XCTestCase {
669669
let provider = manifestGraph.repoProvider!
670670

671671
func newWorkspace() -> Workspace {
672-
return try! Workspace.createWith(
672+
return Workspace.createWith(
673673
rootPackage: path,
674674
manifestLoader: manifestGraph.manifestLoader,
675675
delegate: TestWorkspaceDelegate(),
@@ -774,7 +774,7 @@ final class WorkspaceTests: XCTestCase {
774774
try aRepo.tag(name: "1.0.1")
775775

776776
func newWorkspace() -> Workspace {
777-
return try! Workspace.createWith(
777+
return Workspace.createWith(
778778
rootPackage: path,
779779
manifestLoader: manifestGraph.manifestLoader,
780780
delegate: TestWorkspaceDelegate(),
@@ -877,7 +877,7 @@ final class WorkspaceTests: XCTestCase {
877877
let provider = manifestGraph.repoProvider!
878878

879879
func newWorkspace() -> Workspace {
880-
return try! Workspace.createWith(
880+
return Workspace.createWith(
881881
rootPackage: path,
882882
manifestLoader: manifestGraph.manifestLoader,
883883
delegate: TestWorkspaceDelegate(),
@@ -962,7 +962,7 @@ final class WorkspaceTests: XCTestCase {
962962
let provider = manifestGraph.repoProvider!
963963

964964
func newWorkspace() -> Workspace {
965-
return try! Workspace.createWith(
965+
return Workspace.createWith(
966966
rootPackage: path,
967967
manifestLoader: manifestGraph.manifestLoader,
968968
delegate: TestWorkspaceDelegate(),
@@ -1018,7 +1018,7 @@ final class WorkspaceTests: XCTestCase {
10181018
try provider.specifierMap[manifestGraph.repo("B")]!.tag(name: "2.0.0")
10191019

10201020
func newWorkspace() -> Workspace {
1021-
return try! Workspace.createWith(
1021+
return Workspace.createWith(
10221022
rootPackage: path,
10231023
manifestLoader: manifestGraph.manifestLoader,
10241024
delegate: TestWorkspaceDelegate(),
@@ -1088,7 +1088,7 @@ final class WorkspaceTests: XCTestCase {
10881088
let provider = manifestGraph.repoProvider!
10891089
try provider.specifierMap[manifestGraph.repo("B")]!.tag(name: "2.0.0")
10901090
func newWorkspace() -> Workspace {
1091-
return try! Workspace.createWith(
1091+
return Workspace.createWith(
10921092
rootPackage: path,
10931093
manifestLoader: manifestGraph.manifestLoader,
10941094
delegate: TestWorkspaceDelegate(),
@@ -1124,7 +1124,7 @@ final class WorkspaceTests: XCTestCase {
11241124
let provider = manifestGraph.repoProvider!
11251125

11261126
func newWorkspace() -> Workspace {
1127-
return try! Workspace.createWith(
1127+
return Workspace.createWith(
11281128
rootPackage: path,
11291129
manifestLoader: manifestGraph.manifestLoader,
11301130
delegate: delegate,
@@ -1234,7 +1234,7 @@ final class WorkspaceTests: XCTestCase {
12341234

12351235

12361236
func getWorkspace() -> Workspace {
1237-
return try! Workspace.createWith(
1237+
return Workspace.createWith(
12381238
rootPackage: root,
12391239
manifestLoader: MockManifestLoader(manifests: manifests))
12401240
}
@@ -1372,7 +1372,7 @@ final class WorkspaceTests: XCTestCase {
13721372

13731373
func createWorkspace() throws -> Workspace {
13741374
let buildPath = path.appending(components: "build")
1375-
return try Workspace(
1375+
return Workspace(
13761376
dataPath: buildPath,
13771377
editablesPath: buildPath.appending(component: "Packages"),
13781378
pinsFile: path.appending(component: "Package.pins"),
@@ -1478,7 +1478,7 @@ final class WorkspaceTests: XCTestCase {
14781478
)
14791479

14801480
let delegate = TestWorkspaceDelegate()
1481-
let workspace = try Workspace.createWith(rootPackage: path, manifestLoader: manifestGraph.manifestLoader, delegate: delegate)
1481+
let workspace = Workspace.createWith(rootPackage: path, manifestLoader: manifestGraph.manifestLoader, delegate: delegate)
14821482
workspace.loadPackageGraph()
14831483

14841484
// Put A in edit mode.
@@ -1514,12 +1514,12 @@ final class WorkspaceTests: XCTestCase {
15141514
)
15151515

15161516
let delegate = TestWorkspaceDelegate()
1517-
func createWorkspace() throws -> Workspace {
1518-
return try Workspace.createWith(rootPackage: path, manifestLoader: manifestGraph.manifestLoader, delegate: delegate)
1517+
func createWorkspace() -> Workspace {
1518+
return Workspace.createWith(rootPackage: path, manifestLoader: manifestGraph.manifestLoader, delegate: delegate)
15191519
}
15201520

15211521
do {
1522-
let workspace = try createWorkspace()
1522+
let workspace = createWorkspace()
15231523
workspace.loadPackageGraph()
15241524
let manifests = try workspace.loadDependencyManifests()
15251525

@@ -1541,7 +1541,7 @@ final class WorkspaceTests: XCTestCase {
15411541

15421542
// Update and check states.
15431543
do {
1544-
let workspace = try createWorkspace()
1544+
let workspace = createWorkspace()
15451545
try workspace.updateDependencies(repin: true)
15461546
let manifests = try workspace.loadDependencyManifests()
15471547

@@ -1581,7 +1581,7 @@ final class WorkspaceTests: XCTestCase {
15811581
let manifestLoader = MockManifestLoader(manifests: manifests)
15821582

15831583
func createWorkspace(_ toolsVersion: ToolsVersion) throws -> Workspace {
1584-
let workspace = try Workspace(
1584+
let workspace = Workspace(
15851585
dataPath: AbsolutePath.root.appending(component: ".build"),
15861586
editablesPath: AbsolutePath.root.appending(component: "Packages"),
15871587
pinsFile: AbsolutePath.root.appending(component: "Package.pins"),
@@ -1648,7 +1648,7 @@ final class WorkspaceTests: XCTestCase {
16481648
stream <<< "let package = Package(name: \"root0\")"
16491649
}
16501650

1651-
let workspace = try Workspace.createWith(rootPackage: roots[0])
1651+
let workspace = Workspace.createWith(rootPackage: roots[0])
16521652
workspace.registerPackage(at: roots[1])
16531653
workspace.registerPackage(at: roots[2])
16541654

@@ -1670,7 +1670,7 @@ final class WorkspaceTests: XCTestCase {
16701670
]
16711671
)
16721672
// Create the workspace.
1673-
let workspace = try Workspace.createWith(
1673+
let workspace = Workspace.createWith(
16741674
rootPackage: path, manifestLoader: manifestGraph.manifestLoader, delegate: TestWorkspaceDelegate())
16751675

16761676
func getDependency(_ manifest: Manifest) -> ManagedDependency {

0 commit comments

Comments
 (0)