-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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 ! |
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()
?
@@ -44,7 +44,7 @@ func TestFix(t *testing.T) { | |||
|
|||
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-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.