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

Running formatters with non-stdin option to respect "ignore patterns" #232

Closed
1 task done
wookayin opened this issue Dec 4, 2023 · 6 comments
Closed
1 task done
Labels
bug Something isn't working

Comments

@wookayin
Copy link
Contributor

wookayin commented Dec 4, 2023

Neovim version (nvim -v)

0.9.4

Operating system/version

macOS, Linux

Add the debug logs

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

Log file

          21:29:59[INFO] Run yapf on /path/to/some/project/mypackage/envs.py
          21:29:59[DEBUG] Run command: { "yapf", "--quiet" }
          21:29:59[DEBUG] Run CWD: /path/to/some/project/envs.py
          21:30:00[DEBUG] yapf exited with code 0

Describe the bug

It looks like many of the formatters run the external formatter program through stdin/stdout PIPE. One drawback of this is that the filename is lost, so the formatting does not respect "ignore files".

For example, yapf 0.32+ supports .yapfignore or [tool.yapfignore] in pyproject.toml, but this setting is ignored because conform.nvim executes yapf by feeding the buffer contents into stdin.

I know the options stdin = false and args = { ..., "$FILENAME "}, but is this something that can be improved by default (also for many formatters not only for yapf) or are users supposed to take care of this manually?

Steps To Reproduce

Put .yapfignore in the project root:

# Ignore everything
**/*.py

Expected Behavior

Ignore files are respected.

Minimal example file

(Any files are affected)

Minimal init.lua

Skipped, not needed.

Additional context

No response

@wookayin wookayin added the bug Something isn't working label Dec 4, 2023
@wookayin
Copy link
Contributor Author

wookayin commented Dec 4, 2023

Specifically for yapf, one could try:

    require("conform").formatters["yapf"] = {
      -- cwd = .require("conform.util").root_file({   -- detect project root here
      --   "setup.py", "pyproject.toml", ".style.yapf", ".git",
      -- })
      stdin = false, -- See stevearc/conform.nvim#232 to support 'filename'
      args = { "--quiet", "$FILENAME" },
      range_args = function(ctx)
        return { "--quiet", "--lines", string.format("%d-%d", ctx.range.start[1], ctx.range["end"][1]), "$FILENAME" }
      end,
    }

but this will result in errors (Exit code 1) because when there are no input files to format, yapf will raise

yapf: input filenames did not match any python files

(this is more due to yapf's lack of fine-grained behavior control).

@stevearc
Copy link
Owner

stevearc commented Dec 4, 2023

I think this is going to be handled on a case-by-case basis for formatters. They all have different config files and logic for allowlisting/blocklisting certain files from formatting rules. If you can come up with a configuration that respects the config file by default, I'll take a PR to add it.

@stevearc stevearc closed this as completed Dec 4, 2023
@wookayin
Copy link
Contributor Author

wookayin commented Dec 4, 2023

Question: is there any particular reason where "stdin" mode is preferred over the "$FILENAME" mode in general?

@stevearc
Copy link
Owner

stevearc commented Dec 5, 2023

Fewer moving parts, fewer things that can go wrong, slightly better performance. But it's a mild preference, so if there's a solid reason why we should prefer the file-based formatting we shouldn't be shy about using that instead.

@wookayin
Copy link
Contributor Author

wookayin commented Dec 5, 2023

I see, thanks for the input. I also see another benefit: it can be also useful when dealing with range formatting.

For YAPF, actually some upstream fix would be needed because of the "no input" situation. I will come back again with a PR if I have a reasonable solution to deal with this specific use case.

@loeffel-io
Copy link

ref: bazelbuild/buildtools#1268

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

No branches or pull requests

3 participants