Skip to content

Commit fc2121e

Browse files
committed
refactoring of relative path to allow late stage canonicalization in support of windows
1 parent 74faa6b commit fc2121e

32 files changed

+146
-134
lines changed

Package.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -609,7 +609,8 @@ if ProcessInfo.processInfo.environment["SWIFTPM_LLBUILD_FWK"] == nil {
609609

610610
if ProcessInfo.processInfo.environment["SWIFTCI_USE_LOCAL_DEPS"] == nil {
611611
package.dependencies += [
612-
.package(url: "https://github.com/apple/swift-tools-support-core.git", .branch(relatedDependenciesBranch)),
612+
//.package(url: "https://github.com/apple/swift-tools-support-core.git", .branch(relatedDependenciesBranch)),
613+
.package(path: "../swift-tools-support-core"),
613614
// The 'swift-argument-parser' version declared here must match that
614615
// used by 'swift-driver' and 'sourcekit-lsp'. Please coordinate
615616
// dependency version changes here with those projects.

Sources/Build/BuildOperationBuildSystemDelegateHandler.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -351,8 +351,8 @@ public struct BuildDescription: Codable {
351351
.map { $0.c99name })
352352
self.swiftTargetScanArgs = targetCommandLines
353353
self.generatedSourceTargetSet = Set(generatedSourceTargets)
354-
self.builtTestProducts = plan.buildProducts.filter{ $0.product.type == .test }.map { desc in
355-
return BuiltTestProduct(
354+
self.builtTestProducts = try plan.buildProducts.filter{ $0.product.type == .test }.map { desc in
355+
try BuiltTestProduct(
356356
productName: desc.product.name,
357357
binaryPath: desc.binaryPath
358358
)

Sources/Build/BuildPlan.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -507,7 +507,7 @@ public final class ClangTargetBuildDescription {
507507
}
508508
"""
509509

510-
let implFileSubpath = RelativePath("resource_bundle_accessor.m")
510+
let implFileSubpath = try RelativePath(validating: "resource_bundle_accessor.m")
511511

512512
// Add the file to the derived sources.
513513
derivedSources.relativePaths.append(implFileSubpath)
@@ -840,7 +840,7 @@ public final class SwiftTargetBuildDescription {
840840
}
841841
"""
842842

843-
let subpath = RelativePath("resource_bundle_accessor.swift")
843+
let subpath = try RelativePath(validating: "resource_bundle_accessor.swift")
844844

845845
// Add the file to the derived sources.
846846
derivedSources.relativePaths.append(subpath)
@@ -1372,12 +1372,12 @@ public final class ProductBuildDescription: SPMBuildCore.ProductBuildDescription
13721372
let librarian = buildParameters.toolchain.librarianPath.pathString
13731373
let triple = buildParameters.triple
13741374
if triple.isWindows(), librarian.hasSuffix("link") || librarian.hasSuffix("link.exe") {
1375-
return [librarian, "/LIB", "/OUT:\(binaryPath.pathString)", "@\(linkFileListPath.pathString)"]
1375+
return try [librarian, "/LIB", "/OUT:\(self.binaryPath.pathString)", "@\(linkFileListPath.pathString)"]
13761376
}
13771377
if triple.isDarwin(), librarian.hasSuffix("libtool") {
1378-
return [librarian, "-o", binaryPath.pathString, "@\(linkFileListPath.pathString)"]
1378+
return try [librarian, "-o", self.binaryPath.pathString, "@\(linkFileListPath.pathString)"]
13791379
}
1380-
return [librarian, "crs", binaryPath.pathString, "@\(linkFileListPath.pathString)"]
1380+
return try [librarian, "crs", self.binaryPath.pathString, "@\(linkFileListPath.pathString)"]
13811381
}
13821382

13831383
/// The arguments to link and create this product.
@@ -1401,7 +1401,7 @@ public final class ProductBuildDescription: SPMBuildCore.ProductBuildDescription
14011401
}
14021402

14031403
args += ["-L", buildParameters.buildPath.pathString]
1404-
args += ["-o", binaryPath.pathString]
1404+
args += try ["-o", self.binaryPath.pathString]
14051405
args += ["-module-name", product.name.spm_mangledToC99ExtendedIdentifier()]
14061406
args += dylibs.map({ "-l" + $0.product.name })
14071407

@@ -1430,7 +1430,7 @@ public final class ProductBuildDescription: SPMBuildCore.ProductBuildDescription
14301430
case .library(.dynamic):
14311431
args += ["-emit-library"]
14321432
if buildParameters.triple.isDarwin() {
1433-
let relativePath = "@rpath/\(buildParameters.binaryRelativePath(for: product).pathString)"
1433+
let relativePath = try "@rpath/\(buildParameters.binaryRelativePath(for: product).pathString)"
14341434
args += ["-Xlinker", "-install_name", "-Xlinker", relativePath]
14351435
}
14361436
args += deadStripArguments

Sources/Build/LLBuildManifestBuilder.swift

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ extension LLBuildManifestBuilder {
170170

171171
var outputs: [Node] = []
172172

173-
let infoPlistDestination = RelativePath("Info.plist")
173+
let infoPlistDestination = try! RelativePath(validating: "Info.plist") //try! safe
174174

175175
// Create a copy command for each resource file.
176176
for resource in target.resources {
@@ -560,7 +560,7 @@ extension LLBuildManifestBuilder {
560560
guard let planProduct = plan.productMap[product] else {
561561
throw InternalError("unknown product \(product)")
562562
}
563-
inputs.append(file: planProduct.binaryPath)
563+
try inputs.append(file: planProduct.binaryPath)
564564
}
565565
return
566566
}
@@ -589,7 +589,7 @@ extension LLBuildManifestBuilder {
589589
throw InternalError("unknown product \(product)")
590590
}
591591
// Establish a dependency on binary of the product.
592-
inputs.append(file: planProduct.binaryPath)
592+
try inputs.append(file: planProduct.binaryPath)
593593

594594
// For automatic and static libraries, and plugins, add their targets as static input.
595595
case .library(.automatic), .library(.static), .plugin:
@@ -743,7 +743,7 @@ extension LLBuildManifestBuilder {
743743
throw InternalError("unknown product \(product)")
744744
}
745745
// Establish a dependency on binary of the product.
746-
let binary = planProduct.binaryPath
746+
let binary = try planProduct.binaryPath
747747
inputs.append(file: binary)
748748

749749
case .library(.automatic), .library(.static), .plugin:
@@ -893,18 +893,18 @@ extension LLBuildManifestBuilder {
893893

894894
switch buildProduct.product.type {
895895
case .library(.static):
896-
manifest.addShellCmd(
896+
try manifest.addShellCmd(
897897
name: cmdName,
898898
description: "Archiving \(buildProduct.binaryPath.prettyPath())",
899899
inputs: buildProduct.objects.map(Node.file),
900900
outputs: [.file(buildProduct.binaryPath)],
901-
arguments: try buildProduct.archiveArguments()
901+
arguments: buildProduct.archiveArguments()
902902
)
903903

904904
default:
905-
let inputs = buildProduct.objects + buildProduct.dylibs.map({ $0.binaryPath })
905+
let inputs = try buildProduct.objects + buildProduct.dylibs.map{ try $0.binaryPath }
906906

907-
manifest.addShellCmd(
907+
try manifest.addShellCmd(
908908
name: cmdName,
909909
description: "Linking \(buildProduct.binaryPath.prettyPath())",
910910
inputs: inputs.map(Node.file),
@@ -918,7 +918,7 @@ extension LLBuildManifestBuilder {
918918
let output: Node = .virtual(targetName)
919919

920920
manifest.addNode(output, toTarget: targetName)
921-
manifest.addPhonyCmd(
921+
try manifest.addPhonyCmd(
922922
name: output.name,
923923
inputs: [.file(buildProduct.binaryPath)],
924924
outputs: [output]

Sources/Commands/PackageTools/PluginCommand.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ struct PluginCommand: SwiftCommand {
168168
// Build the product referenced by the tool, and add the executable to the tool map. Product dependencies are not supported within a package, so if the tool happens to be from the same package, we instead find the executable that corresponds to the product. There is always one, because of autogeneration of implicit executables with the same name as the target if there isn't an explicit one.
169169
try buildSystem.build(subset: .product(name))
170170
if let builtTool = try buildSystem.buildPlan.buildProducts.first(where: { $0.product.name == name}) {
171-
toolNamesToPaths[name] = builtTool.binaryPath
171+
try toolNamesToPaths[name] = builtTool.binaryPath
172172
}
173173
case .vendedTool(let name, let path):
174174
toolNamesToPaths[name] = path

Sources/Commands/SwiftRunTool.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,11 @@ public struct SwiftRunTool: SwiftCommand {
275275
guard let cwd = fileSystem.currentWorkingDirectory else {
276276
return false
277277
}
278-
absolutePath = AbsolutePath(cwd, path)
278+
do {
279+
absolutePath = try AbsolutePath(cwd, validating: path)
280+
} catch {
281+
return false
282+
}
279283
}
280284
return fileSystem.isFile(absolutePath)
281285
}

Sources/Commands/Utilities/PluginDelegate.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,15 +129,15 @@ final class PluginDelegate: PluginInvocationDelegate {
129129
return $0.product.name == name
130130
}
131131
}
132-
let builtArtifacts: [PluginInvocationBuildResult.BuiltArtifact] = builtProducts.compactMap {
132+
let builtArtifacts: [PluginInvocationBuildResult.BuiltArtifact] = try builtProducts.compactMap {
133133
switch $0.product.type {
134134
case .library(let kind):
135-
return .init(
135+
return try .init(
136136
path: $0.binaryPath.pathString,
137137
kind: (kind == .dynamic) ? .dynamicLibrary : .staticLibrary
138138
)
139139
case .executable:
140-
return .init(path: $0.binaryPath.pathString, kind: .executable)
140+
return try .init(path: $0.binaryPath.pathString, kind: .executable)
141141
default:
142142
return nil
143143
}

Sources/PackageLoading/TargetSourcesBuilder.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -345,8 +345,9 @@ public struct TargetSourcesBuilder {
345345
}
346346

347347
private func diagnoseInfoPlistConflicts(in resources: [Resource]) {
348+
let infoPlist = try! RelativePath(validating: "Info.plist")
348349
for resource in resources {
349-
if resource.destination == RelativePath("Info.plist") {
350+
if resource.destination == infoPlist {
350351
self.observabilityScope.emit(.infoPlistResourceConflict(
351352
path: resource.path.relative(to: targetPath),
352353
targetName: target.name))

Sources/PackageModel/Resource.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ public struct Resource: Codable, Equatable {
2626
public var destination: RelativePath {
2727
switch self.rule {
2828
case .process(.some(let localization)):
29-
return RelativePath("\(localization).\(Self.localizationDirectoryExtension)/\(path.basename)")
29+
return try! RelativePath(validating: "\(localization).\(Self.localizationDirectoryExtension)/\(path.basename)") // try! safe
3030
default:
31-
return RelativePath(path.basename)
31+
return try! RelativePath(validating: path.basename) // try! safe
3232
}
3333
}
3434

Sources/PackageModel/ToolsVersion.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,9 +138,9 @@ public struct ToolsVersion: Equatable, Hashable, Codable {
138138
/// The subpath to the PackageDescription runtime library.
139139
public var runtimeSubpath: RelativePath {
140140
if self < .v4_2 {
141-
return RelativePath("4")
141+
return try! RelativePath(validating: "4")
142142
}
143-
return RelativePath("4_2")
143+
return try! RelativePath(validating: "4_2")
144144
}
145145

146146
/// The swift language version based on this tools version.

Sources/PackageModel/UserToolchain.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public final class UserToolchain: Toolchain {
7070
let swiftCompiler = try resolveSymlinks(self.swiftCompilerPath)
7171

7272
let runtime = swiftCompiler.appending(
73-
RelativePath("../../lib/swift/clang/lib/darwin/libclang_rt.\(sanitizer.shortName)_osx_dynamic.dylib"))
73+
try RelativePath(validating: "../../lib/swift/clang/lib/darwin/libclang_rt.\(sanitizer.shortName)_osx_dynamic.dylib"))
7474

7575
// Ensure that the runtime is present.
7676
guard localFileSystem.exists(runtime) else {

Sources/PackageRegistry/RegistryDownloadsManager.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ extension PackageIdentity {
308308
guard let (scope, name) = self.scopeAndName else {
309309
throw StringError("invalid package identity, expected registry scope and name")
310310
}
311-
return RelativePath(scope.description).appending(component: name.description)
311+
return try RelativePath(validating: scope.description).appending(component: name.description)
312312
}
313313

314314
internal func downloadPath(version: Version) throws -> RelativePath {

Sources/SPMBuildCore/BuildParameters.swift

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -343,31 +343,31 @@ public struct BuildParameters: Encodable {
343343
}
344344

345345
/// Returns the path to the binary of a product for the current build parameters.
346-
public func binaryPath(for product: ResolvedProduct) -> AbsolutePath {
347-
return buildPath.appending(binaryRelativePath(for: product))
346+
public func binaryPath(for product: ResolvedProduct) throws -> AbsolutePath {
347+
try buildPath.appending(self.binaryRelativePath(for: product))
348348
}
349349

350350
/// Returns the path to the binary of a product for the current build parameters, relative to the build directory.
351-
public func binaryRelativePath(for product: ResolvedProduct) -> RelativePath {
351+
public func binaryRelativePath(for product: ResolvedProduct) throws -> RelativePath {
352352
switch product.type {
353353
case .executable, .snippet:
354-
return RelativePath("\(product.name)\(triple.executableExtension)")
354+
return try RelativePath(validating: "\(product.name)\(triple.executableExtension)")
355355
case .library(.static):
356-
return RelativePath("lib\(product.name)\(triple.staticLibraryExtension)")
356+
return try RelativePath(validating: "lib\(product.name)\(triple.staticLibraryExtension)")
357357
case .library(.dynamic):
358-
return RelativePath("\(triple.dynamicLibraryPrefix)\(product.name)\(triple.dynamicLibraryExtension)")
358+
return try RelativePath(validating: "\(triple.dynamicLibraryPrefix)\(product.name)\(triple.dynamicLibraryExtension)")
359359
case .library(.automatic), .plugin:
360360
fatalError()
361361
case .test:
362362
guard !triple.isWASI() else {
363-
return RelativePath("\(product.name).wasm")
363+
return try RelativePath(validating: "\(product.name).wasm")
364364
}
365365

366366
let base = "\(product.name).xctest"
367367
if triple.isDarwin() {
368-
return RelativePath("\(base)/Contents/MacOS/\(product.name)")
368+
return try RelativePath(validating: "\(base)/Contents/MacOS/\(product.name)")
369369
} else {
370-
return RelativePath(base)
370+
return try RelativePath(validating: base)
371371
}
372372
}
373373
}

Sources/SPMBuildCore/BuildSystem.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,9 @@ public protocol ProductBuildDescription {
7474
extension ProductBuildDescription {
7575
/// The path to the product binary produced.
7676
public var binaryPath: AbsolutePath {
77-
return buildParameters.binaryPath(for: product)
77+
get throws {
78+
try buildParameters.binaryPath(for: product)
79+
}
7880
}
7981
}
8082

Sources/SPMBuildCore/PluginInvocation.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -526,7 +526,7 @@ public extension PluginTarget {
526526
}
527527
// For an executable target we create a `builtTool`.
528528
else if executableOrBinaryTarget.type == .executable {
529-
return [.builtTool(name: builtToolName, path: RelativePath(executableOrBinaryTarget.name))]
529+
return try [.builtTool(name: builtToolName, path: RelativePath(validating: executableOrBinaryTarget.name))]
530530
}
531531
else {
532532
return []

Sources/SPMTestSupport/MockDependency.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,19 +76,19 @@ public struct MockDependency {
7676
}
7777

7878
public static func fileSystem(path: String, products: ProductFilter = .everything) -> MockDependency {
79-
MockDependency(location: .fileSystem(path: RelativePath(path)), products: products)
79+
try! MockDependency(location: .fileSystem(path: RelativePath(validating: path)), products: products)
8080
}
8181

8282
public static func sourceControl(path: String, requirement: SourceControlRequirement, products: ProductFilter = .everything) -> MockDependency {
83-
.sourceControl(path: RelativePath(path), requirement: requirement, products: products)
83+
try! .sourceControl(path: RelativePath(validating: path), requirement: requirement, products: products)
8484
}
8585

8686
public static func sourceControl(path: RelativePath, requirement: SourceControlRequirement, products: ProductFilter = .everything) -> MockDependency {
8787
MockDependency(location: .localSourceControl(path: path, requirement: requirement), products: products)
8888
}
8989

9090
public static func sourceControlWithDeprecatedName(name: String, path: String, requirement: SourceControlRequirement, products: ProductFilter = .everything) -> MockDependency {
91-
MockDependency(deprecatedName: name, location: .localSourceControl(path: RelativePath(path), requirement: requirement), products: products)
91+
try! MockDependency(deprecatedName: name, location: .localSourceControl(path: RelativePath(validating: path), requirement: requirement), products: products)
9292
}
9393

9494
public static func sourceControl(url: String, requirement: SourceControlRequirement, products: ProductFilter = .everything) -> MockDependency {

Sources/SPMTestSupport/MockPackage.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,10 @@ public struct MockPackage {
3838
revisionProvider: ((String) -> String)? = nil,
3939
toolsVersion: ToolsVersion? = nil
4040
) {
41+
let path = try! RelativePath(validating: path ?? name)
4142
self.name = name
4243
self.platforms = platforms
43-
self.location = .fileSystem(path: RelativePath(path ?? name))
44+
self.location = .fileSystem(path: path)
4445
self.targets = targets
4546
self.products = products
4647
self.dependencies = dependencies

Sources/SPMTestSupport/SwiftPMProduct.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,17 @@ public enum SwiftPMProduct: Product {
2626
public var exec: RelativePath {
2727
switch self {
2828
case .SwiftBuild:
29-
return RelativePath("swift-build")
29+
return try! RelativePath(validating: "swift-build") //try! safe
3030
case .SwiftPackage:
31-
return RelativePath("swift-package")
31+
return try! RelativePath(validating: "swift-package") //try! safe
3232
case .SwiftPackageRegistry:
33-
return RelativePath("swift-package-registry")
33+
return try! RelativePath(validating: "swift-package-registry") //try! safe
3434
case .SwiftTest:
35-
return RelativePath("swift-test")
35+
return try! RelativePath(validating: "swift-test") //try! safe
3636
case .SwiftRun:
37-
return RelativePath("swift-run")
37+
return try! RelativePath(validating: "swift-run") //try! safe
3838
case .XCTestHelper:
39-
return RelativePath("swiftpm-xctest-helper")
39+
return try! RelativePath(validating: "swiftpm-xctest-helper") //try! safe
4040
}
4141
}
4242
}

Sources/SPMTestSupport/misc.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public func fixture(
4343
) throws {
4444
do {
4545
// Make a suitable test directory name from the fixture subpath.
46-
let fixtureSubpath = RelativePath(name)
46+
let fixtureSubpath = try RelativePath(validating: name)
4747
let copyName = fixtureSubpath.components.joined(separator: "_")
4848

4949
// Create a temporary directory for the duration of the block.

0 commit comments

Comments
 (0)