From 9bad6cb512b73e1ecd272131c1dbf55ab3d4e590 Mon Sep 17 00:00:00 2001 From: Thomas Bruyelle Date: Wed, 25 Oct 2023 23:32:39 +0200 Subject: [PATCH] refac!: remove `gno build` command Closes: #1242 In favor of `gno precompile --build` --- gnovm/cmd/gno/build.go | 92 ------------------- gnovm/cmd/gno/build_test.go | 11 --- gnovm/cmd/gno/main.go | 1 - gnovm/cmd/gno/precompile.go | 41 ++++++++- gnovm/cmd/gno/precompile_test.go | 19 ++-- gnovm/cmd/gno/test.go | 7 +- .../gno/testdata/gno_build/empty_dir.txtar | 6 -- .../gno_build/invalid_gno_files.txtar | 27 ------ .../testdata/gno_build/invalid_go_files.txtar | 30 ------ .../cmd/gno/testdata/gno_build/no_args.txtar | 6 -- .../gno/testdata/gno_build/no_gno_files.txtar | 12 --- .../gno/testdata/gno_build/no_go_files.txtar | 19 ---- .../cmd/gno/testdata/gno_build/no_gomod.txtar | 16 ---- gnovm/cmd/gno/testdata/gno_build/ok.txtar | 23 ----- .../testdata/gno_precompile/01_no_args.txtar | 6 ++ .../gno_precompile/02_empty_dir.txtar | 6 ++ .../gno_precompile/03_invalid_gno_files.txtar | 19 ++++ .../gno_precompile/04_valid_gno_files.txtar | 32 +++++++ .../gno_precompile/05_skip_fmt_flag.txtar | 34 +++++++ .../gno_precompile/06_build_flag.txtar | 36 ++++++++ .../07_build_flag_with_build_error.txtar | 34 +++++++ gnovm/pkg/gnolang/precompile.go | 5 +- 22 files changed, 220 insertions(+), 262 deletions(-) delete mode 100644 gnovm/cmd/gno/build.go delete mode 100644 gnovm/cmd/gno/build_test.go delete mode 100644 gnovm/cmd/gno/testdata/gno_build/empty_dir.txtar delete mode 100644 gnovm/cmd/gno/testdata/gno_build/invalid_gno_files.txtar delete mode 100644 gnovm/cmd/gno/testdata/gno_build/invalid_go_files.txtar delete mode 100644 gnovm/cmd/gno/testdata/gno_build/no_args.txtar delete mode 100644 gnovm/cmd/gno/testdata/gno_build/no_gno_files.txtar delete mode 100644 gnovm/cmd/gno/testdata/gno_build/no_go_files.txtar delete mode 100644 gnovm/cmd/gno/testdata/gno_build/no_gomod.txtar delete mode 100644 gnovm/cmd/gno/testdata/gno_build/ok.txtar create mode 100644 gnovm/cmd/gno/testdata/gno_precompile/01_no_args.txtar create mode 100644 gnovm/cmd/gno/testdata/gno_precompile/02_empty_dir.txtar create mode 100644 gnovm/cmd/gno/testdata/gno_precompile/03_invalid_gno_files.txtar create mode 100644 gnovm/cmd/gno/testdata/gno_precompile/04_valid_gno_files.txtar create mode 100644 gnovm/cmd/gno/testdata/gno_precompile/05_skip_fmt_flag.txtar create mode 100644 gnovm/cmd/gno/testdata/gno_precompile/06_build_flag.txtar create mode 100644 gnovm/cmd/gno/testdata/gno_precompile/07_build_flag_with_build_error.txtar diff --git a/gnovm/cmd/gno/build.go b/gnovm/cmd/gno/build.go deleted file mode 100644 index b80711586e4..00000000000 --- a/gnovm/cmd/gno/build.go +++ /dev/null @@ -1,92 +0,0 @@ -package main - -import ( - "context" - "flag" - "fmt" - "os" - - gno "github.com/gnolang/gno/gnovm/pkg/gnolang" - "github.com/gnolang/gno/tm2/pkg/commands" -) - -type buildCfg struct { - verbose bool - goBinary string -} - -var defaultBuildOptions = &buildCfg{ - verbose: false, - goBinary: "go", -} - -func newBuildCmd(io *commands.IO) *commands.Command { - cfg := &buildCfg{} - - return commands.NewCommand( - commands.Metadata{ - Name: "build", - ShortUsage: "build [flags] ", - ShortHelp: "Builds the specified gno package", - }, - cfg, - func(_ context.Context, args []string) error { - return execBuild(cfg, args, io) - }, - ) -} - -func (c *buildCfg) RegisterFlags(fs *flag.FlagSet) { - fs.BoolVar( - &c.verbose, - "verbose", - defaultBuildOptions.verbose, - "verbose output when building", - ) - - fs.StringVar( - &c.goBinary, - "go-binary", - defaultBuildOptions.goBinary, - "go binary to use for building", - ) -} - -func execBuild(cfg *buildCfg, args []string, io *commands.IO) error { - if len(args) < 1 { - return flag.ErrHelp - } - - paths, err := gnoPackagesFromArgs(args) - if err != nil { - return fmt.Errorf("list packages: %w", err) - } - - errCount := 0 - for _, pkgPath := range paths { - err = goBuildFileOrPkg(pkgPath, cfg) - if err != nil { - err = fmt.Errorf("%s: build pkg: %w", pkgPath, err) - io.ErrPrintfln("%s\n", err.Error()) - - errCount++ - } - } - - if errCount > 0 { - return fmt.Errorf("%d go build errors", errCount) - } - - return nil -} - -func goBuildFileOrPkg(fileOrPkg string, cfg *buildCfg) error { - verbose := cfg.verbose - goBinary := cfg.goBinary - - if verbose { - fmt.Fprintf(os.Stderr, "%s\n", fileOrPkg) - } - - return gno.PrecompileBuildPackage(fileOrPkg, goBinary) -} diff --git a/gnovm/cmd/gno/build_test.go b/gnovm/cmd/gno/build_test.go deleted file mode 100644 index 5bb03ef0d35..00000000000 --- a/gnovm/cmd/gno/build_test.go +++ /dev/null @@ -1,11 +0,0 @@ -package main - -import ( - "testing" - - "github.com/rogpeppe/go-internal/testscript" -) - -func TestBuild(t *testing.T) { - testscript.Run(t, setupTestScript(t, "testdata/gno_build")) -} diff --git a/gnovm/cmd/gno/main.go b/gnovm/cmd/gno/main.go index 433db703b90..d2d5881f1e6 100644 --- a/gnovm/cmd/gno/main.go +++ b/gnovm/cmd/gno/main.go @@ -33,7 +33,6 @@ func newGnocliCmd(io *commands.IO) *commands.Command { newTestCmd(io), newLintCmd(io), newRunCmd(io), - newBuildCmd(io), newPrecompileCmd(io), newCleanCmd(io), newReplCmd(), diff --git a/gnovm/cmd/gno/precompile.go b/gnovm/cmd/gno/precompile.go index 7e895109706..6744737ca3c 100644 --- a/gnovm/cmd/gno/precompile.go +++ b/gnovm/cmd/gno/precompile.go @@ -18,6 +18,7 @@ type precompileCfg struct { verbose bool skipFmt bool skipImports bool + gobuild bool goBinary string gofmtBinary string output string @@ -85,6 +86,13 @@ func (c *precompileCfg) RegisterFlags(fs *flag.FlagSet) { "do not precompile imports recursively", ) + fs.BoolVar( + &c.gobuild, + "gobuild", + false, + "run go build on generated go files, ignoring test files", + ) + fs.StringVar( &c.goBinary, "go-binary", @@ -125,7 +133,6 @@ func execPrecompile(cfg *precompileCfg, args []string, io *commands.IO) error { if err != nil { err = fmt.Errorf("%s: precompile: %w", filepath, err) io.ErrPrintfln("%s", err.Error()) - errCount++ } } @@ -134,6 +141,27 @@ func execPrecompile(cfg *precompileCfg, args []string, io *commands.IO) error { return fmt.Errorf("%d precompile errors", errCount) } + if cfg.gobuild { + paths, err := gnoPackagesFromArgs(args) + if err != nil { + return fmt.Errorf("list packages: %w", err) + } + + errCount = 0 + for _, pkgPath := range paths { + _ = pkgPath + err = goBuildFileOrPkg(pkgPath, cfg) + if err != nil { + err = fmt.Errorf("%s: build pkg: %w", pkgPath, err) + io.ErrPrintfln("%s\n", err.Error()) + errCount++ + } + } + if errCount > 0 { + return fmt.Errorf("%d build errors", errCount) + } + } + return nil } @@ -219,3 +247,14 @@ func precompileFile(srcPath string, opts *precompileOptions) error { return nil } + +func goBuildFileOrPkg(fileOrPkg string, cfg *precompileCfg) error { + verbose := cfg.verbose + goBinary := cfg.goBinary + + if verbose { + fmt.Fprintf(os.Stderr, "%s\n", fileOrPkg) + } + + return gno.PrecompileBuildPackage(fileOrPkg, goBinary) +} diff --git a/gnovm/cmd/gno/precompile_test.go b/gnovm/cmd/gno/precompile_test.go index 56f63b0de35..86f43362897 100644 --- a/gnovm/cmd/gno/precompile_test.go +++ b/gnovm/cmd/gno/precompile_test.go @@ -1,18 +1,11 @@ package main -import "testing" +import ( + "testing" -func TestPrecompileApp(t *testing.T) { - tc := []testMainCase{ - { - args: []string{"precompile"}, - errShouldBe: "flag: help requested", - }, + "github.com/rogpeppe/go-internal/testscript" +) - // {args: []string{"precompile", "..."}, stdoutShouldContain: "..."}, - // TODO: recursive - // TODO: valid files - // TODO: invalid files - } - testMainCaseRun(t, tc) +func TestPrecompile(t *testing.T) { + testscript.Run(t, setupTestScript(t, "testdata/gno_precompile")) } diff --git a/gnovm/cmd/gno/test.go b/gnovm/cmd/gno/test.go index 85fe3f7ee7d..40831e45df8 100644 --- a/gnovm/cmd/gno/test.go +++ b/gnovm/cmd/gno/test.go @@ -210,9 +210,10 @@ func execTest(cfg *testCfg, args []string, io *commands.IO) error { if verbose { io.ErrPrintfln("=== PREC %s", pkg.Dir) } - precompileOpts := newPrecompileOptions(&precompileCfg{ + precompileCfg := &precompileCfg{ output: tempdirRoot, - }) + } + precompileOpts := newPrecompileOptions(precompileCfg) err := precompilePkg(importPath(pkg.Dir), precompileOpts) if err != nil { io.ErrPrintln(err) @@ -231,7 +232,7 @@ func execTest(cfg *testCfg, args []string, io *commands.IO) error { if err != nil { return errors.New("cannot resolve build dir") } - err = goBuildFileOrPkg(tempDir, defaultBuildOptions) + err = goBuildFileOrPkg(tempDir, precompileCfg) if err != nil { io.ErrPrintln(err) io.ErrPrintln("FAIL") diff --git a/gnovm/cmd/gno/testdata/gno_build/empty_dir.txtar b/gnovm/cmd/gno/testdata/gno_build/empty_dir.txtar deleted file mode 100644 index d346b6ad46f..00000000000 --- a/gnovm/cmd/gno/testdata/gno_build/empty_dir.txtar +++ /dev/null @@ -1,6 +0,0 @@ -# Run gno build on an empty dir - -gno build . - -! stdout .+ -! stderr .+ diff --git a/gnovm/cmd/gno/testdata/gno_build/invalid_gno_files.txtar b/gnovm/cmd/gno/testdata/gno_build/invalid_gno_files.txtar deleted file mode 100644 index 617e12291be..00000000000 --- a/gnovm/cmd/gno/testdata/gno_build/invalid_gno_files.txtar +++ /dev/null @@ -1,27 +0,0 @@ -# Run gno build with invalid gno files (still success) - -gno build . - -! stdout .+ -! stderr .+ - --- go.mod -- -module gnobuild - --- file1.go -- -package file1 - --- main.gno -- -package main - -invalid - -func main() {} - --- sub/sub.gno -- -package sub - -invalid - --- sub/file2.go -- -package file2 diff --git a/gnovm/cmd/gno/testdata/gno_build/invalid_go_files.txtar b/gnovm/cmd/gno/testdata/gno_build/invalid_go_files.txtar deleted file mode 100644 index a7c8b51af49..00000000000 --- a/gnovm/cmd/gno/testdata/gno_build/invalid_go_files.txtar +++ /dev/null @@ -1,30 +0,0 @@ -# Run gno build with invalid go files - -! gno build . - -! stdout .+ -stderr '\./file1\.go:3:1: syntax error: non-declaration statement outside function body' -stderr '\./\.: build pkg: std go compiler' -stderr 'sub/file2\.go:3:1: syntax error: non-declaration statement outside function body' -stderr '\./sub: build pkg: std go compiler' - --- go.mod -- -module gnobuild - --- file1.go -- -package file1 - -invalid1 - --- main.gno -- -package main - -func main() {} - --- sub/sub.gno -- -package sub - --- sub/file2.go -- -package file2 - -invalid2 diff --git a/gnovm/cmd/gno/testdata/gno_build/no_args.txtar b/gnovm/cmd/gno/testdata/gno_build/no_args.txtar deleted file mode 100644 index b3f68676588..00000000000 --- a/gnovm/cmd/gno/testdata/gno_build/no_args.txtar +++ /dev/null @@ -1,6 +0,0 @@ -# Run gno build without args - -! gno build - -! stdout .+ -stderr 'flag: help requested' diff --git a/gnovm/cmd/gno/testdata/gno_build/no_gno_files.txtar b/gnovm/cmd/gno/testdata/gno_build/no_gno_files.txtar deleted file mode 100644 index 58261e77cda..00000000000 --- a/gnovm/cmd/gno/testdata/gno_build/no_gno_files.txtar +++ /dev/null @@ -1,12 +0,0 @@ -# Run gno build on a dir w/o gno files - -gno build . - -! stdout .+ -! stderr .+ - --- README -- -Hello world - --- sub/README -- -Hello world diff --git a/gnovm/cmd/gno/testdata/gno_build/no_go_files.txtar b/gnovm/cmd/gno/testdata/gno_build/no_go_files.txtar deleted file mode 100644 index 46559610ccf..00000000000 --- a/gnovm/cmd/gno/testdata/gno_build/no_go_files.txtar +++ /dev/null @@ -1,19 +0,0 @@ -# Run gno build in a dir without go files - -! gno build . - -! stdout .+ -stderr -count=2 'no Go files in '$WORK -stderr '\./\.: build pkg: std go compiler' -stderr '\./sub: build pkg: std go compiler' - --- go.mod -- -module gnobuild - --- main.gno -- -package main - -func main() {} - --- sub/sub.gno -- -package sub diff --git a/gnovm/cmd/gno/testdata/gno_build/no_gomod.txtar b/gnovm/cmd/gno/testdata/gno_build/no_gomod.txtar deleted file mode 100644 index 9e6cad05664..00000000000 --- a/gnovm/cmd/gno/testdata/gno_build/no_gomod.txtar +++ /dev/null @@ -1,16 +0,0 @@ -# Run gno build on a dir without go.mod - -! gno build . - -! stdout .+ -stderr -count=2 'go: go.mod file not found in current directory or any parent directory' -stderr './.: build pkg: std go compiler' -stderr './sub: build pkg: std go compiler' - --- main.gno -- -package main - -func main() {} - --- sub/sub.gno -- -package sub diff --git a/gnovm/cmd/gno/testdata/gno_build/ok.txtar b/gnovm/cmd/gno/testdata/gno_build/ok.txtar deleted file mode 100644 index 9d70fd97904..00000000000 --- a/gnovm/cmd/gno/testdata/gno_build/ok.txtar +++ /dev/null @@ -1,23 +0,0 @@ -# Run gno build successfully - -gno build . - -! stdout .+ -! stderr .+ - --- go.mod -- -module gnobuild - --- file1.go -- -package file1 - --- main.gno -- -package main - -func main() {} - --- sub/sub.gno -- -package sub - --- sub/file2.go -- -package file2 diff --git a/gnovm/cmd/gno/testdata/gno_precompile/01_no_args.txtar b/gnovm/cmd/gno/testdata/gno_precompile/01_no_args.txtar new file mode 100644 index 00000000000..944c74ee615 --- /dev/null +++ b/gnovm/cmd/gno/testdata/gno_precompile/01_no_args.txtar @@ -0,0 +1,6 @@ +# Run gno precompile without args + +! gno precompile + +! stdout .+ +stderr 'flag: help requested' diff --git a/gnovm/cmd/gno/testdata/gno_precompile/02_empty_dir.txtar b/gnovm/cmd/gno/testdata/gno_precompile/02_empty_dir.txtar new file mode 100644 index 00000000000..7afc7ed0683 --- /dev/null +++ b/gnovm/cmd/gno/testdata/gno_precompile/02_empty_dir.txtar @@ -0,0 +1,6 @@ +# Run gno precompile on an empty dir + +gno precompile . + +! stdout .+ +! stderr .+ diff --git a/gnovm/cmd/gno/testdata/gno_precompile/03_invalid_gno_files.txtar b/gnovm/cmd/gno/testdata/gno_precompile/03_invalid_gno_files.txtar new file mode 100644 index 00000000000..26e12a9c750 --- /dev/null +++ b/gnovm/cmd/gno/testdata/gno_precompile/03_invalid_gno_files.txtar @@ -0,0 +1,19 @@ +# Run gno precompile with invalid gno files + +! gno precompile . + +! stdout .+ +stderr 'precompile: parse: main.gno:3:1: expected declaration, found invalid' +stderr 'precompile: parse: sub/sub.gno:3:1: expected declaration, found invalid' + +-- main.gno -- +package main + +invalid + +func main() {} + +-- sub/sub.gno -- +package sub + +invalid diff --git a/gnovm/cmd/gno/testdata/gno_precompile/04_valid_gno_files.txtar b/gnovm/cmd/gno/testdata/gno_precompile/04_valid_gno_files.txtar new file mode 100644 index 00000000000..0f9d909f627 --- /dev/null +++ b/gnovm/cmd/gno/testdata/gno_precompile/04_valid_gno_files.txtar @@ -0,0 +1,32 @@ +# Run gno precompile with valid gno files + +gno precompile . + +! stdout .+ +! stderr .+ + +cmp main.gno.gen.go main.gno.gen.go.golden +cmp sub/sub.gno.gen.go sub/sub.gno.gen.go.golden + +-- main.gno -- +package main + +func main(){} + +-- sub/sub.gno -- +package sub + +-- main.gno.gen.go.golden -- +// Code generated by github.com/gnolang/gno. DO NOT EDIT. + +//go:build gno + +package main + +func main() {} +-- sub/sub.gno.gen.go.golden -- +// Code generated by github.com/gnolang/gno. DO NOT EDIT. + +//go:build gno + +package sub diff --git a/gnovm/cmd/gno/testdata/gno_precompile/05_skip_fmt_flag.txtar b/gnovm/cmd/gno/testdata/gno_precompile/05_skip_fmt_flag.txtar new file mode 100644 index 00000000000..0f7780efdad --- /dev/null +++ b/gnovm/cmd/gno/testdata/gno_precompile/05_skip_fmt_flag.txtar @@ -0,0 +1,34 @@ +# Run gno precompile with -skip-fmt flag +# NOTE(tb): this flag doesn't actually prevent the code format, because +# `gnolang.Precompile()` calls `format.Node()`. + +gno precompile -skip-fmt . + +! stdout .+ +! stderr .+ + +cmp main.gno.gen.go main.gno.gen.go.golden +cmp sub/sub.gno.gen.go sub/sub.gno.gen.go.golden + +-- main.gno -- +package main + +func main(){} + +-- sub/sub.gno -- +package sub + +-- main.gno.gen.go.golden -- +// Code generated by github.com/gnolang/gno. DO NOT EDIT. + +//go:build gno + +package main + +func main() {} +-- sub/sub.gno.gen.go.golden -- +// Code generated by github.com/gnolang/gno. DO NOT EDIT. + +//go:build gno + +package sub diff --git a/gnovm/cmd/gno/testdata/gno_precompile/06_build_flag.txtar b/gnovm/cmd/gno/testdata/gno_precompile/06_build_flag.txtar new file mode 100644 index 00000000000..b93fae95fcf --- /dev/null +++ b/gnovm/cmd/gno/testdata/gno_precompile/06_build_flag.txtar @@ -0,0 +1,36 @@ +# Run gno precompile with -gobuild flag + +gno precompile -gobuild . + +! stdout .+ +! stderr .+ + +cmp main.gno.gen.go main.gno.gen.go.golden +cmp sub/sub.gno.gen.go sub/sub.gno.gen.go.golden + +-- main.gno -- +package main + +func main() { + var x = 1 + _=x +} +-- sub/sub.gno -- +package sub +-- main.gno.gen.go.golden -- +// Code generated by github.com/gnolang/gno. DO NOT EDIT. + +//go:build gno + +package main + +func main() { + var x = 1 + _ = x +} +-- sub/sub.gno.gen.go.golden -- +// Code generated by github.com/gnolang/gno. DO NOT EDIT. + +//go:build gno + +package sub diff --git a/gnovm/cmd/gno/testdata/gno_precompile/07_build_flag_with_build_error.txtar b/gnovm/cmd/gno/testdata/gno_precompile/07_build_flag_with_build_error.txtar new file mode 100644 index 00000000000..6543623b266 --- /dev/null +++ b/gnovm/cmd/gno/testdata/gno_precompile/07_build_flag_with_build_error.txtar @@ -0,0 +1,34 @@ +# Run gno precompile with -gobuild flag + +! gno precompile -gobuild . + +! stdout .+ +stderr './main.gno.gen.go:8:6: x declared and not used' + +cmp main.gno.gen.go main.gno.gen.go.golden +cmp sub/sub.gno.gen.go sub/sub.gno.gen.go.golden + +-- main.gno -- +package main + +func main() { + var x = 1 +} +-- sub/sub.gno -- +package sub +-- main.gno.gen.go.golden -- +// Code generated by github.com/gnolang/gno. DO NOT EDIT. + +//go:build gno + +package main + +func main() { + var x = 1 +} +-- sub/sub.gno.gen.go.golden -- +// Code generated by github.com/gnolang/gno. DO NOT EDIT. + +//go:build gno + +package sub diff --git a/gnovm/pkg/gnolang/precompile.go b/gnovm/pkg/gnolang/precompile.go index f7a10d5589a..8f64c256560 100644 --- a/gnovm/pkg/gnolang/precompile.go +++ b/gnovm/pkg/gnolang/precompile.go @@ -14,9 +14,10 @@ import ( "sort" "strings" - "github.com/gnolang/gno/tm2/pkg/std" "go.uber.org/multierr" "golang.org/x/tools/go/ast/astutil" + + "github.com/gnolang/gno/tm2/pkg/std" ) const ( @@ -151,7 +152,7 @@ func Precompile(source string, tags string, filename string) (*precompileResult, var out bytes.Buffer fset := token.NewFileSet() - f, err := parser.ParseFile(fset, "tmp.gno", source, parser.ParseComments) + f, err := parser.ParseFile(fset, filename, source, parser.ParseComments) if err != nil { return nil, fmt.Errorf("parse: %w", err) }