From 899a558c0ca41dd4513b27ddbdc8b03974722f0a Mon Sep 17 00:00:00 2001 From: Pascal Hofmann Date: Thu, 21 Sep 2023 08:53:18 +0200 Subject: [PATCH] feat: ignore go build constraints (#22) BREAKING CHANGE: mod will now ignore go build constraints and upgrade/downgrade Semantic Import Versioning in all .go files. The flag `buildflags` has been removed. --- cmd/mod/main.go | 11 ++--------- example/subpkg/subpkg.go | 2 ++ major/major.go | 29 ++++++++++++++++++++--------- migrate/migrate.go | 2 +- 4 files changed, 25 insertions(+), 19 deletions(-) diff --git a/cmd/mod/main.go b/cmd/mod/main.go index def0e99..c7f5522 100644 --- a/cmd/mod/main.go +++ b/cmd/mod/main.go @@ -9,14 +9,9 @@ import ( ) func main() { - buildFlags := &cli.StringSliceFlag{ - Name: "buildflags", - Usage: "build flags to pass to the go compiler. Most commonly use for build flags ie. 'mod upgrade -buildflags=-tags=dev'", - } app := &cli.App{ Name: "mod", Usage: "upgrade/downgrade semantic import versioning", - Flags: []cli.Flag{buildFlags}, Commands: []*cli.Command{ { Name: "upgrade", @@ -33,7 +28,6 @@ func main() { Name: "mod-name", Value: "", }, - buildFlags, }, }, { @@ -46,7 +40,6 @@ func main() { Name: "mod-name", Value: "", }, - buildFlags, }, }, { @@ -76,11 +69,11 @@ func main() { } func upgrade(c *cli.Context) error { - return major.Run(".", "upgrade", c.String("mod-name"), c.Int("tag"), c.StringSlice("buildflags")) + return major.Run(".", "upgrade", c.String("mod-name"), c.Int("tag")) } func downgrade(c *cli.Context) error { - return major.Run(".", "downgrade", c.String("mod-name"), 0, c.StringSlice("buildflags")) + return major.Run(".", "downgrade", c.String("mod-name"), 0) } func migrateDeps(c *cli.Context) error { diff --git a/example/subpkg/subpkg.go b/example/subpkg/subpkg.go index 76a170f..ed2b377 100644 --- a/example/subpkg/subpkg.go +++ b/example/subpkg/subpkg.go @@ -1,3 +1,5 @@ +//go:build something + package subpkg import ( diff --git a/major/major.go b/major/major.go index 29db4e8..31d4fbc 100644 --- a/major/major.go +++ b/major/major.go @@ -4,7 +4,8 @@ import ( "flag" "fmt" "go/format" - "io/ioutil" + "go/parser" + "go/token" "log" "os" "path/filepath" @@ -20,7 +21,8 @@ import ( // Run upgrades or downgrades a module path and // all of its dependencies. -func Run(dir, op, modName string, tag int, buildFlags []string) error { +func Run(dir, op, modName string, tag int) error { + client := true var modFile *modfile.File modFile, err := mod.GetModFile(dir) @@ -39,7 +41,7 @@ func Run(dir, op, modName string, tag int, buildFlags []string) error { case "downgrade": newModPath = getPrevious(modName, separator) } - c := &packages.Config{Mode: packages.LoadSyntax, Tests: true, Dir: dir, BuildFlags: buildFlags} + c := &packages.Config{Mode: packages.NeedName | packages.NeedFiles, Tests: true, Dir: dir} pkgs, err := packages.Load(c, "./...") if err != nil { return errors.Wrap(err, "could not load package") @@ -66,7 +68,7 @@ func Run(dir, op, modName string, tag int, buildFlags []string) error { if err != nil { return errors.Wrap(err, "could not format go.mod file with new import path") } - err = ioutil.WriteFile(filepath.Join(dir, "go.mod"), bts, 0660) + err = os.WriteFile(filepath.Join(dir, "go.mod"), bts, 0660) if err != nil { return errors.Wrap(err, "could not rewrite go.mod file") } @@ -146,18 +148,27 @@ func getPrevious(s, sep string) string { } func updateImportPath(p *packages.Package, old, new, sep string, files map[string]struct{}) error { - for _, syn := range p.Syntax { - goFileName := p.Fset.File(syn.Pos()).Name() + + goFileNames := append(p.GoFiles, p.IgnoredFiles...) + for _, goFileName := range goFileNames { + if _, ok := files[goFileName]; ok { continue } files[goFileName] = struct{}{} + + fset := token.NewFileSet() + parsed, err := parser.ParseFile(fset, goFileName, nil, parser.ParseComments) + if err != nil { + return errors.Wrapf(err, "could not parse go file %v", goFileName) + } + var rewritten bool - for _, i := range syn.Imports { + for _, i := range parsed.Imports { imp := strings.Replace(i.Path.Value, `"`, ``, 2) if strings.HasPrefix(imp, fmt.Sprintf("%s%s", old, sep)) || imp == old { newImp := strings.Replace(imp, old, new, 1) - rewrote := astutil.RewriteImport(p.Fset, syn, imp, newImp) + rewrote := astutil.RewriteImport(fset, parsed, imp, newImp) if rewrote { rewritten = true } @@ -171,7 +182,7 @@ func updateImportPath(p *packages.Package, old, new, sep string, files map[strin if err != nil { return errors.Wrapf(err, "could not create go file %v", goFileName) } - err = format.Node(f, p.Fset, syn) + err = format.Node(f, fset, parsed) f.Close() if err != nil { return errors.Wrapf(err, "could not rewrite go file %v", goFileName) diff --git a/migrate/migrate.go b/migrate/migrate.go index 9b4bd23..9922972 100644 --- a/migrate/migrate.go +++ b/migrate/migrate.go @@ -103,7 +103,7 @@ func migrate(path string, gc *github.Client, test bool) error { } t := getTag(dir) fmt.Printf("upgrading %v to v%v\n", path, t) - if err := major.Run(dir, "upgrade", "", t, nil); err != nil { + if err := major.Run(dir, "upgrade", "", t); err != nil { return errors.Wrap(err, "err upgrading import paths") }