nicklockwood / SwiftFormat

A command-line tool and Xcode Extension for formatting Swift code
MIT License
7.9k stars 639 forks source link

error: Failed to terminate related to --maxwidth and wrapping single line comments #1288

Closed philviso-reverb closed 2 years ago

philviso-reverb commented 2 years ago

Hi,

I just upgraded a project to swiftformat 0.50.2, and whenever I try to format or lint our project I see the following output...

error: Failed to terminate.

I've been able to track the error down to two separate options, but I can't tell what's wrong. The two options that are causing the error to happen are...

--maxwidth 120
--self init-only

In the case of --self, it looks like the only value that's causing a problem is init-only. With --maxwidth it seems like any value other than none causes an error.

Could you provide some info about the context of when error: Failed to terminate happens? I looked through the code, but it doesn't give much information about what that code path actually means.

Thanks!

nicklockwood commented 2 years ago

Failed to terminate means that SwiftFormat wasn't able to reach a stable state where the file required no formatting changes after repeated rule applications. It's usually caused by a conflict between two rules where one undoes the effect of the other.

If you run with --verbose enabled, it should tell you which the last file it tried to format was before the failure. If you share that file + your config I can try to narrow down the problem.

philviso-reverb commented 2 years ago

Thanks for the tip about using --verbose. The conflicting rules we had were maxwidth + wrapSingleLineComments. There was a comment with a long URL that couldn't be wrapped.

nicklockwood commented 2 years ago

@philviso-reverb that shouldn't cause a conflict. SwiftFormat is aware that it can't wrap URLs so it should ignore it rather than get stuck in a loop. If you could post a code sample so I can reproduce the issue that would be helpful.

philviso-reverb commented 2 years ago

Oh 😄

In that case, I'll keep investigating and post sample code / configs if I can pin down exactly what's going on here.

philviso-reverb commented 2 years ago

OK, I narrowed it down to using maxwidth, comments that use 3 slashes ///, and a long URL

Here's the config...

--maxwidth 120

Here's the "code" that triggers the failure. Just put this in a file on its own.

/// Link to SDK documentation - https://docs.adyen.com/checkout/3d-secure/native-3ds2/api-integration#collect-the-3d-secure-2-device-fingerprint-from-an-ios-app

If I replace the /// with // then it works properly.

nicklockwood commented 2 years ago

Thanks!

nicklockwood commented 2 years ago

@philviso-reverb fixed in 0.50.3