Skip to content

Commit b03e18d

Browse files
committed
[Commands] Return the same workspace object in getActiveWorkspace()
getActiveWorkspace() shouldn't recreate workspace objects and should return the same object on consecutive calls.
1 parent 943dd90 commit b03e18d

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

Sources/Commands/SwiftTool.swift

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,21 @@ public class SwiftTool<Mode: Argument, OptionType: Options> {
9494
fatalError("Must be implmented by subclasses")
9595
}
9696

97+
/// Holds the currently active workspace.
98+
///
99+
/// It is not initialized in init() because for some of the commands like package init , usage etc,
100+
/// workspace is not needed, infact it would be an error to ask for the workspace object
101+
/// for package init because the Manifest file should *not* present.
102+
private var _workspace: Workspace? = nil
103+
97104
/// Returns the currently active workspace.
98105
func getActiveWorkspace() throws -> Workspace {
99-
// Get the active workspace.
106+
if let workspace = _workspace {
107+
return workspace
108+
}
100109
let delegate = ToolWorkspaceDelegate()
101-
return try Workspace(rootPackage: try getPackageRoot(), dataPath: buildPath, manifestLoader: manifestLoader, delegate: delegate)
110+
_workspace = try Workspace(rootPackage: try getPackageRoot(), dataPath: buildPath, manifestLoader: manifestLoader, delegate: delegate)
111+
return _workspace!
102112
}
103113

104114
/// Execute the tool.

0 commit comments

Comments
 (0)