-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
make --fix work (naively) with --prefix-path #2293
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
Conversation
|
Hey, thank you for opening your first Pull Request ! |
SVilgelm
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great idea, but please add the tests
Co-authored-by: Oleksandr Redko <[email protected]>
|
Hi, thanks for the review and sorry for the delay! @SVilgelm This is tested, but it's in the same I can break it into a separate test but I'm not sure what the value of that would be, as my uneducated gut feeling is that it would be pretty much the exact same test with less validation (and inputs) but more implied maintenance down the road. Happy to do either so let me know what you'd prefer. |
|
@SVilgelm @alexandear sorry about the ping, just making sure you've noticed @moitias' comment above -- if you could let us know what you think so we can get this moving forward. Thanks! |
| fileWithoutPathPrefix := file | ||
|
|
||
| if f.cfg.Output.PathPrefix != "" { | ||
| fileWithoutPathPrefix = strings.TrimPrefix(fileWithoutPathPrefix, f.cfg.Output.PathPrefix+string(filepath.Separator)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What will happen if the path prefix contains / at the end? --path-prefix=app/
Do we have any checks or are we using something like path.Clean()?
| args := []string{ | ||
| "--disable-all", "--print-issued-lines=false", "--print-linter-name=false", "--out-format=line-number", | ||
| "--allow-parallel-runners", "--fix", | ||
| "--allow-parallel-runners", "--fix", "--path-prefix=mock", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure about this. Right now you just replace one test with another.
It would be better to have different tests for the cases without and with path prefix
Already [opened Issue](golangci/golangci-lint#2293)
Already [opened Issue](golangci/golangci-lint#2293)
Issue
Currently, using
--path-prefixtogether with--fixdoes not work with any other path prefix than "".This is easily reproduced merely by adding
--path-prefix=somethingto 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-linton all of them withpre-commitbefore commiting. To do this, I did not figure out other approaches than to rungolangci-lintin each of those directories and using--path-prefixto 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 directoryThat 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-pathconfigures thePath prefix to add to **output**.Fix
This PR adds a mock
--path-prefixargument toTestFixand implements a (very naive) fix for the issue, stripping the configured path prefix from the path passed toFixer.fixIssuesInFileif 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, allowingFixerto use it instead.Issue, allowingIssue.FilePath()to return it, as this is howFixerseems to determine the path to use.token.Position.Filenameas 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.