nicklockwood / SwiftFormat

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

Not using config file from current dir #1181

Closed bofeizhu closed 2 years ago

bofeizhu commented 2 years ago

Running swiftformat from a child dir always ended up using the parent dir's .swiftformat config file.

nicklockwood commented 2 years ago

Thanks for reporting this. Can you give an example of the folder structure and where you are running the command so I can verify?

nicklockwood commented 2 years ago

To be clear, it's by design that it would use the parent directory config in addition to the config in the directory where you run the command, but not in preference to it.

bofeizhu commented 2 years ago

Thank you for the quick reply!

Here is the folder structure:

parent - .swiftformat, .swift.... |-- child - .swiftformat, .swift...

In my case, the child folder is also a git submodule of the parent repo. So in the parent dir's .swiftformat, I excluded the child dir to avoid unwanted changes to submodule. However, since swiftformat currently loading parent config by default, running the command in child folder would always result in the child folder get ignored. I also tried specify the config file using command --config, but still got the same behavior.

nicklockwood commented 2 years ago

OK, I understand the issue. There's not a really great solution for this right now, but one option is to to add --unexclude path/to/child/dir to your swiftformat command when formatting the child directory.

bofeizhu commented 2 years ago

Thank you! Your suggestion to add --unexclude works!

nonameplum commented 2 years ago

I have probably very similar problem My folder structure is

~/Developer
  .swiftformat (it contains --acronyms rule)
  /My
    /repo1
    /repo2
      .swiftformat
      /SwiftFormat/swiftformat (older version of the swiftformat binary that is used by the project build phrases script)

Regardless if I provide the options directly in build phases script execution of the swiftformat like

SwiftFormat/swiftformat "$SRCROOT" --rules ...`

or I try to provide a .swiftformat file in the repo2 folder. or I try to even force to use specific config by changing the build phase to execute with --config "...." option

It seems like the tool is always looking in the parent folders recursively and sums up the rules. In my case, it means that the repo2 is not building because the ~/Developer/My/repo2/SwiftFormat/swiftformat is using an older version of the tool that doesn't understand the acronyms rule.

@nicklockwood should I open another issue?