Skip to content

Commit 1c74d56

Browse files
committed
x/mod: add the ignore directive
This CL adds the ignore directive that will be used to support the global ignore mechanism in http://go.dev/cl/643355 For golang/go#42965 Change-Id: I6d0b25de1b4d26185298f6e3aea5ba66651256cb Reviewed-on: https://go-review.googlesource.com/c/mod/+/670656 Reviewed-by: Michael Matloob <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Michael Matloob <[email protected]>
1 parent f1f16fe commit 1c74d56

File tree

3 files changed

+203
-5
lines changed

3 files changed

+203
-5
lines changed

modfile/rule.go

+74-4
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ type File struct {
4545
Replace []*Replace
4646
Retract []*Retract
4747
Tool []*Tool
48+
Ignore []*Ignore
4849

4950
Syntax *FileSyntax
5051
}
@@ -101,6 +102,12 @@ type Tool struct {
101102
Syntax *Line
102103
}
103104

105+
// An Ignore is a single ignore statement.
106+
type Ignore struct {
107+
Path string
108+
Syntax *Line
109+
}
110+
104111
// A VersionInterval represents a range of versions with upper and lower bounds.
105112
// Intervals are closed: both bounds are included. When Low is equal to High,
106113
// the interval may refer to a single version ('v1.2.3') or an interval
@@ -305,7 +312,7 @@ func parseToFile(file string, data []byte, fix VersionFixer, strict bool) (parse
305312
})
306313
}
307314
continue
308-
case "module", "godebug", "require", "exclude", "replace", "retract", "tool":
315+
case "module", "godebug", "require", "exclude", "replace", "retract", "tool", "ignore":
309316
for _, l := range x.Line {
310317
f.add(&errs, x, l, x.Token[0], l.Token, fix, strict)
311318
}
@@ -338,7 +345,7 @@ func (f *File) add(errs *ErrorList, block *LineBlock, line *Line, verb string, a
338345
// and simply ignore those statements.
339346
if !strict {
340347
switch verb {
341-
case "go", "module", "retract", "require":
348+
case "go", "module", "retract", "require", "ignore":
342349
// want these even for dependency go.mods
343350
default:
344351
return
@@ -532,6 +539,21 @@ func (f *File) add(errs *ErrorList, block *LineBlock, line *Line, verb string, a
532539
Path: s,
533540
Syntax: line,
534541
})
542+
543+
case "ignore":
544+
if len(args) != 1 {
545+
errorf("ignore directive expects exactly one argument")
546+
return
547+
}
548+
s, err := parseString(&args[0])
549+
if err != nil {
550+
errorf("invalid quoted string: %v", err)
551+
return
552+
}
553+
f.Ignore = append(f.Ignore, &Ignore{
554+
Path: s,
555+
Syntax: line,
556+
})
535557
}
536558
}
537559

@@ -1620,6 +1642,36 @@ func (f *File) DropTool(path string) error {
16201642
return nil
16211643
}
16221644

