Skip to content
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

Return a non-zero exit code when SwiftFormat autocorrect applies code changes #268

Closed
wants to merge 1 commit into from

Conversation

calda
Copy link
Member

@calda calda commented Apr 16, 2024

This PR updates the SPM package plugin to return a non-zero exit code when SwiftFormat autocorrect applies code changes, using the new --strict flag added in nicklockwood/SwiftFormat#1676.

This matches the behavior of our internal linting tool.

@@ -42,8 +42,8 @@ let package = Package(

.binaryTarget(
name: "SwiftFormat",
url: "https://github.com/calda/SwiftFormat/releases/download/0.54-beta-5/SwiftFormat.artifactbundle.zip",
checksum: "7447986db45a51164d23672c07f971406a4c0589b0c423fcb85e95ed8f8e7e48"),
url: "https://github.com/calda/SwiftFormat/releases/download/0.54-beta-6/SwiftFormat.artifactbundle.zip",
Copy link
Contributor

Choose a reason for hiding this comment

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

I checked that this release includes the --strict change.

url: "https://github.com/calda/SwiftFormat/releases/download/0.54-beta-5/SwiftFormat.artifactbundle.zip",
checksum: "7447986db45a51164d23672c07f971406a4c0589b0c423fcb85e95ed8f8e7e48"),
url: "https://github.com/calda/SwiftFormat/releases/download/0.54-beta-6/SwiftFormat.artifactbundle.zip",
checksum: "2b7213c898f0914713b5d8a75e4ed7c560552f3a9e69f31dbcd38c99b878d738"),
Copy link
Contributor

Choose a reason for hiding this comment

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

@calda how do you find/compute this value? Asking to learn.

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 usually:

  1. Update the url to point to the new value
  2. Run rm -rf .build && swift package format
  3. SPM will print out an error that includes the old checksum and the new checksum
  4. Copy the new checksum into the package manifest

There's also a command to calculate this yourself but I don't know it off the top of my head so use this lazy method instead 😛

Copy link
Contributor

Choose a reason for hiding this comment

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

in case it's helpful: if you have the zip locally, you can always run xcrun swift package compute-checksum <path_to_zip_file>

Copy link
Contributor

@bachand bachand left a comment

Choose a reason for hiding this comment

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

@calda thanks for continuing to improve this tool! I was about to approve this PR but now I am realizing that I am unclear on what the behavior of our tool should be. Should the tool fail or succeed if there were violations that were automatically corrected? My understanding was that we wanted the tool to succeed if the violations were automatically corrected, which is the current behavior when running SwiftLint (see testFormatWithOnlySwiftLintAutocorrectedViolation()).

I am also recalling that we had a similar discussion previously in #242 (comment). In that discussion we aligned that we did not want the tool to fail with a nonzero exit code when errors could be automatically corrected. It seems like we are changing that behavior now, but only for SwiftFormat?

In that previous discussion I suggested:

What do you think about documenting the exit code behavior of this tool in the README as part of this PR and then we can ensure that the behavior matches the documented behavior?

Personally, I think we should consider taking on that work as part of this PR. I am finding it challenging to review this PR without knowing what the expected behavior is. Moreover, I think this tool that we are building will be easier for others to adopt if we have documentation on how it is meant to function.


Update: We already updated the README with the expected behavior! 🎉 😍 I am sorry that I did not dig deeper and notice when I was writing this comment originally 🙇‍♂️

Screenshot 2024-04-17 at 2 51 21 PM

Based on this update documented behavior, though, it seems like we do not want to add the --strict flag 🤔 I am curious to get your thoughts @calda .

Comment on lines +80 to +83
// When autocorrecting, SwiftFormat returns EXIT_SUCCESS even if there were
// violations that were fixed, unless you pass in the --strict option.
if command.arguments.contains("--strict") {
return SwiftFormatExitCode.lintFailure
Copy link
Contributor

Choose a reason for hiding this comment

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

👍

ranSwiftLintAutocorrect = true

// When autocorrecting SwiftLint returns EXIT_SUCCESS
// even if there were violations that were fixed
return EXIT_SUCCESS
}))

XCTAssertEqual(error as? ExitCode, ExitCode(SwiftFormatExitCode.lintFailure))
XCTAssertEqual(error as? ExitCode, .failure)
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this the definition of ExitCode? https://apple.github.io/swift-argument-parser/documentation/argumentparser/exitcode/issuccess

If so, I feel like it may be better to check isSuccess than test against .failure. Will this check assertion be satisfied for any nonzero failing exit code?

return EXIT_SUCCESS
}))

XCTAssertEqual(error as? ExitCode, .failure)
Copy link
Contributor

Choose a reason for hiding this comment

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

This is a change in behavior, if I am understanding correctly.

@calda
Copy link
Member Author

calda commented Apr 17, 2024

@bachand -- this is a change in behavior to match our internal lint:swift command, which returns a non-zero exit code if SwiftFormat autocorrect applies any changes.

I was thinking it would make sense for this public tool to match the behavior of our internal tool, especially since we just added this --strict option to SwiftFormat for that use case.

You're right that we should also update the README, thanks for noticing. I'll make that change.

Do you think this change directionally makes sense, or do you think we should keep the current behavior where autocorrect mode will return a success / zero exit code when applying changes?

@bachand
Copy link
Contributor

bachand commented Apr 17, 2024

@bachand -- this is a change in behavior to match our internal lint:swift command, which returns a non-zero exit code if SwiftFormat autocorrect applies any changes.

I was thinking it would make sense for this public tool to match the behavior of our internal tool, especially since we just added this --strict option to SwiftFormat for that use case.

You're right that we should also update the README, thanks for noticing. I'll make that change.

Do you think this change directionally makes sense, or do you think we should keep the current behavior where autocorrect mode will return a success / zero exit code when applying changes?

@calda personally I think that documented behavior of this tool, returning 0 success code when any violations can be automatically corrected, makes sense. The motivation for returning a non-0 failure exit code when autocorrections are made, as I understand, is so that we can fail Git pre-commit / pre-push hooks if there are automatically corrected violations that were made and now need to be committed. My personal belief is that by default this tool should return a successful code "when there is no action for the user to take", either because there were no violations or they have been automatically fixed. This aligns with the default behavior of SwiftFormat/SwiftLint, at least as of the last time that I tested those tools.

That all being said, I think it would be reasonable to add a "strict" mode to our tool so that this tool can be used in a Git pre-commit / pre-push hook and fail that hook when autocorrections have been made. However, I think the strict mode should be opt-in.

I also think that it's important that the behavior of this tool is consistent. My understanding of this change is that the tool will fail if there's a SwiftFormat autocorrection but not if there is a SwiftLint autocorrection. I think that it's important that this tool has the same behavior for both "modes" of autocorrection.

@calda
Copy link
Member Author

calda commented Apr 17, 2024

Sounds good, thanks for sharing your thoughts. Let's leave this tool as-is for now then!

@calda calda closed this Apr 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants