Make --fix work (naively) with --prefix-path #3626
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This builds on @moitias' (Hi! 👋) work in #2293, resolves cropped up conflicts and addresses open review comments #2293 (comment) and #2293 (comment).
Issue
Currently, using
--path-prefix
together with--fix
does not work with any other path prefix than "".This is easily reproduced merely by adding
--path-prefix=something
to the command line when also using--fix
(and, of course, actually having something fixable) or adding it to the arguments used inTestFix
, which will cause the test to fail.This is problematic for my use case, a repository with multiple go modules in it (in separate directories) where I'd like to run
golangci-lint
on all of them withpre-commit
before commiting. To do this, I did not figure out other approaches than to rungolangci-lint
in each of those directories and using--path-prefix
to add the module directory to the output which does not work.Reproduction
(even though this test was ran with not quite the latest version, the issue reproduces with tip of master as well)
The error in the output clearly points out the problem;
ERRO Failed to fix issues in file foo/main.go: failed to get file bytes for foo/main.go: can't read file foo/main.go: open foo/main.go: no such file or directory
That is, the path prefix is prepended to the path of the file to be fixed which does not seem to be what is intended, as the help / documentation states that
--prefix-path
configures thePath prefix to add to **output**
.Fix
This PR adds a mock
--path-prefix
argument toTestFix
and implements a (very naive) fix for the issue, stripping the configured path prefix from the path passed toFixer.fixIssuesInFile
if the configured path prefix is not "".I'm happy to create an alternative fix if some other approach is seen to be better, options off the top of my head would include;
token.Position
, allowingFixer
to use it instead.Issue
, allowingIssue.FilePath()
to return it, as this is howFixer
seems to determine the path to use.token.Position.Filename
as there could be other needs for the actual path of the file in the future? This seems like the best option to me but the details of how to do this and implications it could have are unclear to me as I'm quite unfamiliar with the codebase, so would need some pointers to implement this.