Skip to content

Commit

Permalink
work around cmd/go issue relating to CompiledGoFiles
Browse files Browse the repository at this point in the history
See https://golang.org/issue/28749. The improved asm test would fail:

	go parse: $WORK/imported/imported_amd64.s:1:1: expected 'package', found TEXT (and 2 more errors)

because we would incorrectly parse a non-Go file as a Go file.

Add a workaround. The original reporter's reproducer with go-ethereum
works now, as this was the last hiccup.

Fixes burrowers#555.
  • Loading branch information
mvdan committed Sep 23, 2022
1 parent 4a37d41 commit 1551672
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
2 changes: 1 addition & 1 deletion reverse.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ One can reverse a captured panic stack trace as follows:
}
file, err := parser.ParseFile(fset, goFile, nil, parser.SkipObjectResolution)
if err != nil {
return err
return fmt.Errorf("go parse: %w", err)
}
files = append(files, file)
}
Expand Down
17 changes: 17 additions & 0 deletions shared.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,23 @@ func appendListedPackages(packages []string, withDeps bool) error {
actionID := decodeHash(splitActionID(pkg.BuildID))
pkg.GarbleActionID = addGarbleToHash(actionID)
}

// Work around https://golang.org/issue/28749:
// cmd/go puts assembly, C, and C++ files in CompiledGoFiles.
//
// TODO: remove when upstream has fixed the bug.
out := pkg.CompiledGoFiles[:0]
for _, path := range pkg.CompiledGoFiles {
switch filepath.Ext(path) {
case "": // e.g. a generated Go file inside the build cache
case ".go":
default: // e.g. an assembly file
continue
}
out = append(out, path)
}
pkg.CompiledGoFiles = out

cache.ListedPackages[pkg.ImportPath] = &pkg
}

Expand Down
7 changes: 7 additions & 0 deletions testdata/script/asm.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ cmp stderr main.stderr

[short] stop # no need to verify this with -short

# Ensure that reversing doesn't error with assembly files.
# It should fail, as there is nothing to reverse, but without any parse error.
stdin empty-reverse.txt
! garble reverse .
! stderr .

garble -tiny build
exec ./main
cmp stderr main.stderr
Expand Down Expand Up @@ -101,3 +107,4 @@ TEXT ·PublicAdd(SB),$0-16
13 36
25 70
7
-- empty-reverse.txt --

0 comments on commit 1551672

Please sign in to comment.