Repository: goplus/gop
Pull request #995 (2021-12-01 01:20:47)

gop run file: use gopmod.OpenProject


Patch:
diff --git a/.gitattributes b/.gitattributes
deleted file mode 100644
index b541ff26..00000000
--- a/.gitattributes
+++ /dev/null
@@ -1,5 +0,0 @@
-*.expect text eol=lf
-*.golden text eol=lf
-
-*.gop text eol=lf
-*.go text eol=lf
\ No newline at end of file
diff --git a/.github/workflows/release-build.yml b/.github/workflows/release-build.yml
deleted file mode 100644
index f2406b3b..00000000
--- a/.github/workflows/release-build.yml
+++ /dev/null
@@ -1,38 +0,0 @@
-name: Release Build
-
-on:
-  push:
-    tags:
-      - "v*.*.*"
-
-jobs:
-  build:
-    runs-on: ubuntu-latest
-    steps:
-    - uses: actions/checkout@v2
-
-    - name: Checkout tag
-      run: |
-        git fetch --depth=1 origin +refs/tags/*:refs/tags/*
-        tag_name="${GITHUB_REF##*/}"
-        echo Current tag: $tag_name
-        git checkout $tag_name
-        echo "TAG_NAME=${tag_name}" >> $GITHUB_ENV
-
-    - name: Set up Go
-      uses: actions/setup-go@v2
-      with:
-        go-version: 1.17
-
-    - name: Build
-      run: |
-        make release-all -j$(nproc)
-
-    - name: Release
-      uses: svenstaro/upload-release-action@v2
-      with:
-        repo_token: ${{ secrets.GITHUB_TOKEN }}
-        tag: ${{ env.TAG_NAME }}
-        file: ./gop-*.zip
-        file_glob: true
-        prerelease: true
\ No newline at end of file
diff --git a/Makefile b/Makefile
deleted file mode 100644
index bce14f8a..00000000
--- a/Makefile
+++ /dev/null
@@ -1,119 +0,0 @@
-NAME := gop
-PACKAGE_NAME := github.com/goplus/gop
-RELEASE_VERSION := `git describe --tags`
-COMMIT := `git rev-parse --short HEAD`
-BUILD_DATE := `date -u +"%Y-%m-%d_%H-%M-%S"`
-
-PLATFORM := linux
-BUILD_DIR=.
-BUILD_ROOT_DIR := build-dir
-BUILD_LDFLAGS := -X 'github.com/goplus/gop/env.buildDate=$(BUILD_DATE)'
-BUILD_LDFLAGS +=  -X '$(PACKAGE_NAME).buildCommit=$(COMMIT)'
-BUILD_LDFLAGS +=  -X '$(PACKAGE_NAME).buildVersion=$(RELEASE_VERSION)'
-
-EXE_SUFFIX=
-
-.PHONY: clean all
-all:
-	cd cmd; go install -v ./...
-
-clean:
-	rm -rf $(BUILD_ROOT_DIR)
-	rm -f *.zip
-	rm -f gop gopfmt
-
-
-release:
-  # the release action expects the generated binaries placed in the path where make runs.
-  # Refer: https://github.com/marketplace/actions/go-release-binaries
-	CGO_ENABLED=0 go build -o $(BUILD_DIR)/gop$(EXE_SUFFIX) -ldflags "$(BUILD_LDFLAGS)" github.com/goplus/gop/cmd/gop
-	CGO_ENABLED=0 go build -o $(BUILD_DIR)/gopfmt$(EXE_SUFFIX) -ldflags "$(BUILD_LDFLAGS)" github.com/goplus/gop/cmd/gopfmt
-
-gop:
-	mkdir -p $(BUILD_DIR)
-	$(MAKE) release
-
-release-all: darwin-amd64.zip darwin-arm64.zip linux-386.zip linux-amd64.zip \
-	linux-arm.zip linux-armv5.zip linux-armv6.zip linux-armv7.zip linux-armv8.zip \
-	linux-mips-softfloat.zip linux-mips-hardfloat.zip linux-mipsle-softfloat.zip linux-mipsle-hardfloat.zip \
-	linux-mips64.zip linux-mips64le.zip freebsd-386.zip freebsd-amd64.zip \
-	windows-386.zip windows-amd64.zip windows-arm.zip windows-armv6.zip windows-armv7.zip windows-arm64.zip
-
-%.zip: %
-	@echo "Building $(NAME)-$(RELEASE_VERSION)-$@"
-	@mkdir -p $(BUILD_ROOT_DIR)/$</bin
-	
-	@rm -f $(NAME)-$(RELEASE_VERSION)-$@
-	@cd $(BUILD_ROOT_DIR)/$</ && zip -r ../../$(NAME)-$(RELEASE_VERSION)-$@ bin
-	@zip -dur $(NAME)-$(RELEASE_VERSION)-$@ builtin go.mod go.sum LICENSE README*.md
-	@echo "$(NAME)-$(RELEASE_VERSION)-$@ Done"
-	
-
-darwin-amd64:
-	$(MAKE) GOARCH=amd64 GOOS=darwin BUILD_DIR=$(BUILD_ROOT_DIR)/$@/bin release
-
-darwin-arm64:
-	$(MAKE) GOARCH=arm64 GOOS=darwin BUILD_DIR=$(BUILD_ROOT_DIR)/$@/bin release
-
-linux-386:
-	$(MAKE) GOARCH=386 GOOS=linux BUILD_DIR=$(BUILD_ROOT_DIR)/$@/bin release
-
-linux-amd64:
-	$(MAKE) GOARCH=amd64 GOOS=linux BUILD_DIR=$(BUILD_ROOT_DIR)/$@/bin release
-
-linux-arm:
-	$(MAKE) GOARCH=arm GOOS=linux BUILD_DIR=$(BUILD_ROOT_DIR)/$@/bin release
-
-linux-armv5:
-	$(MAKE) GOARCH=arm GOOS=linux GOARM=5 BUILD_DIR=$(BUILD_ROOT_DIR)/$@/bin release
-
-linux-armv6:
-	$(MAKE) GOARCH=arm GOOS=linux GOARM=6 BUILD_DIR=$(BUILD_ROOT_DIR)/$@/bin release
-
-linux-armv7:
-	$(MAKE) GOARCH=arm GOOS=linux GOARM=7 BUILD_DIR=$(BUILD_ROOT_DIR)/$@/bin release
-
-linux-armv8:
-	$(MAKE) GOARCH=arm64 GOOS=linux BUILD_DIR=$(BUILD_ROOT_DIR)/$@/bin release
-
-linux-mips-softfloat:
-	$(MAKE) GOARCH=mips GOMIPS=softfloat GOOS=linux BUILD_DIR=$(BUILD_ROOT_DIR)/$@/bin release
-
-linux-mips-hardfloat:
-	$(MAKE) GOARCH=mips GOMIPS=hardfloat GOOS=linux BUILD_DIR=$(BUILD_ROOT_DIR)/$@/bin release
-
-linux-mipsle-softfloat:
-	$(MAKE) GOARCH=mipsle GOMIPS=softfloat GOOS=linux BUILD_DIR=$(BUILD_ROOT_DIR)/$@/bin release
-
-linux-mipsle-hardfloat:
-	$(MAKE) GOARCH=mipsle GOMIPS=hardfloat GOOS=linux BUILD_DIR=$(BUILD_ROOT_DIR)/$@/bin release
-
-linux-mips64:
-	$(MAKE) GOARCH=mips64 GOOS=linux BUILD_DIR=$(BUILD_ROOT_DIR)/$@/bin release
-
-linux-mips64le:
-	$(MAKE) GOARCH=mips64le GOOS=linux BUILD_DIR=$(BUILD_ROOT_DIR)/$@/bin release
-
-freebsd-386:
-	$(MAKE) GOARCH=386 GOOS=freebsd BUILD_DIR=$(BUILD_ROOT_DIR)/$@/bin release
-
-freebsd-amd64:
-	$(MAKE) GOARCH=amd64 GOOS=freebsd BUILD_DIR=$(BUILD_ROOT_DIR)/$@/bin release
-
-windows-386:
-	$(MAKE) GOARCH=386 GOOS=windows EXE_SUFFIX=.exe BUILD_DIR=$(BUILD_ROOT_DIR)/$@/bin release
-
-windows-amd64:
-	$(MAKE) GOARCH=amd64 GOOS=windows EXE_SUFFIX=.exe BUILD_DIR=$(BUILD_ROOT_DIR)/$@/bin release
-
-windows-arm:
-	$(MAKE) GOARCH=arm GOOS=windows EXE_SUFFIX=.exe BUILD_DIR=$(BUILD_ROOT_DIR)/$@/bin release
-
-windows-armv6:
-	$(MAKE) GOARCH=arm GOOS=windows EXE_SUFFIX=.exe GOARM=6 BUILD_DIR=$(BUILD_ROOT_DIR)/$@/bin release
-
-windows-armv7:
-	$(MAKE) GOARCH=arm GOOS=windows EXE_SUFFIX=.exe GOARM=7 BUILD_DIR=$(BUILD_ROOT_DIR)/$@/bin release
-
-windows-arm64:
-	$(MAKE) GOARCH=arm64 GOOS=windows EXE_SUFFIX=.exe BUILD_DIR=$(BUILD_ROOT_DIR)/$@/bin release
\ No newline at end of file
diff --git a/README.md b/README.md
index 6df130a6..369fcfdd 100644
--- a/README.md
+++ b/README.md
@@ -21,7 +21,7 @@ For now, we suggest you install Go+ from source code.
 git clone https://github.com/goplus/gop.git
 cd gop
 