1645+
// AddIgnore adds a new ignore directive with the given path.
1646+
// It does nothing if the ignore line already exists.
1647+
func (f *File) AddIgnore(path string) error {
1648+
for _, t := range f.Ignore {
1649+
if t.Path == path {
1650+
return nil
1651+
}
1652+
}
1653+
1654+
f.Ignore = append(f.Ignore, &Ignore{
1655+
Path: path,
1656+
Syntax: f.Syntax.addLine(nil, "ignore", path),
1657+
})
1658+
1659+
f.SortBlocks()
1660+
return nil
1661+
}
1662+
1663+
// DropIgnore removes a ignore directive with the given path.
1664+
// It does nothing if no such ignore directive exists.
1665+
func (f *File) DropIgnore(path string) error {
1666+
for _, t := range f.Ignore {
1667+
if t.Path == path {
1668+
t.Syntax.markRemoved()
1669+
*t = Ignore{}
1670+
}
1671+
}
1672+
return nil
1673+
}
1674+
16231675
func (f *File) SortBlocks() {
16241676
f.removeDups() // otherwise sorting is unsafe
16251677

@@ -1656,10 +1708,10 @@ func (f *File) SortBlocks() {
16561708
// retract directives are not de-duplicated since comments are
16571709
// meaningful, and versions may be retracted multiple times.
16581710
func (f *File) removeDups() {
1659-
removeDups(f.Syntax, &f.Exclude, &f.Replace, &f.Tool)
1711+
removeDups(f.Syntax, &f.Exclude, &f.Replace, &f.Tool, &f.Ignore)
16601712
}
16611713

1662-
func removeDups(syntax *FileSyntax, exclude *[]*Exclude, replace *[]*Replace, tool *[]*Tool) {
1714+
func removeDups(syntax *FileSyntax, exclude *[]*Exclude, replace *[]*Replace, tool *[]*Tool, ignore *[]*Ignore) {
16631715
kill := make(map[*Line]bool)
16641716

16651717
// Remove duplicate excludes.
@@ -1718,6 +1770,24 @@ func removeDups(syntax *FileSyntax, exclude *[]*Exclude, replace *[]*Replace, to
17181770
*tool = newTool
17191771
}
17201772

1773+
if ignore != nil {
1774+
haveIgnore := make(map[string]bool)
1775+
for _, i := range *ignore {
1776+
if haveIgnore[i.Path] {
1777+
kill[i.Syntax] = true
1778+
continue
1779+
}
1780+
haveIgnore[i.Path] = true
1781+
}
1782+
var newIgnore []*Ignore
1783+
for _, i := range *ignore {
1784+
if !kill[i.Syntax] {
1785+
newIgnore = append(newIgnore, i)
1786+
}
1787+
}
1788+
*ignore = newIgnore
1789+
}
1790+
17211791
// Duplicate require and retract directives are not removed.
17221792

17231793
// Drop killed statements from the syntax tree.

modfile/rule_test.go

+128
Original file line numberDiff line numberDiff line change
@@ -1780,6 +1780,72 @@ var dropToolTests = []struct {
17801780
},
17811781
}
17821782

1783+
var addIgnoreTests = []struct {
1784+
desc, in, path, want string
1785+
}{
1786+
{
1787+
`add_first`,
1788+
`module example.com/m`,
1789+
`foo/bar`,
1790+
`module example.com/m
1791+
ignore foo/bar`,
1792+
},
1793+
{
1794+
`sorted_correctly`,
1795+
`module example.com/m
1796+
ignore foo/bar
1797+
`,
1798+
`./foo/bar/path`,
1799+
`module example.com/m
1800+
ignore (
1801+
./foo/bar/path
1802+
foo/bar
1803+
)`,
1804+
},
1805+
{
1806+
`duplicates_ignored`,
1807+
`module example.com/m
1808+
ignore foo/bar
1809+
`,
1810+
`foo/bar`,
1811+
`module example.com/m
1812+
ignore foo/bar`,
1813+
},
1814+
}
1815+
1816+
var dropIgnoreTests = []struct {
1817+
desc, in, path, want string
1818+
}{
1819+
{
1820+
`only`,
1821+
`module example.com/m
1822+
ignore foo/bar`,
1823+
`foo/bar`,
1824+
`module example.com/m`,
1825+
},
1826+
{
1827+
`parenthesized`,
1828+
`module example.com/m
1829+
ignore (
1830+
./foo/bar
1831+
foo/bar
1832+
)`,
1833+
`foo/bar`,
1834+
`module example.com/m
1835+
ignore ./foo/bar`,
1836+
},
1837+
{
1838+
`missing`,
1839+
`module example.com/m
1840+
ignore (
1841+
foo/bar
1842+
)`,
1843+
`./foo/bar`,
1844+
`module example.com/m
1845+
ignore foo/bar`,
1846+
},
1847+
}
1848+
17831849
func fixV(path, version string) (string, error) {
17841850
if path != "example.com/m" {
17851851
return "", fmt.Errorf("module path must be example.com/m")
@@ -2190,3 +2256,65 @@ func TestDropTool(t *testing.T) {
21902256
})
21912257
}
21922258
}
2259+
2260+
func TestAddIgnore(t *testing.T) {
2261+
for _, tt := range addIgnoreTests {
2262+
t.Run(tt.desc, func(t *testing.T) {
2263+
inFile, err := Parse("in", []byte(tt.in), nil)
2264+
if err != nil {
2265+
t.Fatal(err)
2266+
}
2267+
if err := inFile.AddIgnore(tt.path); err != nil {
2268+
t.Fatal(err)
2269+
}
2270+
inFile.Cleanup()
2271+
got, err := inFile.Format()
2272+
if err != nil {
2273+
t.Fatal(err)
2274+
}
2275+
2276+
outFile, err := Parse("out", []byte(tt.want), nil)
2277+
if err != nil {
2278+
t.Fatal(err)
2279+
}
2280+
want, err := outFile.Format()
2281+
if err != nil {
2282+
t.Fatal(err)
2283+
}
2284+
if !bytes.Equal(got, want) {
2285+
t.Fatalf("got:\n%s\nwant:\n%s", got, want)
2286+
}
2287+
})
2288+
}
2289+
}
2290+
2291+
func TestDropIgnore(t *testing.T) {
2292+
for _, tt := range dropIgnoreTests {
2293+
t.Run(tt.desc, func(t *testing.T) {
2294+
inFile, err := Parse("in", []byte(tt.in), nil)
2295+
if err != nil {
2296+
t.Fatal(err)
2297+
}
2298+
if err := inFile.DropIgnore(tt.path); err != nil {
2299+
t.Fatal(err)
2300+
}
2301+
inFile.Cleanup()
2302+
got, err := inFile.Format()
2303+
if err != nil {
2304+
t.Fatal(err)
2305+
}
2306+
2307+
outFile, err := Parse("out", []byte(tt.want), nil)
2308+
if err != nil {
2309+
t.Fatal(err)
2310+
}
2311+
want, err := outFile.Format()
2312+
if err != nil {
2313+
t.Fatal(err)
2314+
}
2315+
if !bytes.Equal(got, want) {
2316+
t.Fatalf("got:\n%s\nwant:\n%s", got, want)
2317+
}
2318+
})
2319+
}
2320+
}

modfile/work.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -329,5 +329,5 @@ func (f *WorkFile) SortBlocks() {
329329
// retract directives are not de-duplicated since comments are
330330
// meaningful, and versions may be retracted multiple times.
331331
func (f *WorkFile) removeDups() {
332-
removeDups(f.Syntax, nil, &f.Replace, nil)
332+
removeDups(f.Syntax, nil, &f.Replace, nil, nil)
333333
}

0 commit comments

Comments
 (0)