Skip to content

Commit 0e77a24

Browse files
committed
Treat package path as authoritative for package init
1 parent 3dd4e0a commit 0e77a24

File tree

3 files changed

+20
-5
lines changed

3 files changed

+20
-5
lines changed

Sources/Commands/PackageTools/Init.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ extension SwiftPackageTool {
5454
@Option(name: .customLong("name"), help: "Provide custom package name")
5555
var packageName: String?
5656

57+
var packagePathIsAuthoritative: Bool {
58+
return true
59+
}
60+
5761
func run(_ swiftTool: SwiftTool) throws {
5862
guard let cwd = swiftTool.fileSystem.currentWorkingDirectory else {
5963
throw InternalError("Could not find the current working directory")

Sources/CoreCommands/SwiftTool.swift

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,21 +81,23 @@ public typealias WorkspaceLoaderProvider = (_ fileSystem: FileSystem, _ observab
8181
-> WorkspaceLoader
8282

8383
public protocol _SwiftCommand {
84+
var globalOptions: GlobalOptions { get }
85+
// Most commands will search upwards from package path until they find a package manifest. Commands which set this to true will instead stick to the defined package path.
86+
var packagePathIsAuthoritative: Bool { get }
8487
var toolWorkspaceConfiguration: ToolWorkspaceConfiguration { get }
8588
var workspaceDelegateProvider: WorkspaceDelegateProvider { get }
8689
var workspaceLoaderProvider: WorkspaceLoaderProvider { get }
8790
func buildSystemProvider(_ swiftTool: SwiftTool) throws -> BuildSystemProvider
8891
}
8992

9093
extension _SwiftCommand {
94+
public var packagePathIsAuthoritative: Bool { false }
9195
public var toolWorkspaceConfiguration: ToolWorkspaceConfiguration {
9296
return .init()
9397
}
9498
}
9599

96100
public protocol SwiftCommand: ParsableCommand, _SwiftCommand {
97-
var globalOptions: GlobalOptions { get }
98-
99101
func run(_ swiftTool: SwiftTool) throws
100102
}
101103

@@ -115,6 +117,7 @@ extension SwiftCommand {
115117
public func run() throws {
116118
let swiftTool = try SwiftTool(
117119
options: globalOptions,
120+
packagePathIsAuthoritative: self.packagePathIsAuthoritative,
118121
toolWorkspaceConfiguration: self.toolWorkspaceConfiguration,
119122
workspaceDelegateProvider: self.workspaceDelegateProvider,
120123
workspaceLoaderProvider: self.workspaceLoaderProvider
@@ -153,8 +156,6 @@ extension SwiftCommand {
153156
}
154157

155158
public protocol AsyncSwiftCommand: AsyncParsableCommand, _SwiftCommand {
156-
var globalOptions: GlobalOptions { get }
157-
158159
func run(_ swiftTool: SwiftTool) async throws
159160
}
160161

@@ -164,6 +165,7 @@ extension AsyncSwiftCommand {
164165
public func run() async throws {
165166
let swiftTool = try SwiftTool(
166167
options: globalOptions,
168+
packagePathIsAuthoritative: self.packagePathIsAuthoritative,
167169
toolWorkspaceConfiguration: self.toolWorkspaceConfiguration,
168170
workspaceDelegateProvider: self.workspaceDelegateProvider,
169171
workspaceLoaderProvider: self.workspaceLoaderProvider
@@ -283,6 +285,7 @@ public final class SwiftTool {
283285
/// - parameter options: The command line options to be passed to this tool.
284286
public convenience init(
285287
options: GlobalOptions,
288+
packagePathIsAuthoritative: Bool,
286289
toolWorkspaceConfiguration: ToolWorkspaceConfiguration = .init(),
287290
workspaceDelegateProvider: @escaping WorkspaceDelegateProvider,
288291
workspaceLoaderProvider: @escaping WorkspaceLoaderProvider
@@ -295,6 +298,7 @@ public final class SwiftTool {
295298
try self.init(
296299
outputStream: TSCBasic.stderrStream,
297300
options: options,
301+
packagePathIsAuthoritative: packagePathIsAuthoritative,
298302
toolWorkspaceConfiguration: toolWorkspaceConfiguration,
299303
workspaceDelegateProvider: workspaceDelegateProvider,
300304
workspaceLoaderProvider: workspaceLoaderProvider
@@ -305,6 +309,7 @@ public final class SwiftTool {
305309
internal init(
306310
outputStream: OutputByteStream,
307311
options: GlobalOptions,
312+
packagePathIsAuthoritative: Bool,
308313
toolWorkspaceConfiguration: ToolWorkspaceConfiguration,
309314
workspaceDelegateProvider: @escaping WorkspaceDelegateProvider,
310315
workspaceLoaderProvider: @escaping WorkspaceLoaderProvider
@@ -348,7 +353,12 @@ public final class SwiftTool {
348353
}
349354

350355
// Create local variables to use while finding build path to avoid capture self before init error.
351-
let packageRoot = findPackageRoot(fileSystem: fileSystem)
356+
let packageRoot: AbsolutePath?
357+
if packagePathIsAuthoritative {
358+
packageRoot = fileSystem.currentWorkingDirectory
359+
} else {
360+
packageRoot = findPackageRoot(fileSystem: fileSystem)
361+
}
352362

353363
self.packageRoot = packageRoot
354364
self.scratchDirectory =

Tests/CommandsTests/SwiftToolTests.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,7 @@ extension SwiftTool {
325325
return try SwiftTool(
326326
outputStream: outputStream,
327327
options: options,
328+
packagePathIsAuthoritative: false,
328329
toolWorkspaceConfiguration: .init(shouldInstallSignalHandlers: false),
329330
workspaceDelegateProvider: {
330331
ToolWorkspaceDelegate(

0 commit comments

Comments
 (0)