-# On mac/linux run:
+# On linux run:
 ./all.bash
 # On Windows run:
 all.bat
diff --git a/README_zh.md b/README_zh.md
index 93c1f8a0..03734118 100644
--- a/README_zh.md
+++ b/README_zh.md
@@ -7,11 +7,11 @@
 [![Tutorials](https://img.shields.io/badge/tutorial-Go+-blue.svg)](https://tutorial.goplus.org/)
 [![Playground](https://img.shields.io/badge/playground-Go+-blue.svg)](https://play.goplus.org/)
 [![VSCode](https://img.shields.io/badge/vscode-Go+-teal.svg)](https://github.com/gopcode/vscode-goplus)
-[![Readme](https://img.shields.io/badge/README-%E4%B8%AD%E6%96%87-teal.svg)](https://github.com/goplus/gop/blob/main/README.md)
+[![Readme](https://img.shields.io/badge/README-%E4%B8%AD%E6%96%87-teal.svg)](https://github.com/goplus/gop/blob/main/README_zh.md)
 
--   **对于工程领域而言** ：意味着大家将会使用一门简单、易学、好用的编程语言。
--   **对于 STEM 教育而言** ：掌握一门可能会在将来工作中用到的工程语言将大有裨益。
--   **对于数据科学家而言** ：意味着你们将和工程师使用同一种语言进行合作和交流。
+* **对于工程领域而言** ：意味着大家将会使用一门简单、易学、好用的编程语言。
+* **对于 STEM 教育而言** ：掌握一门可能会在将来工作中用到的工程语言将大有裨益。
+* **对于数据科学家而言** ：意味着你们将和工程师使用同一种语言进行合作和交流。
 
 ## 如何安装 Go+?
 
@@ -20,15 +20,9 @@
 ```bash
 git clone https://github.com/goplus/gop.git
 cd gop
-
-# On linux run:
 ./all.bash
-# On Windows run:
-all.bat
 ```
 
-实际上， `all.bash` 和 `all.bat` 内部都会执行 `go run cmd/install.go`
-
 ## 代码风格（很重要）
 
 -   [https://tutorial.goplus.org/hello-world](https://tutorial.goplus.org/hello-world)
@@ -39,7 +33,7 @@ all.bat
 
 -   一种静态类型语言。
 -   完全兼容 [Go 语言](https://github.com/golang/go) 。
--   类似脚本语言的风格，代码比 Go 更易读。
+-   脚本化的风格，以及比 Go 更易读的数据科学代码。
 
 例如，以下是合法的 Go+ 源代码：
 
diff --git a/cl/compile.go b/cl/compile.go
index 4bf51367..5c150ebc 100644
--- a/cl/compile.go
+++ b/cl/compile.go
@@ -560,15 +560,6 @@ func preloadFile(p *gox.Package, parent *pkgCtx, file string, f *ast.File, targe
 				}
 				for _, v := range specs {
 					spec := v.(*ast.ValueSpec)
-					if spec.Type == nil {
-						pos := ctx.Position(v.Pos())
-						ctx.handleCodeErrorf(&pos, "missing field type in class file")
-						continue
-					}
-					if len(spec.Values) > 0 {
-						pos := ctx.Position(v.Pos())
-						ctx.handleCodeErrorf(&pos, "cannot assign value to field in class file")
-					}
 					typ := toType(ctx, spec.Type)
 					for _, name := range spec.Names {
 						flds = append(flds, types.NewField(name.Pos(), pkg, name.Name, typ, false))
diff --git a/cmd/goprun/goprun.go b/cmd/goprun/goprun.go
index dad15276..9aedf1b5 100644
--- a/cmd/goprun/goprun.go
+++ b/cmd/goprun/goprun.go
@@ -15,9 +15,10 @@ func main() {
 		return
 	}
 	ctx := gopmod.New("")
-	goProj, err := ctx.OpenProject(os.Args[1])
+	goProj, err := ctx.OpenProject(0, os.Args[1])
 	if err != nil {
-		log.Println(err)
+		fmt.Fprint(os.Stderr, "OpenProject failed:", err)
+		return
 	}
 	goProj.ExecArgs = os.Args[2:]
 	cmd := ctx.GoCommand("run", goProj)
diff --git a/cmd/internal/clean/clean.go b/cmd/internal/clean/clean.go
index e0e896b2..cac9e97b 100644
--- a/cmd/internal/clean/clean.go
+++ b/cmd/internal/clean/clean.go
@@ -18,13 +18,12 @@ package clean
 
 import (
 	"fmt"
+	"github.com/qiniu/x/log"
 	"io/ioutil"
 	"os"
 	"path/filepath"
 	"strings"
 
-	"github.com/qiniu/x/log"
-
 	"github.com/goplus/gop/cmd/internal/base"
 )
 
@@ -84,7 +83,7 @@ func removeGopDir(dir string) {
 
 // -----------------------------------------------------------------------------
 
-// Cmd - gop clean
+// Cmd - gop build
 var Cmd = &base.Command{
 	UsageLine: "gop clean [-v] <gopSrcDir>",
 	Short:     "Clean all Go+ auto generated files",
diff --git a/cmd/internal/env/env.go b/cmd/internal/env/env.go
index bc502bab..1bba50ff 100644
--- a/cmd/internal/env/env.go
+++ b/cmd/internal/env/env.go
@@ -74,23 +74,10 @@ func runCmd(_ *base.Command, args []string) {
 	gopEnv["GOPMOD"], _ = env.GOPMOD("")
 	gopEnv["HOME"] = env.HOME()
 
+	onlyValues := false
 	vars := flag.Args()
-
-	outputEnvVars(gopEnv, vars, *envJson)
-}
-
-func outputEnvVars(gopEnv map[string]interface{}, vars []string, outputJson bool) {
-	onlyValues := true
-
-	if len(vars) == 0 {
-		onlyValues = false
-
-		vars = make([]string, 0, len(gopEnv))
-		for k := range gopEnv {
-			vars = append(vars, k)
-		}
-		sort.Strings(vars)
-	} else {
+	if len(vars) > 0 {
+		onlyValues = true
 		newEnv := make(map[string]interface{})
 		for _, v := range vars {
 			if value, ok := gopEnv[v]; ok {
@@ -102,6 +89,10 @@ func outputEnvVars(gopEnv map[string]interface{}, vars []string, outputJson bool
 		gopEnv = newEnv
 	}
 
+	outputEnvVars(gopEnv, onlyValues, *envJson)
+}
+
+func outputEnvVars(gopEnv map[string]interface{}, onlyValues bool, outputJson bool) {
 	if outputJson {
 		b, err := json.Marshal(gopEnv)
 		if err != nil {
@@ -109,10 +100,15 @@ func outputEnvVars(gopEnv map[string]interface{}, vars []string, outputJson bool
 		}
 
 		var out bytes.Buffer
-		json.Indent(&out, b, "", "\t")
+		json.Indent(&out, b, "", "  ")
 		fmt.Println(out.String())
 	} else {
-		for _, k := range vars {
+		keys := make([]string, 0, len(gopEnv))
+		for k := range gopEnv {
+			keys = append(keys, k)
+		}
+		sort.Strings(keys)
+		for _, k := range keys {
 			v := gopEnv[k]
 			if onlyValues {
 				fmt.Printf("%v\n", v)
diff --git a/cmd/internal/modload/modload.go b/cmd/internal/modload/modload.go
index cd30b4c1..ece6af3e 100644
--- a/cmd/internal/modload/modload.go
+++ b/cmd/internal/modload/modload.go
@@ -219,9 +219,6 @@ func CreateModFile(modPath string) {
 
 func Load() {
 	LoadModFile()
-	if modRoot == "" {
-		return
-	}
 	SyncGoMod()
 	if classModFile != nil && classModFile.Classfile != nil {
 		gengo.RegisterPkgFlags(classModFile.Classfile.ProjExt, gengo.PkgFlagGmx)
@@ -263,9 +260,6 @@ func fixGoVersion(fixed *bool) gomodfile.VersionFixer {
 // -mod wasn't set explicitly and automatic vendoring should be enabled.
 func LoadModFile() {
 	Init()
-	if modRoot == "" {
-		return
-	}
 	// If gop.mod does not exist, then modroot does not exist,
 	// and if go.mod exists then a copy of go.mod will be synchronized to gop.mod
 	gopmod := GopModFilePath()
diff --git a/cmd/internal/run/mod.go b/cmd/internal/run/mod.go
index d43e5cfe..a2b8db7c 100644
--- a/cmd/internal/run/mod.go
+++ b/cmd/internal/run/mod.go
@@ -1,9 +1,12 @@
 package run
 
 import (
+	"os"
+	"os/exec"
 	"path/filepath"
 
 	"github.com/goplus/gop/env"
+	"github.com/goplus/gop/x/gopmod"
 	"github.com/qiniu/x/log"
 )
 
@@ -23,3 +26,34 @@ func findGoModDir(dir string) (string, bool) {
 	}
 	return filepath.Dir(modfile), nocachefile
 }
+
+func gopRun(source string, args ...string) {
+	ctx := gopmod.New("")
+	flags := 0
+	if *flagGop {
+		flags = gopmod.FlagGoAsGoPlus
+	}
+	goProj, err := ctx.OpenProject(flags, source)
+	if err != nil {
+		log.Fatalln("OpenProject failed:", err)
+	}
+	goProj.ExecArgs = args
+	goProj.FlagRTOE = *flagRTOE
+	goProj.FlagNRINC = *flagNorun
+	cmd := ctx.GoCommand("run", goProj)
+	if cmd.IsValid() {
+		cmd.Stdin = os.Stdin
+		cmd.Stdout = os.Stdout
+		cmd.Stderr = os.Stderr
+		cmd.Env = os.Environ()
+		err = cmd.Run()
+		if err != nil {
+			switch e := err.(type) {
+			case *exec.ExitError:
+				os.Exit(e.ExitCode())
+			default:
+				log.Fatalln(err)
+			}
+		}
+	}
+}
diff --git a/cmd/internal/run/run.go b/cmd/internal/run/run.go
index 4a4cd838..47def396 100644
--- a/cmd/internal/run/run.go
+++ b/cmd/internal/run/run.go
@@ -18,8 +18,6 @@
 package run
 
 import (
-	"crypto/sha1"
-	"encoding/base64"
 	"fmt"
 	"os"
 	"os/exec"
@@ -113,9 +111,8 @@ func runCmd(cmd *base.Command, args []string) {
 	isDir := fi.IsDir()
 
 	var isDirty bool
-	var srcDir, file, gofile string
+	var srcDir, gofile string
 	var pkgs map[string]*ast.Package
-	onExit = nil
 	if isDir {
 		srcDir = src
 		gofile = src + "/gop_autogen.go"
@@ -127,37 +124,8 @@ func runCmd(cmd *base.Command, args []string) {
 			return
 		}
 	} else {
-		srcDir, file = filepath.Split(src)
-		modload.Load()
-		isGo := filepath.Ext(file) == ".go"
-		if isGo {
-			hash := sha1.Sum([]byte(src))
-			dir := os.Getenv("HOME") + "/.gop/run"
-			os.MkdirAll(dir, 0755)
-			gofile = dir + "/g" + base64.RawURLEncoding.EncodeToString(hash[:]) + file
-			if *flagRTOE { // remove tempfile on error
-				onExit = func() {
-					os.Remove(gofile)
-				}
-			}
-		} else if hasMultiFiles(srcDir, ".gop") {
-			gofile = filepath.Join(srcDir, "gop_autogen_"+file+".go")
-		} else {
-			gofile = srcDir + "/gop_autogen.go"
-		}
-		isDirty = fileIsDirty(fi, gofile)
-		if isDirty {
-			if isGo {
-				fmt.Println("==> GenGo to", gofile)
-			}
-			if *flagGop {
-				pkgs, err = parser.Parse(fset, src, nil, parserMode)
-			} else {
-				pkgs, err = parser.Parse(fset, src, nil, 0) // TODO: only to check dependencies
-			}
-		} else if *flagNorun {
-			return
-		}
+		gopRun(src, args...)
+		return
 	}
 	if err != nil {
 		scanner.PrintError(os.Stderr, err)
@@ -196,18 +164,6 @@ func runCmd(cmd *base.Command, args []string) {
 	}
 }
 
-func fileIsDirty(fi os.FileInfo, gofile string) bool {
-	fiDest, err := os.Stat(gofile)
-	if err != nil {
-		return true
-	}
-	return fi.ModTime().After(fiDest.ModTime())
-}
-
-var (
-	onExit func()
-)
-
 func goRun(file string, args []string) {
 	goArgs := make([]string, len(args)+2)
 	goArgs[0] = "run"
@@ -220,9 +176,6 @@ func goRun(file string, args []string) {
 	cmd.Env = os.Environ()
 	err := cmd.Run()
 	if err != nil {
-		if onExit != nil {
-			onExit()
-		}
 		switch e := err.(type) {
 		case *exec.ExitError:
 			os.Exit(e.ExitCode())
@@ -270,21 +223,4 @@ func runGoPkg(src string, args []string, doRun bool) {
 	}
 }
 
-func hasMultiFiles(srcDir string, ext string) bool {
-	var has bool
-	if f, err := os.Open(srcDir); err == nil {
-		defer f.Close()
-		fis, _ := f.ReadDir(-1)
-		for _, fi := range fis {
-			if !fi.IsDir() && filepath.Ext(fi.Name()) == ext {
-				if has {
-					return true
-				}
-				has = true
-			}
-		}
-	}
-	return false
-}
-
 // -----------------------------------------------------------------------------
diff --git a/x/gopmod/gocmd.go b/x/gopmod/gocmd.go
index e5c7be00..81c79fbc 100644
--- a/x/gopmod/gocmd.go
+++ b/x/gopmod/gocmd.go
@@ -18,6 +18,7 @@ package gopmod
 
 import (
 	"fmt"
+	"os"
 	"os/exec"
 	"path/filepath"
 
@@ -52,17 +53,19 @@ func LoadFlags() string {
 
 type GoCmd struct {
 	*exec.Cmd
-	after func() error
+	after func(error) error
+}
+
+func (p GoCmd) IsValid() bool {
+	return p.Cmd != nil
 }
 
 func (p GoCmd) Run() error {
-	if err := p.Cmd.Run(); err != nil {
-		return err
-	}
+	err := p.Cmd.Run()
 	if p.after != nil {
-		return p.after()
+		return p.after(err)
 	}
-	return nil
+	return err
 }
 
 func goCommand(dir, op string, t *goTarget) (ret GoCmd) {
@@ -76,8 +79,15 @@ func goCommand(dir, op string, t *goTarget) (ret GoCmd) {
 		dir, _ = filepath.Split(t.goFile)
 		exargs[0] = "build"
 		exargs = append(exargs, "-o", t.outFile, t.goFile)
-		ret.after = func() error {
-			return runCommand(afterDir, t.outFile, proj.ExecArgs...)
+		ret.after = func(e error) error {
+			if e == nil {
+				e = runCommand(afterDir, t.outFile, proj.ExecArgs...)
+			}
+			if e != nil && t.proj.FlagRTOE { // remove tempfile on error
+				os.Remove(t.goFile)
+				os.Remove(t.outFile)
+			}
+			return e
 		}
 	} else {
 		exargs = append(exargs, t.goFile)         // 1
diff --git a/x/gopmod/gopfiles.go b/x/gopmod/gopfiles.go
index eed29908..4868c385 100644
--- a/x/gopmod/gopfiles.go
+++ b/x/gopmod/gopfiles.go
@@ -38,7 +38,11 @@ type gopFiles struct {
 
 func (p *Context) openFromGopFiles(files []string) (proj *Project, err error) {
 	proj = &Project{
-		Source: &gopFiles{files: files},
+		Source:        &gopFiles{files: files},
+		UseDefaultCtx: true,
+	}
+	if len(files) == 1 {
+		proj.FriendlyFname = filepath.Base(files[0])
 	}
 	return
 }
diff --git a/x/gopmod/gopmod.go b/x/gopmod/gopmod.go
index 38a87191..a73e3f0e 100644
--- a/x/gopmod/gopmod.go
+++ b/x/gopmod/gopmod.go
@@ -43,6 +43,9 @@ type Project struct {
 	FriendlyFname string // friendly fname of source
 	BuildArgs     []string
 	ExecArgs      []string
+	UseDefaultCtx bool
+	FlagNRINC     bool // do not run if not changed
+	FlagRTOE      bool // remove tempfile on error
 }
 
 type Context struct {
@@ -52,19 +55,25 @@ type Context struct {
 }
 
 func New(dir string) *Context {
-	defctx := false
 	modfile, err := env.GOPMOD(dir)
 	if err != nil {
-		modfile = env.HOME() + "/.gop/cmd/go.mod"
-		if _, err := os.Stat(modfile); os.IsNotExist(err) {
-			genDefaultGopMod(modfile)
-		}
-		defctx = true
+		return NewDefault(dir)
 	}
-	return &Context{modfile: modfile, dir: dir, defctx: defctx}
+	return &Context{modfile: modfile, dir: dir}
+}
+
+func NewDefault(dir string) *Context {
+	modfile := env.HOME() + "/.gop/run/go.mod"
+	if _, err := os.Stat(modfile); os.IsNotExist(err) {
+		genDefaultGopMod(modfile)
+	}
+	return &Context{modfile: modfile, dir: dir, defctx: true}
 }
 
 func (p *Context) GoCommand(op string, src *Project) GoCmd {
+	if src.UseDefaultCtx {
+		p = NewDefault(p.dir)
+	}
 	out := p.out(src)
 	if src.IsDirty(out.goFile, p.defctx) {
 		if p.defctx {
@@ -74,6 +83,8 @@ func (p *Context) GoCommand(op string, src *Project) GoCmd {
 		if err := src.GenGo(out.goFile, p.modfile); err != nil {
 			log.Panicln(err)
 		}
+	} else if src.FlagNRINC { // do not run if not changed
+		return GoCmd{}
 	}
 	return goCommand(p.dir, op, &out)
 }
diff --git a/x/gopmod/gopproj.go b/x/gopmod/gopproj.go
index e9d80440..5ca47e11 100644
--- a/x/gopmod/gopproj.go
+++ b/x/gopmod/gopproj.go
@@ -23,7 +23,11 @@ import (
 
 // -----------------------------------------------------------------------------
 
-func (p *Context) OpenProject(args ...string) (proj *Project, err error) {
+const (
+	FlagGoAsGoPlus = 1 << iota
+)
+
+func (p *Context) OpenProject(flags int, args ...string) (proj *Project, err error) {
 	if len(args) != 1 {
 		return p.openFromGopFiles(args)
 	}
@@ -35,7 +39,7 @@ func (p *Context) OpenProject(args ...string) (proj *Project, err error) {
 	if fi.IsDir() {
 		return p.openFromDir(src)
 	}
-	if filepath.Ext(src) == ".go" {
+	if (flags&FlagGoAsGoPlus) == 0 && filepath.Ext(src) == ".go" {
 		return openFromGoFile(src)
 	}
 	return p.openFromGopFiles(args)


Test patch:
diff --git a/cl/compile_spx_test.go b/cl/compile_spx_test.go
index 04b20236..db874b23 100644
--- a/cl/compile_spx_test.go
+++ b/cl/compile_spx_test.go
@@ -73,50 +73,6 @@ func gopSpxTestEx(t *testing.T, gmx, spxcode, expected, gmxfile, spxfile string)
 	}
 }
 
-func gopSpxErrorTestEx(t *testing.T, msg, gmx, spxcode, gmxfile, spxfile string) {
-	fs := newTwoFileFS("/foo", spxfile, spxcode, gmxfile, gmx)
-	pkgs, err := parser.ParseFSDir(gblFset, fs, "/foo", nil, 0)
-	if err != nil {
-		scanner.PrintError(os.Stderr, err)
-		t.Fatal("ParseFSDir:", err)
-	}
-	conf := *baseConf.Ensure()
-	conf.NoFileLine = false
-	conf.WorkingDir = "/foo"
-	conf.TargetDir = "/foo"
-	bar := pkgs["main"]
-	_, err = cl.NewPackage("", bar, &conf)
-	if err == nil {
-		t.Fatal("no error?")
-	}
-	if ret := err.Error(); ret != msg {
-		t.Fatalf("\nError: \"%s\"\nExpected: \"%s\"\n", ret, msg)
-	}
-}
-
-func TestSpxError(t *testing.T) {
-	gopSpxErrorTestEx(t, `./Game.tgmx:4:2: cannot assign value to field in class file`, `
-var (
-	Kai Kai
-	userScore int = 100
-)
-`, `
-println "hi"
-`, "Game.tgmx", "Kai.tspx")
-
-	gopSpxErrorTestEx(t, `./Kai.tspx:3:2: missing field type in class file`, `
-var (
-	Kai Kai
-	userScore int
-)
-`, `
-var (
-	id = 100
-)
-println "hi"
-`, "Game.tgmx", "Kai.tspx")
-}
-
 func TestSpxBasic(t *testing.T) {
 	gopSpxTestEx(t, `
 func onInit() {
diff --git a/parser/parserdir_test.go b/parser/parserdir_test.go
index 9db148cb..3624a7dc 100644
--- a/parser/parserdir_test.go
+++ b/parser/parserdir_test.go
@@ -22,6 +22,7 @@ import (
 	"os"
 	"path"
 	"reflect"
+	"runtime"
 	"strings"
 	"testing"
 
@@ -132,6 +133,9 @@ func TestParseGoFiles(t *testing.T) {
 }
 
 func TestFromTestdata(t *testing.T) {
+	if runtime.GOOS == "windows" {
+		return // TODO: how to pass this testcase in windows
+	}
 	sel := ""
 	dir, err := os.Getwd()
 	if err != nil {
diff --git a/printer/gop_test.go b/printer/gop_test.go
index 13cd9703..aa776a31 100644
--- a/printer/gop_test.go
+++ b/printer/gop_test.go
@@ -21,6 +21,7 @@ import (
 	"io/ioutil"
 	"os"
 	"path/filepath"
+	"runtime"
 	"testing"
 
 	"github.com/goplus/gop/ast"
@@ -117,6 +118,9 @@ func testFrom(t *testing.T, fpath string) {
 }
 
 func TestFromTestdata(t *testing.T) {
+	if runtime.GOOS == "windows" {
+		return // TODO: how to pass this testcase in windows
+	}
 	dir, err := os.Getwd()
 	if err != nil {
 		t.Fatal("Getwd failed:", err)
@@ -135,6 +139,9 @@ func TestFromTestdata(t *testing.T) {
 }
 
 func TestFromParse(t *testing.T) {
+	if runtime.GOOS == "windows" {
+		return // TODO: how to pass this testcase in windows
+	}
 	dir, err := os.Getwd()
 	if err != nil {
 		t.Fatal("Getwd failed:", err)
diff --git a/x/mod/modfile/read_test.go b/x/mod/modfile/read_test.go
index 054013a9..c278ef07 100644
--- a/x/mod/modfile/read_test.go
+++ b/x/mod/modfile/read_test.go
@@ -23,6 +23,7 @@ import (
 	"os/exec"
 	"path/filepath"
 	"reflect"
+	"runtime"
 	"strings"
 	"testing"
 )
@@ -30,6 +31,9 @@ import (
 // Test that reading and then writing the golden files
 // does not change their output.
 func TestPrintGolden(t *testing.T) {
+	if runtime.GOOS == "windows" {
+		return // TODO: how to pass this testcase in windows
+	}
 	outs, err := filepath.Glob("testdata/*.golden")
 	if err != nil {
 		t.Fatal(err)
@@ -150,6 +154,9 @@ func TestParseLax(t *testing.T) {
 // and printed and parsed again, we get the same parse tree
 // both times.
 func TestPrintParse(t *testing.T) {
+	if runtime.GOOS == "windows" {
+		return // TODO: how to pass this testcase in windows
+	}
 	outs, err := filepath.Glob("testdata/*")
 	if err != nil {
 		t.Fatal(err)