Repository: goplus/gop
Pull request #1023 (2021-12-03 14:20:44)

fix gopfmt & gop fmt report error
fix https://github.com/goplus/gop/issues/1020


Patch:
diff --git a/cmd/gopfmt/gopfmt.go b/cmd/gopfmt/gopfmt.go
index afadf841..a74afa08 100644
--- a/cmd/gopfmt/gopfmt.go
+++ b/cmd/gopfmt/gopfmt.go
@@ -69,7 +69,7 @@ func processFile(filename string, in io.Reader, out io.Writer) error {
 		return err
 	}
 
-	res, err := format.Source(src)
+	res, err := format.Source(filename, src)
 	if err != nil {
 		return err
 	}
@@ -111,7 +111,9 @@ func walk(path string, d fs.DirEntry, err error) error {
 		ext := filepath.Ext(path)
 		if _, ok := extGops[ext]; ok {
 			procCnt++
-			err = processFile(path, nil, os.Stdout)
+			if err = processFile(path, nil, os.Stdout); err != nil {
+				report(err)
+			}
 		}
 	}
 	return err
diff --git a/cmd/internal/gopfmt/fmt.go b/cmd/internal/gopfmt/fmt.go
index 0d4ded32..0be10663 100644
--- a/cmd/internal/gopfmt/fmt.go
+++ b/cmd/internal/gopfmt/fmt.go
@@ -70,9 +70,9 @@ func gopfmt(path string, smart, mvgo bool) (err error) {
 	}
 	var target []byte
 	if smart {
-		target, err = xformat.GopstyleSource(src)
+		target, err = xformat.GopstyleSource(path, src)
 	} else {
-		target, err = format.Source(src)
+		target, err = format.Source(path, src)
 	}
 	if err != nil {
 		return
@@ -127,12 +127,20 @@ func walk(path string, d fs.DirEntry, err error) error {
 				smart := *flagSmart
 				mvgo := smart && *flagMoveGo
 				err = gopfmt(path, smart && (mvgo || ext != ".go"), mvgo)
+				if err != nil {
+					report(err)
+				}
 			}
 		}
 	}
 	return err
 }
 
+func report(err error) {
+	fmt.Println(err)
+	os.Exit(2)
+}
+
 func runCmd(cmd *base.Command, args []string) {
 	err := flag.Parse(args)
 	if err != nil {
diff --git a/format/format.go b/format/format.go
index 9e05c16a..63d92793 100644
--- a/format/format.go
+++ b/format/format.go
@@ -101,9 +101,9 @@ func Node(dst io.Writer, fset *token.FileSet, node interface{}) error {
 // space as src), and the result is indented by the same amount as the first
 // line of src containing code. Imports are not sorted for partial source files.
 //
-func Source(src []byte) ([]byte, error) {
+func Source(filename string, src []byte) ([]byte, error) {
 	fset := token.NewFileSet()
-	file, sourceAdj, indentAdj, err := parse(fset, "", src, true)
+	file, sourceAdj, indentAdj, err := parse(fset, filename, src, true)
 	if err != nil {
 		return nil, err
 	}
diff --git a/x/format/gopstyle.go b/x/format/gopstyle.go
index 3f6139ba..7fba5f83 100644
--- a/x/format/gopstyle.go
+++ b/x/format/gopstyle.go
@@ -11,9 +11,10 @@ import (
 
 // -----------------------------------------------------------------------------
 
-func GopstyleSource(src []byte) (ret []byte, err error) {
+func GopstyleSource(filename string, src []byte) (ret []byte, err error) {
 	fset := token.NewFileSet()
-	if f, err := parser.ParseFile(fset, "", src, parser.ParseComments); err == nil {
+	var f *ast.File
+	if f, err = parser.ParseFile(fset, filename, src, parser.ParseComments); err == nil {
 		Gopstyle(f)
 		var buf bytes.Buffer
 		if err = format.Node(&buf, fset, f); err == nil {


Test patch:
diff --git a/printer/gop_test.go b/printer/gop_test.go
index 13cd9703..329aaa22 100644
--- a/printer/gop_test.go
+++ b/printer/gop_test.go
@@ -108,7 +108,7 @@ func testFrom(t *testing.T, fpath string) {
 		t.Fatal(err)
 	}
 
-	res, err := format.Source(src)
+	res, err := format.Source(fpath, src)
 	if err != nil {
 		t.Fatal("Source failed:", err)
 	}
diff --git a/x/format/gopstyle_test.go b/x/format/gopstyle_test.go
index 2b9c88e4..77dfe072 100644
--- a/x/format/gopstyle_test.go
+++ b/x/format/gopstyle_test.go
@@ -8,7 +8,7 @@ import (
 
 func testFormat(t *testing.T, name string, src, expect string) {
 	t.Run(name, func(t *testing.T) {
-		result, err := GopstyleSource([]byte(src))
+		result, err := GopstyleSource(name, []byte(src))
 		if err != nil {
 			t.Fatal("format.Source failed:", err)
 		}