-
Notifications
You must be signed in to change notification settings - Fork 1.4k
[Commands] Prototype swift-fixit
— a subcommand for deserializing diagnostics and applying fix-its
#8576
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
Open
AnthonyLatsis
wants to merge
2
commits into
swiftlang:main
Choose a base branch
from
AnthonyLatsis:danaus-plexippus-6
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
[Commands] Prototype swift-fixit
— a subcommand for deserializing diagnostics and applying fix-its
#8576
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
// swift-tools-version:5.9 | ||
|
||
import PackageDescription | ||
|
||
let package = Package( | ||
name: "SwiftFixItPackage", | ||
targets: [ | ||
.target(name: "Diagnostics", path: "Sources", exclude: ["Fixed"]), | ||
] | ||
) |
12 changes: 12 additions & 0 deletions
12
Fixtures/SwiftFixIt/SwiftFixItPackage/Sources/Fixed/first.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
func foo(_: any P, _: any P) {} | ||
|
||
func throwing() throws -> Int {} | ||
|
||
func foo() throws { | ||
do { | ||
_ = 0 | ||
} | ||
do { | ||
_ = try throwing() | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
Fixtures/SwiftFixIt/SwiftFixItPackage/Sources/Fixed/second.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
protocol P { | ||
associatedtype A | ||
} | ||
|
||
func bar(_: any P) {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
func foo(_: P, _: P) {} | ||
|
||
func throwing() throws -> Int {} | ||
|
||
func foo() throws { | ||
do { | ||
var x = 0 | ||
} | ||
do { | ||
let x = throwing() | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
protocol P { | ||
associatedtype A | ||
} | ||
|
||
func bar(_: P) {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the Swift open source project | ||
// | ||
// Copyright (c) 2025 Apple Inc. and the Swift project authors | ||
// Licensed under Apache License v2.0 with Runtime Library Exception | ||
// | ||
// See http://swift.org/LICENSE.txt for license information | ||
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
import struct ArgumentParser.Argument | ||
import protocol ArgumentParser.AsyncParsableCommand | ||
import struct ArgumentParser.CommandConfiguration | ||
import struct ArgumentParser.OptionGroup | ||
import protocol ArgumentParser.ParsableArguments | ||
|
||
import struct Basics.AbsolutePath | ||
import var Basics.localFileSystem | ||
import struct Basics.SwiftVersion | ||
|
||
import struct CoreCommands.LoggingOptions | ||
|
||
import struct SwiftFixIt.SwiftFixIt | ||
|
||
private struct Options: ParsableArguments { | ||
@OptionGroup(title: "Logging") | ||
var logging: LoggingOptions | ||
|
||
@Argument( | ||
help: "", | ||
completion: .file(extensions: [".dia"]) | ||
) | ||
var diagnosticFiles: [AbsolutePath] = [] | ||
} | ||
|
||
/// Deserializes `.dia` files and applies all fix-its in place. | ||
package struct SwiftFixitCommand: AsyncParsableCommand { | ||
package static var configuration = CommandConfiguration( | ||
commandName: "fixit", | ||
_superCommandName: "swift", | ||
abstract: "Deserialize diagnostics and apply fix-its", | ||
version: SwiftVersion.current.completeDisplayString, | ||
helpNames: [ | ||
.short, | ||
.long, | ||
.customLong("help", withSingleDash: true), | ||
] | ||
) | ||
|
||
@OptionGroup | ||
fileprivate var options: Options | ||
|
||
package init() {} | ||
|
||
package func run() async throws { | ||
if self.options.diagnosticFiles.isEmpty { | ||
return | ||
} | ||
|
||
let swiftFixIt = try SwiftFixIt(diagnosticFiles: options.diagnosticFiles, fileSystem: localFileSystem) | ||
|
||
try swiftFixIt.applyFixIts() | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# This source file is part of the Swift open source project | ||
# | ||
# Copyright (c) 2025 Apple Inc. and the Swift project authors | ||
# Licensed under Apache License v2.0 with Runtime Library Exception | ||
# | ||
# See http://swift.org/LICENSE.txt for license information | ||
# See http://swift.org/CONTRIBUTORS.txt for Swift project authors | ||
|
||
add_executable(swift-fixit | ||
Entrypoint.swift) | ||
target_link_libraries(swift-test PRIVATE | ||
Commands) | ||
|
||
target_compile_options(swift-test PRIVATE | ||
-parse-as-library) | ||
|
||
install(TARGETS swift-fixit | ||
RUNTIME DESTINATION bin) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the Swift open source project | ||
// | ||
// Copyright (c) 2025 Apple Inc. and the Swift project authors | ||
// Licensed under Apache License v2.0 with Runtime Library Exception | ||
// | ||
// See http://swift.org/LICENSE.txt for license information | ||
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
import Commands | ||
|
||
@main | ||
struct Entrypoint { | ||
static func main() async { | ||
await SwiftFixitCommand.main() | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the Swift open source project | ||
// | ||
// Copyright (c) 2025 Apple Inc. and the Swift project authors | ||
// Licensed under Apache License v2.0 with Runtime Library Exception | ||
// | ||
// See http://swift.org/LICENSE.txt for license information | ||
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
import _InternalTestSupport | ||
import struct Basics.AbsolutePath | ||
import var Basics.localFileSystem | ||
@testable | ||
import Commands | ||
import class PackageModel.UserToolchain | ||
import XCTest | ||
|
||
final class FixItCommandTests: CommandsTestCase { | ||
func testHelp() async throws { | ||
let stdout = try await SwiftPM.fixit.execute(["-help"]).stdout | ||
|
||
XCTAssert(stdout.contains("USAGE: swift fixit"), stdout) | ||
XCTAssert(stdout.contains("-h, -help, --help"), stdout) | ||
} | ||
|
||
func testApplyFixIts() async throws { | ||
try await fixture(name: "SwiftFixIt/SwiftFixItPackage") { fixturePath in | ||
let sourcePaths: [AbsolutePath] | ||
let fixedSourcePaths: [AbsolutePath] | ||
do { | ||
let sourcesPath = fixturePath.appending(components: "Sources") | ||
let fixedSourcesPath = sourcesPath.appending("Fixed") | ||
|
||
sourcePaths = try localFileSystem.getDirectoryContents(sourcesPath).filter { filename in | ||
filename.hasSuffix(".swift") | ||
}.sorted().map { filename in | ||
sourcesPath.appending(filename) | ||
} | ||
fixedSourcePaths = try localFileSystem.getDirectoryContents(fixedSourcesPath).filter { filename in | ||
filename.hasSuffix(".swift") | ||
}.sorted().map { filename in | ||
fixedSourcesPath.appending(filename) | ||
} | ||
} | ||
|
||
XCTAssertEqual(sourcePaths.count, fixedSourcePaths.count) | ||
|
||
let targetName = "Diagnostics" | ||
|
||
_ = try? await executeSwiftBuild(fixturePath, extraArgs: ["--target", targetName]) | ||
|
||
do { | ||
let artifactsPath = try fixturePath.appending( | ||
components: ".build", | ||
UserToolchain.default.targetTriple.platformBuildPathComponent, | ||
"debug", | ||
"\(targetName).build" | ||
) | ||
let diaFilePaths = try localFileSystem.getDirectoryContents(artifactsPath).filter { filename in | ||
// Ignore "*.emit-module.dia". | ||
filename.split(".").1 == "dia" | ||
}.map { filename in | ||
artifactsPath.appending(component: filename).pathString | ||
} | ||
|
||
XCTAssertEqual(sourcePaths.count, diaFilePaths.count) | ||
|
||
_ = try await SwiftPM.fixit.execute(diaFilePaths) | ||
} | ||
|
||
for (sourcePath, fixedSourcePath) in zip(sourcePaths, fixedSourcePaths) { | ||
try XCTAssertEqual( | ||
localFileSystem.readFileContents(sourcePath), | ||
localFileSystem.readFileContents(fixedSourcePath) | ||
) | ||
} | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.