mvdan / gofumpt

A stricter gofmt
https://pkg.go.dev/mvdan.cc/gofumpt
BSD 3-Clause "New" or "Revised" License
3.15k stars 110 forks source link

different output for stdin vs passing in filename #277

Closed chrisplo closed 12 months ago

chrisplo commented 12 months ago

using v0.5.0

import ( ... mymodule/mypackage ... ) gets formatted in a separate block when running like gofumpt myfile.go but cat myfile.go formats differently in both cases the current working directory is the package for mayflie.go and that package is in the same module

I ran across this with null-ls integration at https://github.com/jose-elias-alvarez/null-ls.nvim/blob/main/lua/null-ls/builtins/formatting/gofumpt.lua uses stdin. There it uses stdin with the new file contents (can't use the file argument as the file is the old contents).

using stdin in the uql directory

cat client.go | gofumpt | head -8
package uql

import (
    "appsecviz/platform"
    "appsecviz/util" 

using filename in the uql directory

gofumpt client.go | head -8
package uql

import (
    "bytes"
    "context" 
mvdan commented 12 months ago

See https://github.com/mvdan/gofumpt/issues/117#issuecomment-813349378. Your module path should contain a dot, otherwise we assume that it's the standard library. An exception is when we are formatting inside a Go module, in which case we know the module path and we group its packages separately. This does not happen when formatting standard input, since it's only files on disk which can be part of a module.