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

Support custom formatters with args #3729

Open
palsivertsen opened this issue Mar 21, 2025 · 4 comments
Open

Support custom formatters with args #3729

palsivertsen opened this issue Mar 21, 2025 · 4 comments

Comments

@palsivertsen
Copy link

I'm trying to set up gci as my formatter for more control over import arrangement. I think I've found the relevant extensions, but I can not get the arguments to work. Here's a configuration example:

{
  "go.alternateTools": {
    "customFormatter": "gci"
  },
  "go.formatFlags": [
    "print",
    "--custom-order",
    "--section=standard",
    "--section=default",
    "--section=blank",
    "--section=dot",
    "--section=alias",
    "--section=localmodule"
  ],
  "[go]": {
    "editor.defaultFormatter": "golang.go"
  },
  "go.formatTool": "custom"
}

Looking at the docs it seems like the args are just ignored:

Flags to pass to format tool (e.g. ["-s"]). Not applicable when using the language server.

I would have more control if I could configure formatting like so:

{
  "go.formatTool": {
    "command": "gci",
    "arguments": [
      "print",
      "--custom-order",
      "--section=standard",
      "--section=default",
      "--section=blank",
      "--section=dot",
      "--section=alias",
      "--section=localmodule"
    ],
    "passFileOn": "stdin"
  }
}
@gopherbot gopherbot added this to the Untriaged milestone Mar 21, 2025
@logica0419
Copy link

logica0419 commented Mar 24, 2025

Looking at the docs it seems like the args are just ignored:

Flags to pass to format tool (e.g. ["-s"]). Not applicable when using the language server.

Reading the source code, the language server isn't used when you set "go.formatTool" to "custom".
https://github.com/golang/vscode-go/blob/master/extension/src/language/legacy/goFormat.ts#L121-L139
https://github.com/golang/vscode-go/blob/master/extension/src/language/goLanguageServer.ts#L1011-L1026
https://github.com/golang/vscode-go/blob/master/extension/src/language/goLanguageServer.ts#L628-L638

The arguments for custom formatter work for me properly.
ex. These flags work.
https://golangci-lint.run/usage/configuration/#fmt

  "go.formatTool": "custom",
  "go.alternateTools": {
    "customFormatter": "golangci-lint"
  },
  "go.formatFlags": [
    "fmt",
    "--stdin"
  ],

Could you check if this command prints out the full code to stdout, receiving the previous full code from stdin?

@palsivertsen
Copy link
Author

@logica0419 Are you sure this configuration work? When I copy paste your settings it does not work for me. There is a formatter running, and it might be golangci-lint, but it's not using the args. For example providing an illegal argument still works:

    "go.formatTool": "custom",
    "go.alternateTools": {
        "customFormatter": "golangci-lint"
    },
    "go.formatFlags": [
        "asd",
        "fmt",
        "--stdin"
    ]

Also when comparing the output when running the tool directly I get two different results.

I see that golangci-lint fmt has support for GCI as the formatter, so I would be happy to use that, but it is not picking up any for my config files (from projectDir/.golangci-lint.yaml or ~/.golangci-lint.yaml).

@logica0419
Copy link

@palsivertsen
Umm... I'm sure the configuration does work. That's weird.
I tried the configuration you wrote in the issue description, and it worked with no problem in my environment.
Seems like this is not the configuration issue.

The format error may be displayed in the dev tools console (you can see it by clicking Help > Toggle Developer Tools), so could you check if some errors are displayed there?

@palsivertsen
Copy link
Author

There are no errors in the debug console.

I cleared all settings (user and workspace json files) and uninstalled all extensions except go. This is the only thing I have in my settings file:

{
    "go.formatTool": "custom",
    "go.alternateTools": {
        "customFormatter": "golangci-lint"
    },
    "go.formatFlags": [
        "fmt",
        "--stdin",
        "--no-config",
        "--enable",
        "gci"
    ]
}

Note I added --no-config to make testing this more consistent. I also explicitly enabled the gci formatter. Then I created the following file as a test case:

package example

import (

	"github.com/aws/aws-sdk-go-v2/aws"

	"fmt"

	"io"

	"log"

)

func Example() {
	var (
		_ = aws.SDKVersion
		_ = io.EOF
		_ = log.Print
		_ = fmt.Print
	)
}

When I run this through the formatter I get the expected results:

golangci-lint fmt --stdin --no-config --enable gci < example.go
package example

import (
	"fmt"
	"io"
	"log"

	"github.com/aws/aws-sdk-go-v2/aws"
)

func Example() {
	var (
		_ = aws.SDKVersion
		_ = io.EOF
		_ = log.Print
		_ = fmt.Print
	)
}

When I run the formatter in vscode I get a different result:

package example

import (
	"github.com/aws/aws-sdk-go-v2/aws"

	"fmt"

	"io"

	"log"
)

func Example() {
	var (
		_ = aws.SDKVersion
		_ = io.EOF
		_ = log.Print
		_ = fmt.Print
	)
}

This do match the results of running golangci-lint fmt --stdin --no-config < example.go, which matches the config that you have. Maybe these are the default args?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants