Skip to content

goimports --fix not fixing new lines #1373

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

Closed
howardjohn opened this issue Sep 17, 2020 · 5 comments · Fixed by #3917
Closed

goimports --fix not fixing new lines #1373

howardjohn opened this issue Sep 17, 2020 · 5 comments · Fixed by #3917
Labels
feedback required Requires additional feedback stale No recent correspondence or work activity

Comments

@howardjohn
Copy link

howardjohn commented Sep 17, 2020

When running goimports, golangci-lint will detect a missing newline at EOF, but not apply them:

$ golangci-lint run -v -c ./common/config/.golangci-format.yml test.go
INFO [config_reader] Used config file common/config/.golangci-format.yml
INFO [lintersdb] Active 1 linters: [goimports]
INFO [loader] Go packages loading at mode 7 (files|name|compiled_files) took 27.659759ms
INFO [runner/filename_unadjuster] Pre-built 0 adjustments in 49.973µs
INFO [linters context/goanalysis] analyzers took 0s with no stages
INFO [runner] Processors filtering stat (out/in): nolint: 1/1, max_from_linter: 1/1, source_code: 1/1, cgo: 1/1, autogenerated_exclude: 1/1, exclude: 1/1, exclude-rules: 1/1, uniq_by_line: 1/1, max_same_issues: 1/1, severity-rules: 1/1, sort_results: 1/1, filename_unadjuster: 1/1, skip_files: 1/1, skip_dirs: 1/1, max_per_file_from_linter: 1/1, path_prefixer: 1/1, path_prettifier: 1/1, identifier_marker: 1/1, diff: 1/1, path_shortener: 1/1
INFO [runner] processing took 128.018µs with stages: exclude: 37.354µs, identifier_marker: 20.623µs, path_prettifier: 15.191µs, autogenerated_exclude: 15.109µs, source_code: 14.982µs, nolint: 13.723µs, skip_dirs: 3.976µs, max_same_issues: 1.379µs, uniq_by_line: 1.359µs, max_per_file_from_linter: 1.155µs, path_shortener: 744ns, cgo: 640ns, max_from_linter: 572ns, filename_unadjuster: 401ns, diff: 204ns, sort_results: 182ns, exclude-rules: 116ns, severity-rules: 113ns, path_prefixer: 99ns, skip_files: 96ns
INFO [runner] linters took 430.891µs with stages: goimports: 243.474µs
test.go:5: File is not `goimports`-ed (goimports)
}
INFO File cache stats: 1 entries of total size 30B
INFO Memory: 2 samples, avg is 70.8MB, max is 70.8MB
INFO Execution took 31.869917ms
$ golangci-lint run --fix -v -c ./common/config/.golangci-format.yml test.go
INFO [config_reader] Used config file common/config/.golangci-format.yml
INFO [lintersdb] Active 1 linters: [goimports]
INFO [loader] Go packages loading at mode 7 (compiled_files|files|name) took 28.805532ms
INFO [runner/filename_unadjuster] Pre-built 0 adjustments in 51.842µs
INFO [linters context/goanalysis] analyzers took 0s with no stages
INFO [runner] Processors filtering stat (out/in): max_from_linter: 1/1, skip_files: 1/1, skip_dirs: 1/1, identifier_marker: 1/1, exclude-rules: 1/1, nolint: 1/1, filename_unadjuster: 1/1, autogenerated_exclude: 1/1, path_shortener: 1/1, severity-rules: 1/1, sort_results: 1/1, path_prefixer: 1/1, cgo: 1/1, path_prettifier: 1/1, uniq_by_line: 1/1, max_per_file_from_linter: 1/1, source_code: 1/1, exclude: 1/1, diff: 1/1, max_same_issues: 1/1
INFO [runner] processing took 166.636µs with stages: nolint: 50.056µs, exclude: 32.248µs, identifier_marker: 19.086µs, path_prettifier: 16.716µs, autogenerated_exclude: 13.315µs, cgo: 13.223µs, source_code: 12.397µs, skip_dirs: 5.241µs, path_shortener: 753ns, max_same_issues: 682ns, uniq_by_line: 668ns, filename_unadjuster: 578ns, max_per_file_from_linter: 537ns, max_from_linter: 330ns, diff: 213ns, sort_results: 151ns, skip_files: 129ns, exclude-rules: 120ns, severity-rules: 103ns, path_prefixer: 90ns
INFO [runner] linters took 627.997µs with stages: goimports: 368.007µs
INFO Fix issue &result.Issue{FromLinter:"goimports", Text:"File is not `goimports`-ed", Severity:"", SourceLines:[]string{"}"}, Replacement:(*result.Replacement)(0xc0008f7bc0), Pkg:(*packages.Package)(0xc00090d560), LineRange:(*result.Range)(nil), Pos:token.Position{Filename:"test.go", Offset:0, Line:5, Column:0}, HunkPos:0, ExpectNoLint:false, ExpectedNoLintLinter:""} with range {5 5}
INFO fixer took 203.964µs with stages: all: 203.964µs
INFO File cache stats: 1 entries of total size 30B
INFO Memory: 2 samples, avg is 70.1MB, max is 70.1MB
INFO Execution took 33.677428ms
$ golangci-lint run -v -c ./common/config/.golangci-format.yml test.go
INFO [config_reader] Used config file common/config/.golangci-format.yml
INFO [lintersdb] Active 1 linters: [goimports]
INFO [loader] Go packages loading at mode 7 (compiled_files|files|name) took 28.745181ms
INFO [runner/filename_unadjuster] Pre-built 0 adjustments in 54.279µs
INFO [linters context/goanalysis] analyzers took 0s with no stages
INFO [runner] Processors filtering stat (out/in): skip_dirs: 1/1, skip_files: 1/1, identifier_marker: 1/1, exclude-rules: 1/1, nolint: 1/1, uniq_by_line: 1/1, max_same_issues: 1/1, cgo: 1/1, path_prettifier: 1/1, sort_results: 1/1, source_code: 1/1, path_shortener: 1/1, diff: 1/1, max_per_file_from_linter: 1/1, max_from_linter: 1/1, severity-rules: 1/1, path_prefixer: 1/1, filename_unadjuster: 1/1, autogenerated_exclude: 1/1, exclude: 1/1
INFO [runner] processing took 131.85µs with stages: exclude: 32.109µs, identifier_marker: 24.309µs, path_prettifier: 18.135µs, autogenerated_exclude: 17.364µs, nolint: 14.508µs, source_code: 10.002µs, skip_dirs: 7.72µs, max_same_issues: 1.392µs, uniq_by_line: 1.364µs, cgo: 1.159µs, max_per_file_from_linter: 939ns, path_shortener: 735ns, filename_unadjuster: 573ns, max_from_linter: 547ns, sort_results: 296ns, diff: 265ns, skip_files: 121ns, exclude-rules: 120ns, severity-rules: 98ns, path_prefixer: 94ns
INFO [runner] linters took 529.673µs with stages: goimports: 342.251µs
test.go:5: File is not `goimports`-ed (goimports)
}
INFO File cache stats: 1 entries of total size 30B
INFO Memory: 2 samples, avg is 70.8MB, max is 70.8MB
INFO Execution took 33.307478ms

test.go

package main

func main() {

}

NOTE: no newline at end of test.go.

A direct goimports -w test.go does fix it.

$ golangci-lint --version
golangci-lint has version 1.30.0 built from 45b90f6 on 2020-08-03T03:10:34Z

Config:

service:
  golangci-lint-version: 1.30.x

linters:
  disable-all: true
  enable:
  - goimports
@howardjohn
Copy link
Author

Note - everything else is fixed except adding the trailing new line

@SVilgelm
Copy link
Member

I just checked with the latest (v1.35.1) version golangci-lint run -v --no-config --disable-all -E goimports --fix main.go applies all needed fixes.
@howardjohn Could you please check it?

@ldez ldez added the feedback required Requires additional feedback label Jan 11, 2021
@bombsimon
Copy link
Member

I think the issue here is that golangci-lint --fix is fixing the issue but goimports -w does not which is a bit confusing, especially if you've configured your editor to run goimports on save which should fix the issue.

@jgracenin
Copy link

jgracenin commented Jan 29, 2021

I am also running into this. I tried 1.36.0. The log is below.

Every time it runs, it detects the issue with an "INFO Fix issue log", but doesn't seem to fix it. This also affects gofmt.

Also: manually running goimports -w fixes it.

# Removing the newline at the end of a source file
$ truncate -s -1 main.go

