Repository: goplus/gop
Pull request #2071 (2025-01-12 02:38:59)

gop: support -tags

codecov[bot]: ## [Codecov](https://app.codecov.io/gh/goplus/gop/pull/2071?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goplus) Report
All modified and coverable lines are covered by tests :white_check_mark:
> Project coverage is 93.41%. Comparing base [(`f4daf21`)](https://app.codecov.io/gh/goplus/gop/commit/f4daf21fc107684e86dd3390b5758993c46e8640?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goplus) to head [(`c4154bb`)](https://app.codecov.io/gh/goplus/gop/commit/c4154bb146e675c1927643b44a6564465e4a7652?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goplus).

<details><summary>Additional details and impacted files</summary>


```diff
@@            Coverage Diff             @@
##             main    #2071      +/-   ##
==========================================
- Coverage   93.42%   93.41%   -0.01%     
==========================================
  Files          43       42       -1     
  Lines       11871    11869       -2     
==========================================
- Hits        11090    11088       -2     
  Misses        624      624              
  Partials      157      157              
```

</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/goplus/gop/pull/2071?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goplus).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goplus).


Patch:
diff --git a/cmd/internal/base/pass.go b/cmd/internal/base/pass.go
index 729c1afc..e2dd70a4 100644
--- a/cmd/internal/base/pass.go
+++ b/cmd/internal/base/pass.go
@@ -3,6 +3,7 @@ package base
 import (
 	"flag"
 	"fmt"
+	"strings"
 )
 
 type stringValue struct {
@@ -42,6 +43,15 @@ type PassArgs struct {
 	Flag *flag.FlagSet
 }
 
+func (p *PassArgs) Tags() string {
+	for _, v := range p.Args {
+		if strings.HasPrefix(v, "-tags=") {
+			return v[6:]
+		}
+	}
+	return ""
+}
+
 func (p *PassArgs) Var(names ...string) {
 	for _, name := range names {
 		p.Flag.Var(&stringValue{p: p, name: name}, name, "")
diff --git a/cmd/internal/build/build.go b/cmd/internal/build/build.go
index db10d6e5..dc55ea72 100644
--- a/cmd/internal/build/build.go
+++ b/cmd/internal/build/build.go
@@ -39,9 +39,9 @@ var Cmd = &base.Command{
 }
 
 var (
+	flag       = &Cmd.Flag
 	flagDebug  = flag.Bool("debug", false, "print debug information")
 	flagOutput = flag.String("o", "", "gop build output file")
-	flag       = &Cmd.Flag
 )
 
 func init() {
@@ -74,7 +74,7 @@ func runCmd(cmd *base.Command, args []string) {
 		log.Panicln("too many arguments:", args)
 	}
 
-	conf, err := gop.NewDefaultConf(".", gop.ConfFlagNoTestFiles)
+	conf, err := gop.NewDefaultConf(".", gop.ConfFlagNoTestFiles, pass.Tags())
 	if err != nil {
 		log.Panicln("gop.NewDefaultConf:", err)
 	}
diff --git a/cmd/internal/gengo/go.go b/cmd/internal/gengo/go.go
index d66682b9..2fd5a549 100644
--- a/cmd/internal/gengo/go.go
+++ b/cmd/internal/gengo/go.go
@@ -44,6 +44,7 @@ var (
 	flagSingleMode       = flag.Bool("s", false, "run in single file mode for package")
 	flagIgnoreNotatedErr = flag.Bool(
 		"ignore-notated-error", false, "ignore notated errors, only available together with -t (check mode)")
+	flagTags = flag.String("tags", "", "a comma-separated list of additional build tags to consider satisfied")
 )
 
 func init() {
@@ -71,7 +72,7 @@ func runCmd(cmd *base.Command, args []string) {
 		cl.SetDisableRecover(true)
 	}
 
-	conf, err := gop.NewDefaultConf(".", 0)
+	conf, err := gop.NewDefaultConf(".", 0, *flagTags)
 	if err != nil {
 		log.Panicln("gop.NewDefaultConf:", err)
 	}
diff --git a/cmd/internal/install/install.go b/cmd/internal/install/install.go
index 09166c84..6f4aa2b9 100644
--- a/cmd/internal/install/install.go
+++ b/cmd/internal/install/install.go
@@ -71,7 +71,7 @@ func runCmd(cmd *base.Command, args []string) {
 		cl.SetDisableRecover(true)
 	}
 
-	conf, err := gop.NewDefaultConf(".", gop.ConfFlagNoTestFiles)
+	conf, err := gop.NewDefaultConf(".", gop.ConfFlagNoTestFiles, pass.Tags())
 	if err != nil {
 		log.Panicln("gop.NewDefaultConf:", err)
 	}
diff --git a/cmd/internal/run/run.go b/cmd/internal/run/run.go
index 96cf53aa..6637af12 100644
--- a/cmd/internal/run/run.go
+++ b/cmd/internal/run/run.go
@@ -80,7 +80,7 @@ func runCmd(cmd *base.Command, args []string) {
 	}
 
 	noChdir := *flagNoChdir
-	conf, err := gop.NewDefaultConf(".", gop.ConfFlagNoTestFiles)
+	conf, err := gop.NewDefaultConf(".", gop.ConfFlagNoTestFiles, pass.Tags())
 	if err != nil {
 		log.Panicln("gop.NewDefaultConf:", err)
 	}
diff --git a/go.mod b/go.mod
index 4e9f122d..bdd850b7 100644
--- a/go.mod
+++ b/go.mod
@@ -4,9 +4,9 @@ go 1.18
 
 require (
 	github.com/fsnotify/fsnotify v1.8.0
-	github.com/goplus/gogen v1.16.5-0.20250109123606-4aa868a225d7
+	github.com/goplus/gogen v1.16.5-0.20250112014949-58e4e07fbbea
 	github.com/goplus/llgo v0.9.9
-	github.com/goplus/mod v0.13.13
+	github.com/goplus/mod v0.13.15
 	github.com/qiniu/x v1.13.10
 	golang.org/x/tools v0.19.0
 )
diff --git a/go.sum b/go.sum
index f6a01c5d..5c6b9f94 100644
--- a/go.sum
+++ b/go.sum
@@ -1,11 +1,11 @@
 github.com/fsnotify/fsnotify v1.8.0 h1:dAwr6QBTBZIkG8roQaJjGof0pp0EeF+tNV7YBP3F/8M=
 github.com/fsnotify/fsnotify v1.8.0/go.mod h1:8jBTzvmWwFyi3Pb8djgCCO5IBqzKJ/Jwo8TRcHyHii0=
-github.com/goplus/gogen v1.16.5-0.20250109123606-4aa868a225d7 h1:+w0mFMKgnWpNU9jEIbkVj5GfLXUKRFESSjtC26T12X8=
-github.com/goplus/gogen v1.16.5-0.20250109123606-4aa868a225d7/go.mod h1:6TQYbabXDF9LCdDkOOzHmfg1R4ENfXQ3XpHa9RhTSD8=
+github.com/goplus/gogen v1.16.5-0.20250112014949-58e4e07fbbea h1:xPRFhNEl7DxJTom3UCqK2iSXrqhzuoG4YvkeG/DzwOs=
+github.com/goplus/gogen v1.16.5-0.20250112014949-58e4e07fbbea/go.mod h1:6TQYbabXDF9LCdDkOOzHmfg1R4ENfXQ3XpHa9RhTSD8=
 github.com/goplus/llgo v0.9.9 h1:sCM8ehOtuAEY/NQbJ4xAMRZhxEdDgilouWZpoaLiYrM=
 github.com/goplus/llgo v0.9.9/go.mod h1:udcq+s6tGOdhJq7I8fXPTv4qT2j17/KrlvtcJrMZAoM=
-github.com/goplus/mod v0.13.13 h1:rvwXCCQciTz4NjB3GLAZ2cskw035B64F7KzRAyMYUCw=
-github.com/goplus/mod v0.13.13/go.mod h1:invR72Rz2+qpOOsXqxz830MX8/aR2GDR2EAow/WgfHI=
+github.com/goplus/mod v0.13.15 h1:IyneSjwm1VpwvHGz6hSHnFxZCuO6ULHcu74IAZcW9nw=
+github.com/goplus/mod v0.13.15/go.mod h1:invR72Rz2+qpOOsXqxz830MX8/aR2GDR2EAow/WgfHI=
 github.com/qiniu/x v1.13.10 h1:J4Z3XugYzAq85SlyAfqlKVrbf05glMbAOh+QncsDQpE=
 github.com/qiniu/x v1.13.10/go.mod h1:INZ2TSWSJVWO/RuELQROERcslBwVgFG7MkTfEdaQz9E=
 golang.org/x/mod v0.20.0 h1:utOm6MM3R3dnawAiJgn0y+xvuYRsm1RKM/4giyfDgV0=
diff --git a/imp.go b/imp.go
index 6a1d01f8..f7360c52 100644
--- a/imp.go
+++ b/imp.go
@@ -71,6 +71,13 @@ func NewImporter(mod *gopmod.Module, gop *env.Gop, fset *token.FileSet) *Importe
 	return ret
 }
 
+func (p *Importer) SetTags(tags string) {
+	p.impFrom.SetTags(tags)
+	if c, ok := p.impFrom.Cache().(*cache.Impl); ok {
+		c.SetTags(tags)
+	}
+}
+
 // CacheFile returns file path of the cache.
 func (p *Importer) CacheFile() string {
 	cacheDir, _ := os.UserCacheDir()
@@ -83,6 +90,9 @@ func (p *Importer) CacheFile() string {
 		io.WriteString(h, root)
 		fname = filepath.Base(root)
 	}
+	if tags := p.impFrom.Tags(); tags != "" {
+		io.WriteString(h, tags)
+	}
 	hash := base64.RawURLEncoding.EncodeToString(h.Sum(nil))
 	return cacheDir + hash + fname
 }
diff --git a/load.go b/load.go
index f6c3cf46..f6728c55 100644
--- a/load.go
+++ b/load.go
@@ -145,7 +145,7 @@ const (
 )
 
 // NewDefaultConf creates a dfault configuration for common cases.
-func NewDefaultConf(dir string, flags ConfFlags) (conf *Config, err error) {
+func NewDefaultConf(dir string, flags ConfFlags, tags ...string) (conf *Config, err error) {
 	mod, err := LoadMod(dir)
 	if err != nil {
 		return
@@ -153,6 +153,9 @@ func NewDefaultConf(dir string, flags ConfFlags) (conf *Config, err error) {
 	gop := gopenv.Get()
 	fset := token.NewFileSet()
 	imp := NewImporter(mod, gop, fset)
+	if len(tags) > 0 {
+		imp.SetTags(strings.Join(tags, ","))
+	}
 	conf = &Config{
 		Gop: gop, Fset: fset, Mod: mod, Importer: imp,
 		IgnoreNotatedError: flags&ConfFlagIgnoreNotatedError != 0,


Test patch:
diff --git a/cmd/internal/test/test.go b/cmd/internal/test/test.go
index e9bf58c8..7dd01cf9 100644
--- a/cmd/internal/test/test.go
+++ b/cmd/internal/test/test.go
@@ -69,7 +69,7 @@ func runCmd(cmd *base.Command, args []string) {
 		cl.SetDisableRecover(true)
 	}
 
-	conf, err := gop.NewDefaultConf(".", 0)
+	conf, err := gop.NewDefaultConf(".", 0, pass.Tags())
 	if err != nil {
 		log.Panicln("gop.NewDefaultConf:", err)
 	}