Skip to content

Commit

Permalink
Merge pull request #8169 from planetscale/systay-codegen-safe
Browse files Browse the repository at this point in the history
Make sure to only allow codegen when the rest of the code compiles
  • Loading branch information
harshit-gangal authored May 21, 2021
2 parents 1d2e0c9 + 2851c42 commit 7e7a571
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 11 deletions.
17 changes: 7 additions & 10 deletions go/tools/astfmtgen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,21 @@ import (
"go/printer"
"go/token"
"go/types"
"log"
"os"
"path"
"strconv"
"strings"

"vitess.io/vitess/go/tools/common"

"golang.org/x/tools/go/ast/astutil"
"golang.org/x/tools/go/packages"
)

func main() {
err := load(os.Args[1])
if err != nil {
panic(err)
}
}
packageName := os.Args[1]

func load(packageName string) error {
config := &packages.Config{
Mode: packages.NeedName |
packages.NeedFiles |
Expand All @@ -49,19 +47,18 @@ func load(packageName string) error {
packages.NeedTypesInfo,
}
pkgs, err := packages.Load(config, packageName)
if err != nil {
return fmt.Errorf("error loading package %s: %w", packageName, err)
if err != nil || common.PkgFailed(pkgs) {
log.Fatal("error loading packaged")
}
for _, pkg := range pkgs {
if pkg.Name == "sqlparser" {
rewriter := &Rewriter{pkg: pkg}
err := rewriter.Rewrite()
if err != nil {
return err
log.Fatal(err.Error())
}
}
}
return nil
}

type Rewriter struct {
Expand Down
5 changes: 4 additions & 1 deletion go/tools/asthelpergen/asthelpergen.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import (
"path"
"strings"

"vitess.io/vitess/go/tools/common"

"github.com/dave/jennifer/jen"
"golang.org/x/tools/go/packages"
)
Expand Down Expand Up @@ -201,7 +203,8 @@ func GenerateASTHelpers(packagePatterns []string, rootIface, exceptCloneType str
Mode: packages.NeedName | packages.NeedTypes | packages.NeedTypesSizes | packages.NeedTypesInfo | packages.NeedDeps | packages.NeedImports | packages.NeedModule,
}, packagePatterns...)

if err != nil {
if err != nil || common.PkgFailed(loaded) {
log.Fatal("error loading packaged")
return nil, err
}

Expand Down
16 changes: 16 additions & 0 deletions go/tools/asthelpergen/main/main.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
Copyright 2021 The Vitess Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package main

import (
Expand Down
35 changes: 35 additions & 0 deletions go/tools/common/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
Copyright 2021 The Vitess Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package common

import (
"log"

"golang.org/x/tools/go/packages"
)

// PkgFailed returns true if any of the packages contain errors
func PkgFailed(loaded []*packages.Package) bool {
failed := false
for _, pkg := range loaded {
for _, e := range pkg.Errors {
log.Println(e.Error())
failed = true
}
}
return failed
}
6 changes: 6 additions & 0 deletions go/tools/sizegen/sizegen.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ import (
"sort"
"strings"

"vitess.io/vitess/go/tools/common"

"github.com/dave/jennifer/jen"
"golang.org/x/tools/go/packages"
)
Expand Down Expand Up @@ -507,6 +509,10 @@ func GenerateSizeHelpers(packagePatterns []string, typePatterns []string) (map[s
return nil, err
}

if common.PkgFailed(loaded) {
return nil, fmt.Errorf("failed to load packages")
}

sizegen := newSizegen(loaded[0].Module, loaded[0].TypesSizes)

scopes := make(map[string]*types.Scope)
Expand Down

0 comments on commit 7e7a571

Please sign in to comment.