use errors.Is for listPackage errors

pull/632/head
Daniel Martí 2 years ago
parent 41df1f8725
commit 9a0d48c27e

@ -1006,12 +1006,11 @@ func (tf *transformer) transformLinkname(localName, newName string) (string, str
lpkg, err := listPackage(pkgPath)
if err != nil {
// TODO(mvdan): use errors.As or errors.Is instead
if strings.Contains(err.Error(), "path not found") {
if errors.Is(err, ErrNotFound) {
// Probably a made up name like above, but with a dot.
return localName, newName
}
if strings.Contains(err.Error(), "refusing to list") {
if errors.Is(err, ErrNotDependency) {
fmt.Fprintf(os.Stderr,
"//go:linkname refers to %s - add `import _ %q` so garble can find the package",
newName, pkgPath)

@ -7,6 +7,7 @@ import (
"bytes"
"encoding/gob"
"encoding/json"
"errors"
"fmt"
"log"
"os"
@ -314,6 +315,10 @@ var cannotObfuscate = map[string]bool{
var listedRuntimeLinknamed = false
var ErrNotFound = errors.New("not found")
var ErrNotDependency = errors.New("not a dependency")
// listPackage gets the listedPackage information for a certain package
func listPackage(path string) (*listedPackage, error) {
if path == curPkg.ImportPath {
@ -396,7 +401,7 @@ func listPackage(path string) (*listedPackage, error) {
return pkg, nil
}
if !ok {
return nil, fmt.Errorf("path not found in listed packages: %s", path)
return nil, fmt.Errorf("list %s: %w", path, ErrNotFound)
}
// Packages other than runtime can list any package,
@ -420,5 +425,5 @@ func listPackage(path string) (*listedPackage, error) {
}
}
return nil, fmt.Errorf("refusing to list non-dependency package: %s", path)
return nil, fmt.Errorf("list %s: %w", path, ErrNotDependency)
}

Loading…
Cancel
Save