SwiftFormat icon indicating copy to clipboard operation
SwiftFormat copied to clipboard

Can`t save when I use Xcode build phase

Open RhettTamp opened this issue 3 years ago • 2 comments

when I test my code, it can`t format after I build. If I cmd+s first ,then build ,it can format, is there any way to solve this problem?

RhettTamp avatar May 12 '21 08:05 RhettTamp

Can you explain what you mean? How are you running the formatter - as a build step? What happens when it doesn't work?

nicklockwood avatar May 12 '21 14:05 nicklockwood

fwiw 2 years later ... I ran into this (or something that sounds very like it). I had SwiftFormat set up as a build phase script which was run before the compile phase (per instructions). I found that the formatting was unreliable in whether it occurred. And often I'd get a warning that another application had saved the file.

It would go something like this:

  • make a change, build + run.
  • see formatting update all good
  • make another change, build + run
  • formatting doesn't change (still builds and runs)
  • make another change, build+run
  • see warning about conflicting saves of same file

I eventually came to the conclusion that the Xcode build pipeline is running things in parallel and the changes you make in a file are not necessarily autosaved and flushed to disk before SwiftFormat gets to process them. But they are flushed before the compile stage. I tried to add input and output dependencies to the SwiftFormat build phase in an attempt to convince Xcode to flush all swift files before running the script but that didn't work (which strikes me as kinda broken - so either my conclusions are bad or my attempts to do this were broken in some way).

In the end I put the SwiftFormat build stage after the compile stage and reliability is back to 100%. Of course it does have the disadvantage of compiling the code before formatting. Currently I can live with that.

fivegreenapples avatar Jul 18 '23 09:07 fivegreenapples