diff --git a/modfile/read.go b/modfile/read.go index 5b5bb5e..2205682 100644 --- a/modfile/read.go +++ b/modfile/read.go @@ -225,7 +225,7 @@ func (x *FileSyntax) Cleanup() { if ww == 0 { continue } - if ww == 1 { + if ww == 1 && len(stmt.RParen.Comments.Before) == 0 { // Collapse block into single line. line := &Line{ Comments: Comments{ diff --git a/modfile/rule.go b/modfile/rule.go index 35fd1f5..0e7b7e2 100644 --- a/modfile/rule.go +++ b/modfile/rule.go @@ -308,6 +308,7 @@ var laxGoVersionRE = lazyregexp.New(`^v?(([1-9][0-9]*)\.(0|[1-9][0-9]*))([^0-9]. // Toolchains must be named beginning with `go1`, // like "go1.20.3" or "go1.20.3-gccgo". As a special case, "default" is also permitted. +// TODO(samthanawalla): Replace regex with https://pkg.go.dev/go/version#IsValid in 1.23+ var ToolchainRE = lazyregexp.New(`^default$|^go1($|\.)`) func (f *File) add(errs *ErrorList, block *LineBlock, line *Line, verb string, args []string, fix VersionFixer, strict bool) { @@ -384,7 +385,7 @@ func (f *File) add(errs *ErrorList, block *LineBlock, line *Line, verb string, a errorf("toolchain directive expects exactly one argument") return } else if strict && !ToolchainRE.MatchString(args[0]) { - errorf("invalid toolchain version '%s': must match format go1.23.0 or local", args[0]) + errorf("invalid toolchain version '%s': must match format go1.23.0 or default", args[0]) return } f.Toolchain = &Toolchain{Syntax: line} @@ -630,7 +631,7 @@ func (f *WorkFile) add(errs *ErrorList, line *Line, verb string, args []string, errorf("go directive expects exactly one argument") return } else if !GoVersionRE.MatchString(args[0]) { - errorf("invalid go version '%s': must match format 1.23", args[0]) + errorf("invalid go version '%s': must match format 1.23.0", args[0]) return } @@ -646,7 +647,7 @@ func (f *WorkFile) add(errs *ErrorList, line *Line, verb string, args []string, errorf("toolchain directive expects exactly one argument") return } else if !ToolchainRE.MatchString(args[0]) { - errorf("invalid toolchain version '%s': must match format go1.23 or local", args[0]) + errorf("invalid toolchain version '%s': must match format go1.23.0 or default", args[0]) return } @@ -974,6 +975,8 @@ func (f *File) AddGoStmt(version string) error { var hint Expr if f.Module != nil && f.Module.Syntax != nil { hint = f.Module.Syntax + } else if f.Syntax == nil { + f.Syntax = new(FileSyntax) } f.Go = &Go{ Version: version, diff --git a/modfile/rule_test.go b/modfile/rule_test.go index 57c8be6..ca11d17 100644 --- a/modfile/rule_test.go +++ b/modfile/rule_test.go @@ -90,6 +90,24 @@ var addRequireTests = []struct { ) `, }, + { + `unattached_comments`, + ` + module m + require ( + foo v0.0.0-00010101000000-000000000000 + // bar v0.0.0-00010101000000-000000000000 + ) + `, + "foo", "v0.0.0-00010101000000-000000000000", + ` + module m + require ( + foo v0.0.0-00010101000000-000000000000 + // bar v0.0.0-00010101000000-000000000000 + ) + `, + }, } type require struct { @@ -1549,6 +1567,20 @@ var fixVersionTests = []struct { }, } +var modifyEmptyFilesTests = []struct { + desc string + operations func(f *File) + want string +}{ + { + desc: `addGoStmt`, + operations: func(f *File) { + f.AddGoStmt("1.20") + }, + want: `go 1.20`, + }, +} + func fixV(path, version string) (string, error) { if path != "example.com/m" { return "", fmt.Errorf("module path must be example.com/m") @@ -1846,3 +1878,29 @@ func TestFixVersion(t *testing.T) { }) } } + +func TestAddOnEmptyFile(t *testing.T) { + for _, tt := range modifyEmptyFilesTests { + t.Run(tt.desc, func(t *testing.T) { + f := &File{} + tt.operations(f) + + expect, err := Parse("out", []byte(tt.want), nil) + if err != nil { + t.Fatal(err) + } + golden, err := expect.Format() + if err != nil { + t.Fatal(err) + } + got, err := f.Format() + if err != nil { + t.Fatal(err) + } + + if !bytes.Equal(got, golden) { + t.Fatalf("got:\n%s\nwant:\n%s", got, golden) + } + }) + } +} diff --git a/sumdb/client.go b/sumdb/client.go index aecdc68..04c6e24 100644 --- a/sumdb/client.go +++ b/sumdb/client.go @@ -8,7 +8,6 @@ import ( "bytes" "errors" "fmt" - "path" "strings" "sync" "sync/atomic" @@ -193,51 +192,7 @@ func (c *Client) SetGONOSUMDB(list string) { var ErrGONOSUMDB = errors.New("skipped (listed in GONOSUMDB)") func (c *Client) skip(target string) bool { - return globsMatchPath(c.nosumdb, target) -} - -// globsMatchPath reports whether any path prefix of target -// matches one of the glob patterns (as defined by path.Match) -// in the comma-separated globs list. -// It ignores any empty or malformed patterns in the list. -func globsMatchPath(globs, target string) bool { - for globs != "" { - // Extract next non-empty glob in comma-separated list. - var glob string - if i := strings.Index(globs, ","); i >= 0 { - glob, globs = globs[:i], globs[i+1:] - } else { - glob, globs = globs, "" - } - if glob == "" { - continue - } - - // A glob with N+1 path elements (N slashes) needs to be matched - // against the first N+1 path elements of target, - // which end just before the N+1'th slash. - n := strings.Count(glob, "/") - prefix := target - // Walk target, counting slashes, truncating at the N+1'th slash. - for i := 0; i < len(target); i++ { - if target[i] == '/' { - if n == 0 { - prefix = target[:i] - break - } - n-- - } - } - if n > 0 { - // Not enough prefix elements. - continue - } - matched, _ := path.Match(glob, prefix) - if matched { - return true - } - } - return false + return module.MatchPrefixPatterns(c.nosumdb, target) } // Lookup returns the go.sum lines for the given module path and version. diff --git a/sumdb/tlog/tile.go b/sumdb/tlog/tile.go index 857d487..37771c5 100644 --- a/sumdb/tlog/tile.go +++ b/sumdb/tlog/tile.go @@ -115,16 +115,14 @@ func NewTiles(h int, oldTreeSize, newTreeSize int64) []Tile { for level := uint(0); newTreeSize>>(H*level) > 0; level++ { oldN := oldTreeSize >> (H * level) newN := newTreeSize >> (H * level) + if oldN == newN { + continue + } for n := oldN >> H; n < newN>>H; n++ { tiles = append(tiles, Tile{H: h, L: int(level), N: n, W: 1 << H}) } n := newN >> H - maxW := int(newN - n< n< 0 { tiles = append(tiles, Tile{H: h, L: int(level), N: n, W: w}) } } diff --git a/sumdb/tlog/tile_test.go b/sumdb/tlog/tile_test.go index e451a63..62b50b7 100644 --- a/sumdb/tlog/tile_test.go +++ b/sumdb/tlog/tile_test.go @@ -5,6 +5,7 @@ package tlog import ( + "fmt" "testing" ) @@ -22,3 +23,28 @@ func FuzzParseTilePath(f *testing.F) { ParseTilePath(path) }) } + +func TestNewTilesForSize(t *testing.T) { + for _, tt := range []struct { + old, new int64 + want int + }{ + {1, 1, 0}, + {100, 101, 1}, + {1023, 1025, 3}, + {1024, 1030, 1}, + {1030, 2000, 1}, + {1030, 10000, 10}, + {49516517, 49516586, 3}, + } { + t.Run(fmt.Sprintf("%d-%d", tt.old, tt.new), func(t *testing.T) { + tiles := NewTiles(10, tt.old, tt.new) + if got := len(tiles); got != tt.want { + t.Errorf("got %d, want %d", got, tt.want) + for _, tile := range tiles { + t.Logf("%+v", tile) + } + } + }) + } +}