$ golangci-lint version
golangci-lint has version 1.36.0 built from 6c25d06 on 2021-01-26T13:14:05Z

$ golangci-lint run -v --no-config --disable-all -E goimports --fix main.go
INFO [lintersdb] Active 1 linters: [goimports]    
INFO [loader] Go packages loading at mode 7 (compiled_files|files|name) took 195.815817ms 
INFO [runner/filename_unadjuster] Pre-built 0 adjustments in 121.748µs 
INFO [linters context/goanalysis] analyzers took 0s with no stages 
INFO [runner] Processors filtering stat (out/in): nolint: 1/1, max_from_linter: 1/1, severity-rules: 1/1, path_prefixer: 1/1, cgo: 1/1, filename_unadjuster: 1/1, skip_files: 1/1, identifier_marker: 1/1, path_prettifier: 1/1, source_code: 1/1, max_same_issues: 1/1, path_shortener: 1/1, skip_dirs: 1/1, autogenerated_exclude: 1/1, uniq_by_line: 1/1, max_per_file_from_linter: 1/1, exclude: 1/1, exclude-rules: 1/1, diff: 1/1, sort_results: 1/1 
INFO [runner] processing took 236.134µs with stages: nolint: 65.41µs, identifier_marker: 48.707µs, autogenerated_exclude: 23.365µs, source_code: 22.943µs, exclude-rules: 21.731µs, path_prettifier: 21.424µs, skip_dirs: 17.819µs, max_same_issues: 5.865µs, path_shortener: 1.438µs, cgo: 1.341µs, max_per_file_from_linter: 1.217µs, filename_unadjuster: 1.024µs, uniq_by_line: 1.003µs, max_from_linter: 678ns, skip_files: 587ns, diff: 498ns, exclude: 391ns, sort_results: 284ns, severity-rules: 240ns, path_prefixer: 169ns 
INFO [runner] linters took 1.168202ms with stages: goimports: 771.342µs 
INFO Fix issue &result.Issue{FromLinter:"goimports", Text:"File is not `goimports`-ed", Severity:"", SourceLines:[]string{"}"}, Replacement:(*result.Replacement)(0xc000de6600), Pkg:(*packages.Package)(0xc000dc1320), LineRange:(*result.Range)(nil), Pos:token.Position{Filename:"main.go", Offset:0, Line:7, Column:0}, HunkPos:0, ExpectNoLint:false, ExpectedNoLintLinter:""} with range {7 7} 
INFO fixer took 218.234µs with stages: all: 218.234µs 
INFO File cache stats: 1 entries of total size 66B 
INFO Memory: 4 samples, avg is 71.3MB, max is 71.3MB 
INFO Execution took 216.442738ms                  

$ golangci-lint run -v --no-config --disable-all -E gofmt --fix main.go
...
INFO Fix issue &result.Issue{FromLinter:"gofmt", Text:"File is not `gofmt`-ed with `-s`", Severity:"", SourceLines:[]string{"}"}, Replacement:(*result.Replacement)(0xc0005cb210), Pkg:(*packages.Package)(0xc000ca6c60), LineRange:(*result.Range)(nil), Pos:token.Position{Filename:"main.go", Offset:0, Line:7, Column:0}, HunkPos:0, ExpectNoLint:false, ExpectedNoLintLinter:""} with range {7 7} 
...

@stale
Copy link

stale bot commented Mar 30, 2022

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale No recent correspondence or work activity label Mar 30, 2022
@stale stale bot closed this as completed Apr 29, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feedback required Requires additional feedback stale No recent correspondence or work activity
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants