Skip to content

goimports with --fix doesn't fix missing newline at EOF #3747

@kseniadumpling

Description

@kseniadumpling

Welcome

  • Yes, I'm using a binary release within 2 latest major releases. Only such installations are supported.
  • Yes, I've searched similar issues on GitHub and found goimports --fix not fixing new lines #1373, but it's closed as 'stale' without any fixes .
  • Yes, I've included all information below (version, config, etc.).
  • Yes, I've tried with the standalone linter if available (e.g., gocritic, go vet, etc.). (https://golangci-lint.run/usage/linters/)

Description of the problem

Goimports can detect the absence of a newline at EOF, but it doesn't fix the problem with --fix flag.

  # checking test.go with no newline at EOF
$ golangci-lint run --no-config --disable-all -E goimports test.go
test.go:7: File is not `goimports`-ed (goimports)
}

  # runnin the same command with --fix flag
$ golangci-lint run -v --fix --no-config --disable-all -E goimports test.go
...
INFO [runner] Fix issue &result.Issue{FromLinter:"goimports", Text:"File is not `goimports`-ed", Severity:"", SourceLines:[]string{"}"}, Replacement:(*result.Replacement)(0xc000ecc930), Pkg:(*packages.Package)(0xc0001bad80), LineRange:(*result.Range)(nil), Pos:token.Position{Filename:"test.go", Offset:0, Line:7, Column:0}, HunkPos:0, ExpectNoLint:false, ExpectedNoLintLinter:""} with range {7 7} 
...
INFO Execution took 103.535312ms     

  # checking test.go after the fix - the problem remains the same
$ golangci-lint run --no-config --disable-all -E goimports test.go
test.go:7: File is not `goimports`-ed (goimports)
}           

Full log is in the details

Standalone tool works fine:

$ goimports -d -v ./
2023/03/30 11:43:05.200811 fixImports(filename="test.go"), abs="/tmp/test-golangci/test.go", srcDir="/tmp/test-golangci" ...
diff -u test.go.orig test.go
--- test.go.orig        2023-03-30 11:43:05.199701873 +0000
+++ test.go     2023-03-30 11:43:05.199701873 +0000
@@ -4,4 +4,4 @@
 
 func main() {
        fmt.Println("Hello, world!")
-}
\ No newline at end of file
+}

$ goimports -w ./ && echo $?
0
$ goimports -d ./ && echo $?
0

This bug is the same as #1373, but it was closed about a year ago with no actual fix.

Version of golangci-lint

$ golangci-lint --version
golangci-lint has version 1.52.2 built with go1.20.2 from da04413a on 2023-03-25T18:11:28Z

Configuration file

$ cat .golangci.yml       # I'm not using any
cat: .golangci.yml: No such file or directory 

Go environment

$ go version && go env
go version go1.19.4 linux/amd64

Verbose output of running

$ golangci-lint cache clean
$ golangci-lint run -v --fix --no-config --disable-all -E goimports test.go
INFO [lintersdb] Active 1 linters: [goimports]    
INFO [loader] Go packages loading at mode 7 (compiled_files|name|files) took 58.298354ms 
INFO [runner/filename_unadjuster] Pre-built 0 adjustments in 149.815µs 
INFO [linters_context/goanalysis] analyzers took 0s with no stages 
INFO [runner] Fix issue &result.Issue{FromLinter:"goimports", Text:"File is not `goimports`-ed", Severity:"", SourceLines:[]string{"}"}, Replacement:(*result.Replacement)(0xc000f1cba0), Pkg:(*packages.Package)(0xc000002d80), LineRange:(*result.Range)(nil), Pos:token.Position{Filename:"test.go", Offset:0, Line:7, Column:0}, HunkPos:0, ExpectNoLint:false, ExpectedNoLintLinter:""} with range {7 7} 
INFO [runner] fixer took 155.435µs with stages: all: 155.435µs 
INFO [runner] Issues before processing: 1, after processing: 0 
INFO [runner] Processors filtering stat (out/in): path_shortener: 1/1, fixer: 0/1, cgo: 1/1, filename_unadjuster: 1/1, skip_files: 1/1, max_from_linter: 1/1, source_code: 1/1, severity-rules: 1/1, exclude: 1/1, exclude-rules: 1/1, uniq_by_line: 1/1, diff: 1/1, max_per_file_from_linter: 1/1, max_same_issues: 1/1, path_prettifier: 1/1, skip_dirs: 1/1, autogenerated_exclude: 1/1, identifier_marker: 1/1, nolint: 1/1 
INFO [runner] processing took 319.389µs with stages: fixer: 173.952µs, autogenerated_exclude: 31.277µs, identifier_marker: 30.462µs, nolint: 28.841µs, source_code: 19.217µs, path_prettifier: 15.342µs, exclude-rules: 8.36µs, skip_dirs: 6.844µs, cgo: 1.063µs, path_shortener: 624ns, max_same_issues: 601ns, max_from_linter: 568ns, filename_unadjuster: 485ns, uniq_by_line: 370ns, max_per_file_from_linter: 246ns, skip_files: 240ns, severity-rules: 215ns, exclude: 206ns, sort_results: 200ns, diff: 143ns, path_prefixer: 133ns 
INFO [runner] linters took 696.767µs with stages: goimports: 297.863µs 
INFO File cache stats: 1 entries of total size 73B 
INFO Memory: 2 samples, avg is 25.9MB, max is 25.9MB 
INFO Execution took 65.127716ms      

$ golangci-lint run -v --no-config --disable-all -E goimports test.go
INFO [lintersdb] Active 1 linters: [goimports]    
INFO [loader] Go packages loading at mode 7 (compiled_files|files|name) took 53.026583ms 
INFO [runner/filename_unadjuster] Pre-built 0 adjustments in 124.19µs 
INFO [linters_context/goanalysis] analyzers took 0s with no stages 
INFO [runner] Processors filtering stat (out/in): exclude: 1/1, nolint: 1/1, diff: 1/1, max_same_issues: 1/1, source_code: 1/1, skip_files: 1/1, identifier_marker: 1/1, max_from_linter: 1/1, fixer: 1/1, sort_results: 1/1, cgo: 1/1, filename_unadjuster: 1/1, skip_dirs: 1/1, autogenerated_exclude: 1/1, path_shortener: 1/1, severity-rules: 1/1, path_prefixer: 1/1, path_prettifier: 1/1, exclude-rules: 1/1, uniq_by_line: 1/1, max_per_file_from_linter: 1/1 
INFO [runner] processing took 121.501µs with stages: nolint: 27.702µs, autogenerated_exclude: 19.23µs, identifier_marker: 18.724µs, source_code: 17.478µs, path_prettifier: 10.36µs, exclude-rules: 9.032µs, max_from_linter: 6.936µs, skip_dirs: 5.512µs, uniq_by_line: 1.089µs, max_same_issues: 996ns, max_per_file_from_linter: 934ns, cgo: 801ns, filename_unadjuster: 704ns, path_shortener: 610ns, exclude: 294ns, skip_files: 238ns, severity-rules: 217ns, sort_results: 199ns, diff: 186ns, fixer: 168ns, path_prefixer: 91ns 
INFO [runner] linters took 450.473µs with stages: goimports: 268.934µs 
test.go:7: File is not `goimports`-ed (goimports)
}
INFO File cache stats: 1 entries of total size 73B 
INFO Memory: 2 samples, avg is 26.1MB, max is 26.1MB 
INFO Execution took 61.763105ms                

Code example or link to a public repository

package main

import "fmt"

func main() {
	fmt.Println("Hello, world!")
} // NO NEWLINE BELOW

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions