From 5c96d6aecb74b5940472030575aac4d63dc9df4e Mon Sep 17 00:00:00 2001 From: Theo Brigitte Date: Sat, 1 Jun 2024 19:29:35 +0200 Subject: [PATCH 1/2] goimports use go.mod module path as prefix --- cmd/goimports/goimports.go | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/cmd/goimports/goimports.go b/cmd/goimports/goimports.go index dcb5023a2e7..b4f1fd8cfbd 100644 --- a/cmd/goimports/goimports.go +++ b/cmd/goimports/goimports.go @@ -20,6 +20,7 @@ import ( "runtime/pprof" "strings" + "golang.org/x/mod/modfile" "golang.org/x/tools/internal/gocommand" "golang.org/x/tools/internal/imports" ) @@ -51,7 +52,7 @@ var ( func init() { flag.BoolVar(&options.AllErrors, "e", false, "report all errors (not just the first 10 on different lines)") - flag.StringVar(&options.LocalPrefix, "local", "", "put imports beginning with this string after 3rd-party packages; comma-separated list") + flag.StringVar(&options.LocalPrefix, "local", "", "put imports beginning with this string after 3rd-party packages; comma-separated list; if set to auto, use module path from go.mod as prefix") flag.BoolVar(&options.FormatOnly, "format-only", false, "if true, don't fix imports and only format. In this mode, goimports is effectively gofmt, with the addition that imports are grouped into sections.") } @@ -268,6 +269,23 @@ func gofmtMain() { return } + if options.LocalPrefix == "auto" { + wd, err := os.Getwd() + if err != nil { + log.Fatal(err) + } + filename := filepath.Join(wd, "go.mod") + data, err := os.ReadFile(filename) + if err != nil { + log.Fatal(err) + } + gomodModulePath := modfile.ModulePath(data) + if err != nil { + log.Fatal(err) + } + options.LocalPrefix = gomodModulePath + } + if len(paths) == 0 { if err := processFile("", os.Stdin, os.Stdout, fromStdin); err != nil { report(err) From b7472e549da1978bcc0fb85fd4e1f7557c821d61 Mon Sep 17 00:00:00 2001 From: Theo Brigitte Date: Sat, 1 Jun 2024 19:41:05 +0200 Subject: [PATCH 2/2] trigger ci