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

bug: swift_format doesn't respect .swift-format file #211

Closed
1 task done
heygarrett opened this issue Nov 21, 2023 · 10 comments · Fixed by #283
Closed
1 task done

bug: swift_format doesn't respect .swift-format file #211

heygarrett opened this issue Nov 21, 2023 · 10 comments · Fixed by #283
Labels
bug Something isn't working P2 Not a priority. PRs welcome

Comments

@heygarrett
Copy link

heygarrett commented Nov 21, 2023

Neovim version (nvim -v)

NVIM v0.9.4

Operating system/version

macOS 14.1.1 (23B81)

Add the debug logs

  • I have set log_level = vim.log.levels.DEBUG and pasted the log contents below.

Log file

Log file: /Users/garrett/dev/scrap/conform-swift/.repro//state/nvim/conform.log
          22:37:11[DEBUG] Run command: { "swift-format" }
          22:37:11[DEBUG] swift_format exited with code 0
          22:38:05[DEBUG] Running formatters on /Users/garrett/dev/scrap/conform-swift/Package.swift: { "swift_format" }
          22:38:05[INFO] Run swift_format on /Users/garrett/dev/scrap/conform-swift/Package.swift
          22:38:05[DEBUG] Run command: { "swift-format" }
          22:38:05[DEBUG] swift_format exited with code 0
          22:39:40[DEBUG] Running formatters on /Users/garrett/dev/scrap/conform-swift/Package.swift: { "swift_format" }
          22:39:40[INFO] Run swift_format on /Users/garrett/dev/scrap/conform-swift/Package.swift
          22:39:40[DEBUG] Run command: { "swift-format", "--configuration", ".swift-format" }
          22:39:40[DEBUG] swift_format exited with code 0
          22:40:05[DEBUG] Running formatters on /Users/garrett/dev/scrap/conform-swift/Package.swift: { "swift_format" }
          22:40:05[INFO] Run swift_format on /Users/garrett/dev/scrap/conform-swift/Package.swift
          22:40:05[DEBUG] Run command: { "swift-format" }
          22:40:05[DEBUG] swift_format exited with code 0

Formatters for this buffer:
swift_format ready (swift)

Other formatters:

Describe the bug

When using the configuration of swift-format included in conform.nvim (listed as swift_format), the presence of a .swift-format file is not respected when formatting Swift code. Running swift-format from the command line does respect the .swift-format file by default.

Adding prepend_args = { "--configuration", ".swift-format" } to the swift_format configuration does resolve the issue, but doesn't explain why the configuration file isn't respected by default.

Steps To Reproduce

  1. mkdir conform-swift && cd conform-swift
  2. swift package init --type executable
  3. echo '{ "indentation": { "tabs": 1 } }' > .swift-format
  4. nvim -u repro.lua Package.swift
  5. :lua require("conform").format()
  6. Uncomment the line in repro.lua that adds the prepend_args property
  7. Repeat steps 4 and 5

Expected Behavior

The spaces used for indentation in the Package.swift file would be replaced with tabs, as configured in the .swift-format file.

It only works after adding the prepend_args property as detailed above.

Minimal example file

swift package init --type executable will create the Package.swift file I'm using to reproduce the issue, but I'll include it here also:

// swift-tools-version: 5.9
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
    name: "conform-swift",
    targets: [
        // Targets are the basic building blocks of a package, defining a module or a test suite.
        // Targets can depend on other targets in this package and products from dependencies.
        .executableTarget(
            name: "conform-swift")
    ]
)

Minimal init.lua

-- DO NOT change the paths and don't remove the colorscheme
local root = vim.fn.fnamemodify("./.repro", ":p")

-- set stdpaths to use .repro
for _, name in ipairs({ "config", "data", "state", "cache" }) do
	vim.env[("XDG_%s_HOME"):format(name:upper())] = root .. "/" .. name
end

-- bootstrap lazy
local lazypath = root .. "/plugins/lazy.nvim"
if not vim.loop.fs_stat(lazypath) then
	vim.fn.system({
		"git",
		"clone",
		"--filter=blob:none",
		"--single-branch",
		"https://github.com/folke/lazy.nvim.git",
		lazypath,
	})
end
vim.opt.runtimepath:prepend(lazypath)

-- install plugins
local plugins = {
	"folke/tokyonight.nvim",
	{
		"stevearc/conform.nvim",
		config = function()
			require("conform").setup({
				log_level = vim.log.levels.DEBUG,
				-- add your config here
				formatters_by_ft = { swift = { "swift_format" } },
				formatters = {
					swift_format = {
						-- Uncomment this line to see the .swift-format file respected
						-- prepend_args = { "--configuration", ".swift-format" },
					},
				},
			})
		end,
	},
	-- add any other plugins here
}
require("lazy").setup(plugins, {
	root = root .. "/plugins",
})

