gci
gci copied to clipboard
automatically detect Go module path
This is just a proof of concept for #30.
Hey guys! @dmke would you be interested into updating this PR?
Also @daixiang0 what is your take here? If you don't mind me asking! I'm working with a monorepo with multiple modules, and this feature would make my life simpler!
@victoraugustolls Interested sure, but I'm currently in short supply of free time :)
It might take me until the weekend before I get to update the PR. Looking at #43, this might not be a trivial task...
This is an useful feature I think :)
Thanks for your contribution, now it has changed a lot.
I will review if you update it.
I've updated the code, however I can't get it to work:
At some point, (pkg/gci/sections).Module
needs to know which module paths are in scope. These are currently is seeded in (*pkg/gci/GciConfiguration).InitializeModules
, however, because (*pkg/gci/GciConfiguration).Sections
is a SectionList
, it is difficult to inject the module paths (for _, section := range g.Sections
creates a copy). The added test case fails for this reason.
I think a better way to pass the module path is to add an appropriate field to (pkg/gci/imports).ImportDef
, although I couldn't figure out how/when those objects are created.
Looking at (pkg/gci).processStdInAndGoFilesInPaths()
, there's another lingering problem: gci can read file contents from stdin, but the module path resolver needs a file name in order to traverse the file system and find the associated go.mod
. This might produce different results:
$ cat ../whatever.go | gci diff -s Standard,Module,Default
$ gci diff -s Standard,Module,Default ../whatever.go
I think this feature is helpful for a project, we can detect from go.mod
. But for a file, it is hard to detect.
Maybe we can skip single file support for now, until we get a better solution.
Feel free to take my code and make it work. I currently am quite busy with other things, and don't know when I gain the mental capacity needed to complete this...
I usually call gci using file watcher from the root of the project, indicating the full path to the file. Therefore, I think that you can try to look for the go.mod file in the current folder (where the utility is called) and if found, use it to determine the name of the module
Feel free to take my code and make it work. I currently am quite busy with other things, and don't know when I gain the mental capacity needed to complete this...
Thanks for your work! I've created https://github.com/daixiang0/gci/pull/179 that is a slimmed-down version of this work: it just handles looking at go.mod
in the current directory (the goal is to try and implement just the bare-minimum for this feature, and add extra functionality when/if we want it)