// We need to do a second round of bad file processing.
var badGoError error
- badFiles := make(map[string]bool)
- badFile := func(name string, err error) {
+ badGoFiles := make(map[string]bool)
+ badGoFile := func(name string, err error) {
if badGoError == nil {
badGoError = err
}
- if !badFiles[name] {
+ if !badGoFiles[name] {
p.InvalidGoFiles = append(p.InvalidGoFiles, name)
- badFiles[name] = true
+ badGoFiles[name] = true
}
}
allTags := make(map[string]bool)
for _, tf := range rp.sourceFiles {
name := tf.name()
- if error := tf.error(); error != "" {
- badFile(name, errors.New(tf.error()))
- continue
- } else if parseError := tf.parseError(); parseError != "" {
- badFile(name, parseErrorFromString(tf.parseError()))
- // Fall through: we still want to list files with parse errors.
+ // Check errors for go files and call badGoFiles to put them in
+ // InvalidGoFiles if they do have an error.
+ if strings.HasSuffix(name, ".go") {
+ if error := tf.error(); error != "" {
+ badGoFile(name, errors.New(tf.error()))
+ continue
+ } else if parseError := tf.parseError(); parseError != "" {
+ badGoFile(name, parseErrorFromString(tf.parseError()))
+ // Fall through: we still want to list files with parse errors.
+ }
}
var shouldBuild = true
// TODO(#45999): The choice of p.Name is arbitrary based on file iteration
// order. Instead of resolving p.Name arbitrarily, we should clear out the
// existing Name and mark the existing files as also invalid.
- badFile(name, &MultiplePackageError{
+ badGoFile(name, &MultiplePackageError{
Dir: p.Dir,
Packages: []string{p.Name, pkg},
Files: []string{firstFile, name},
for _, imp := range imports {
if imp.path == "C" {
if isTest {
- badFile(name, fmt.Errorf("use of cgo in test %s not supported", name))
+ badGoFile(name, fmt.Errorf("use of cgo in test %s not supported", name))
continue
}
isCgo = true
}
if directives := tf.cgoDirectives(); directives != "" {
if err := ctxt.saveCgo(name, (*Package)(p), directives); err != nil {
- badFile(name, err)
+ badGoFile(name, err)
}
}
--- /dev/null
+# Test that a directory with an .s file that has a comment that can't
+# be parsed isn't matched as a go directory. (This was happening because
+# non-go files with unparsable comments were being added to InvalidGoFiles
+# leading the package matching code to think there were Go files in the
+# directory.)
+
+go list ./...
+! stdout .
+
+
+[short] skip
+
+# Test that an unparsable .s file is completely ignored when its name
+# has build tags that cause it to be filtered out, but produces an error
+# when it is included
+
+env GOARCH=arm64
+env GOOS=linux
+go build ./baz
+
+env GOARCH=amd64
+env GOOS=linux
+! go build ./baz
+
+-- go.mod --
+module example.com/foo
+
+go 1.20
+-- bar/bar.s --
+;/
+-- baz/baz.go --
+package bar
+-- baz/baz_amd64.s --
+;/
}
var badGoError error
- badFiles := make(map[string]bool)
- badFile := func(name string, err error) {
+ badGoFiles := make(map[string]bool)
+ badGoFile := func(name string, err error) {
if badGoError == nil {
badGoError = err
}
- if !badFiles[name] {
+ if !badGoFiles[name] {
p.InvalidGoFiles = append(p.InvalidGoFiles, name)
- badFiles[name] = true
+ badGoFiles[name] = true
}
}
ext := nameExt(name)
info, err := ctxt.matchFile(p.Dir, name, allTags, &p.BinaryOnly, fset)
- if err != nil {
- badFile(name, err)
+ if err != nil && strings.HasSuffix(name, ".go") {
+ badGoFile(name, err)
continue
}
if info == nil {
}
continue
}
- data, filename := info.header, info.name
// Going to save the file. For non-Go files, can stop here.
switch ext {
continue
}
+ data, filename := info.header, info.name
+
if info.parseErr != nil {
- badFile(name, info.parseErr)
+ badGoFile(name, info.parseErr)
// Fall through: we might still have a partial AST in info.parsed,
// and we want to list files with parse errors anyway.
}
// TODO(#45999): The choice of p.Name is arbitrary based on file iteration
// order. Instead of resolving p.Name arbitrarily, we should clear out the
// existing name and mark the existing files as also invalid.
- badFile(name, &MultiplePackageError{
+ badGoFile(name, &MultiplePackageError{
Dir: p.Dir,
Packages: []string{p.Name, pkg},
Files: []string{firstFile, name},
if line != 0 {
com, err := strconv.Unquote(qcom)
if err != nil {
- badFile(name, fmt.Errorf("%s:%d: cannot parse import comment", filename, line))
+ badGoFile(name, fmt.Errorf("%s:%d: cannot parse import comment", filename, line))
} else if p.ImportComment == "" {
p.ImportComment = com
firstCommentFile = name
} else if p.ImportComment != com {
- badFile(name, fmt.Errorf("found import comments %q (%s) and %q (%s) in %s", p.ImportComment, firstCommentFile, com, name, p.Dir))
+ badGoFile(name, fmt.Errorf("found import comments %q (%s) and %q (%s) in %s", p.ImportComment, firstCommentFile, com, name, p.Dir))
}
}
}
for _, imp := range info.imports {
if imp.path == "C" {
if isTest {
- badFile(name, fmt.Errorf("use of cgo in test %s not supported", filename))
+ badGoFile(name, fmt.Errorf("use of cgo in test %s not supported", filename))
continue
}
isCgo = true
if imp.doc != nil {
if err := ctxt.saveCgo(filename, p, imp.doc); err != nil {
- badFile(name, err)
+ badGoFile(name, err)
}
}
}
}
f.Close()
if err != nil {
- return nil, fmt.Errorf("read %s: %v", info.name, err)
+ return info, fmt.Errorf("read %s: %v", info.name, err)
}
// Look for go:build comments to accept or reject the file.
}
}
+// TestIssue56509 tests that go/build does not add non-go files to InvalidGoFiles
+// when they have unparsable comments.
+func TestIssue56509(t *testing.T) {
+ // The directory testdata/bads contains a .s file that has an unparsable
+ // comment. (go/build parses initial comments in non-go files looking for
+ // //go:build or //+go build comments).
+ p, err := ImportDir("testdata/bads", 0)
+ if err == nil {
+ t.Fatalf("could not import testdata/bads: %v", err)
+ }
+
+ if len(p.InvalidGoFiles) != 0 {
+ t.Fatalf("incorrectly added non-go file to InvalidGoFiles")
+ }
+}
+
// TestMissingImportErrorRepetition checks that when an unknown package is
// imported, the package path is only shown once in the error.
// Verifies golang.org/issue/34752.