vim.cmd.colorscheme("tokyonight")
-- add anything else here
vim.o.list = true

Additional context

I don't know if this is an issue with swift-format itself, but I can only reproduce it with conform.nvim

@heygarrett heygarrett added the bug Something isn't working label Nov 21, 2023
@stevearc
Copy link
Owner

Kind of crazy that the same command run on the command line produces different output. Is it being run from the same directory? If you add a cwd to the formatter, does that change anything?

@stevearc stevearc added question Further information is requested P1 May get worked on if I get free time. PRs welcome and removed P1 May get worked on if I get free time. PRs welcome labels Nov 30, 2023
@isaacthefallenapple
Copy link

@stevearc I've actually had a similar issue and I think using stdin is the problem. When you run <Package.swift swift-format it also doesn't respect the config (at least with my setup), whereas swift-format Package.swift does. I think using stdin = false and args = { '$FILENAME' } might work.

@heygarrett
Copy link
Author

heygarrett commented Dec 3, 2023

If you add a cwd to the formatter, does that change anything?

No change when using cwd = require("conform.util").root_file({ ".swift-format" }),

@github-actions github-actions bot removed the question Further information is requested label Dec 3, 2023
@stevearc
Copy link
Owner

stevearc commented Dec 5, 2023

@heygarrett can you confirm if the suggestion from @isaacthefallenapple works?

@stevearc stevearc added the question Further information is requested label Dec 5, 2023
@github-actions github-actions bot removed the question Further information is requested label Dec 5, 2023
@stevearc stevearc added the question Further information is requested label Dec 5, 2023
@heygarrett
Copy link
Author

The suggestion from @isaacthefallenapple doesn't work for me, unfortunately.

@github-actions github-actions bot removed the question Further information is requested label Dec 7, 2023
@stevearc
Copy link
Owner

I unfortunately don't have a great way to reproduce this. Happy to provide pointers, but I think I'll need someone else to make a PR for this one.

@stevearc stevearc added the P2 Not a priority. PRs welcome label Dec 24, 2023
@NSHaoSong
Copy link
Contributor

@heygarrett I had similar issues, this the config that works for me:

formatters = {
  swift_format = {
    stdin = false,
    args = { "$FILENAME", "--in-place" },
  },
},
formatters_by_ft = {
  swift = { "swift_format" },
},

This is very similar to what @isaacthefallenapple suggested, just need to add --in-place.

apple/swift-format/README.md

  • -i/--in-place: Overwrites the input files when formatting instead of
    printing the results to standard output. No backup of the original file is
    made before it is overwritten.

@NSHaoSong
Copy link
Contributor

Here's the log I have with my config

10:14:17[DEBUG] Running formatters on /Users/haosong/Developer/apple/test-conform-swift-format/Package.swift: { "swift_format" }
10:14:17[INFO] Run swift_format on /Users/haosong/Developer/apple/test-conform-swift-format/Package.swift
10:14:17[DEBUG] Creating temp file /Users/haosong/Developer/apple/test-conform-swift-format/.conform.9683855.Package.swift
10:14:17[DEBUG] Run command: { "swift-format", "/Users/haosong/Developer/apple/test-conform-swift-format/.conform.9683855.Package.swift", "--in-place" }
10:14:17[DEBUG] swift_format exited with code 0
10:14:17[DEBUG] Cleaning up temp file /Users/haosong/Developer/apple/test-conform-swift-format/.conform.9683855.Package.swift

NSHaoSong added a commit to NSHaoSong/conform.nvim that referenced this issue Jan 28, 2024
`swift-format` won't pick up `.swift-format` config file in stdin mode. We need to set `stdin = false` and `--in-place` to make it work with `conform.nvim`.

[apple/swift-format/README.md](https://github.com/apple/swift-format/blob/e312ede68bd8549381f9e2edfef7e6c176e9a50e/README.md#L129-L131)

> *   `-i/--in-place`: Overwrites the input files when formatting instead of
    printing the results to standard output. _No backup of the original file is
    made before it is overwritten._

Fix stevearc#211
@NSHaoSong
Copy link
Contributor

@stevearc I created a PR to add the extra args in the config file for swift_format: #283

Not sure if this is the best way to fix or not.

stevearc pushed a commit that referenced this issue Jan 29, 2024
`swift-format` won't pick up `.swift-format` config file in stdin mode. We need to set `stdin = false` and `--in-place` to make it work with `conform.nvim`.

[apple/swift-format/README.md](https://github.com/apple/swift-format/blob/e312ede68bd8549381f9e2edfef7e6c176e9a50e/README.md#L129-L131)

> *   `-i/--in-place`: Overwrites the input files when formatting instead of
    printing the results to standard output. _No backup of the original file is
    made before it is overwritten._

Fix #211
@heygarrett
Copy link
Author

Fixed for me! Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working P2 Not a priority. PRs welcome
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants