Repository: goplus/gop
Pull request #516 (2020-08-02 15:37:05)

cmd: export fix ignore internal import pkg bug
之前在 https://github.com/goplus/gop/pull/507 时判断忽略导出 internal pkg 时引入错误，导制部分库会忽略导出。
现已修复此错误，并改用在函数导出参数时进行 internal pkg 判断。

fix ignore internal pkg export. 
```
qexp golang.org/x/tools@latest/go/packages/packagestest
ignore func (*golang.org/x/tools/go/packages/packagestest.Exported).Mark(name string, r golang.org/x/tools/internal/span.Range)
export "golang.org/x/tools/go/packages/packagestest" success
```

codecov-commenter: # [Codecov](https://codecov.io/gh/goplus/gop/pull/516?src=pr&el=h1) Report
> Merging [#516](https://codecov.io/gh/goplus/gop/pull/516?src=pr&el=desc) into [master](https://codecov.io/gh/goplus/gop/commit/e0242e1641f7cafec8cf474b86bc2dec7c239241&el=desc) will **not change** coverage.
> The diff coverage is `n/a`.

[![Impacted file tree graph](https://codecov.io/gh/goplus/gop/pull/516/graphs/tree.svg?width=650&height=150&src=pr&token=4qln9Wk3Pn)](https://codecov.io/gh/goplus/gop/pull/516?src=pr&el=tree)

```diff
@@           Coverage Diff           @@
##           master     #516   +/-   ##
=======================================
  Coverage   89.38%   89.38%           
=======================================
  Files          40       40           
  Lines        9784     9784           
=======================================
  Hits         8745     8745           
  Misses        759      759           
  Partials      280      280           
```



------

[Continue to review full report at Codecov](https://codecov.io/gh/goplus/gop/pull/516?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/goplus/gop/pull/516?src=pr&el=footer). Last update [e0242e1...13a0a27](https://codecov.io/gh/goplus/gop/pull/516?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/cmd/internal/export/export.go b/cmd/internal/export/export.go
index 693f6e3c..c99529fd 100644
--- a/cmd/internal/export/export.go
+++ b/cmd/internal/export/export.go
@@ -196,11 +196,6 @@ func exportPkg(pkgPath string, srcDir string, goRoot bool) (err error) {
 	if err != nil {
 		return fmt.Errorf("import %q failed: %v", pkgPath, err)
 	}
-	for _, im := range pkg.Imports() {
-		if isIgnorePkg(im.Path()) {
-			return gopkg.ErrIgnore
-		}
-	}
 	var buf bytes.Buffer
 	err = gopkg.ExportPackage(pkg, &buf)
 	if err != nil {
diff --git a/cmd/internal/export/gopkg/exporter.go b/cmd/internal/export/gopkg/exporter.go
index 93f2da1b..eecfff1c 100644
--- a/cmd/internal/export/gopkg/exporter.go
+++ b/cmd/internal/export/gopkg/exporter.go
@@ -244,6 +244,15 @@ func (p *Exporter) fixPkgString(typ string) string {
 	})
 }
 
+func isInternalPkg(pkg string) bool {
+	for _, a := range strings.Split(pkg, "/") {
+		if a == "internal" {
+			return true
+		}
+	}
+	return false
+}
+
 // ExportFunc exports a go function/method.
 func (p *Exporter) ExportFunc(fn *types.Func) error {
 	tfn := fn.Type().(*types.Signature)
@@ -281,6 +290,10 @@ func (p *Exporter) ExportFunc(fn *types.Func) error {
 				fmt.Println("ignore", fn)
 				return nil
 			}
+			if pkg := named.Obj().Pkg(); pkg != nil && isInternalPkg(pkg.Path()) {
+				fmt.Println("ignore", fn)
+				return nil
+			}
 		}
 		p.useType(typ)
 		args[i] = p.typeCast("args["+strconv.Itoa(i+from)+"]", typ)


Test patch:
diff --git a/cmd/internal/export/gopkg/exporter_test.go b/cmd/internal/export/gopkg/exporter_test.go
index d54280ed..57bbc156 100644
--- a/cmd/internal/export/gopkg/exporter_test.go
+++ b/cmd/internal/export/gopkg/exporter_test.go
@@ -135,6 +135,13 @@ func TestExportIo(t *testing.T) {
 	}
 }
 
+func TestExportIoUtil(t *testing.T) {
+	err := Export("io/ioutil", ioutil.Discard)
+	if err != nil {
+		t.Fatal("TestExport failed:", err)
+	}
+}
+
 func TestExportGoTypes(t *testing.T) {
 	err := Export("go/types", ioutil.Discard)
 	if err != nil {
@@ -193,7 +200,21 @@ func TestLooupMod(t *testing.T) {
 	if dir != dir1 {
 		t.Fatal("LookupMod failed:", dir, dir1)
 	}
+}
 
+func TestIgnorePkg(t *testing.T) {
+	if !isInternalPkg("golang.org/x/tools/internal/span") {
+		t.Fatal("is internal pkg")
+	}
+	if isInternalPkg("golang.org/x/tools") {
+		t.Fatal("not internal pkg")
+	}
+}
+
+func TestWithoutPkg(t *testing.T) {
+	if withoutPkg("github.com/qiniu/x/log") != "github.com/qiniu/x/log" {
+		t.Fatal()
+	}
 }
 
 func TestBadLooupMod(t *testing.T) {