Skip to content

Support building swift-format using SwiftPM for toolchain builds on Windows #841

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 30 additions & 14 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,22 +47,22 @@ let package = Package(

.target(
name: "SwiftFormat",
dependencies: [
dependencies: omittingExternalDependenciesIfNecessary([
.product(name: "Markdown", package: "swift-markdown"),
.product(name: "SwiftSyntax", package: "swift-syntax"),
.product(name: "SwiftSyntaxBuilder", package: "swift-syntax"),
.product(name: "SwiftOperators", package: "swift-syntax"),
.product(name: "SwiftParser", package: "swift-syntax"),
.product(name: "SwiftParserDiagnostics", package: "swift-syntax"),
],
]),
exclude: ["CMakeLists.txt"]
),
.target(
name: "_SwiftFormatTestSupport",
dependencies: [
dependencies: omittingExternalDependenciesIfNecessary([
"SwiftFormat",
.product(name: "SwiftOperators", package: "swift-syntax"),
]
])
),
.plugin(
name: "Format Source Code",
Expand Down Expand Up @@ -93,44 +93,42 @@ let package = Package(
.executableTarget(
name: "generate-swift-format",
dependencies: [
"SwiftFormat",
.product(name: "SwiftSyntax", package: "swift-syntax"),
.product(name: "SwiftParser", package: "swift-syntax"),
"SwiftFormat"
]
),
.executableTarget(
name: "swift-format",
dependencies: [
dependencies: omittingExternalDependenciesIfNecessary([
"_SwiftFormatInstructionCounter",
"SwiftFormat",
.product(name: "ArgumentParser", package: "swift-argument-parser"),
.product(name: "SwiftSyntax", package: "swift-syntax"),
.product(name: "SwiftParser", package: "swift-syntax"),
],
]),
exclude: ["CMakeLists.txt"],
linkerSettings: swiftformatLinkSettings
),

.testTarget(
name: "SwiftFormatPerformanceTests",
dependencies: [
dependencies: omittingExternalDependenciesIfNecessary([
"SwiftFormat",
"_SwiftFormatTestSupport",
.product(name: "SwiftSyntax", package: "swift-syntax"),
.product(name: "SwiftParser", package: "swift-syntax"),
]
])
),
.testTarget(
name: "SwiftFormatTests",
dependencies: [
dependencies: omittingExternalDependenciesIfNecessary([
"SwiftFormat",
"_SwiftFormatTestSupport",
.product(name: "Markdown", package: "swift-markdown"),
.product(name: "SwiftOperators", package: "swift-syntax"),
.product(name: "SwiftParser", package: "swift-syntax"),
.product(name: "SwiftSyntax", package: "swift-syntax"),
.product(name: "SwiftSyntaxBuilder", package: "swift-syntax"),
]
])
),
]
)
Expand All @@ -149,10 +147,28 @@ var installAction: Bool { hasEnvironmentVariable("SWIFTFORMAT_CI_INSTALL") }
/// remote dependency.
var useLocalDependencies: Bool { hasEnvironmentVariable("SWIFTCI_USE_LOCAL_DEPS") }

var omitExternalDependencies: Bool { hasEnvironmentVariable("SWIFTFORMAT_OMIT_EXTERNAL_DEPENDENCIES") }

func omittingExternalDependenciesIfNecessary(
_ dependencies: [Target.Dependency]
) -> [Target.Dependency] {
guard omitExternalDependencies else {
return dependencies
}
return dependencies.filter { dependency in
if case .productItem(_, let package, _, _) = dependency {
return package == nil
}
return true
}
}

// MARK: - Dependencies

var dependencies: [Package.Dependency] {
if useLocalDependencies {
if omitExternalDependencies {
return []
} else if useLocalDependencies {
return [
.package(path: "../swift-argument-parser"),
.package(path: "../swift-markdown"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,9 @@ public final class NoEmptyLinesOpeningClosingBraces: SyntaxFormatRule {
let (trimmedLeadingTrivia, count) = first.leadingTrivia.trimmingSuperfluousNewlines()
if trimmedLeadingTrivia.sourceLength != first.leadingTriviaLength {
diagnose(.removeEmptyLinesAfter(count), on: first, anchor: .leadingTrivia(0))
result[index] = first.with(\.leadingTrivia, trimmedLeadingTrivia)
var first = first
first.leadingTrivia = trimmedLeadingTrivia
result[index] = first
Comment on lines +89 to +91
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven’t had a chance to reduce the failure but I think test execution crashes with the previous code due to swiftlang/swift#67676.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is fine anyway, I've come to mildly disfavor the bespoke with methods and just use normal value type mutation for operations like this.

}
}
return rewrite(result).as(C.self)!
Expand Down