Skip to content

Commit

Permalink
Update workflowcheck and allow it to handle aliased context (#1642)
Browse files Browse the repository at this point in the history
Handle aliasing of internal packages in workflow
check
  • Loading branch information
Quinn-With-Two-Ns authored Sep 23, 2024
1 parent 8283604 commit d10e871
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 18 deletions.
2 changes: 1 addition & 1 deletion contrib/tools/workflowcheck/determinism/checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ import (
"reflect"
"regexp"
"runtime"
"slices"
"strings"
"sync"

"golang.org/x/exp/slices"
"golang.org/x/tools/go/analysis"
"golang.org/x/tools/go/types/typeutil"
)
Expand Down
8 changes: 4 additions & 4 deletions contrib/tools/workflowcheck/go.mod
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
module go.temporal.io/sdk/contrib/tools/workflowcheck

go 1.20
go 1.22.0

require (
golang.org/x/exp v0.0.0-20230905200255-921286631fa9
golang.org/x/tools v0.17.0
golang.org/x/tools v0.25.0
gopkg.in/yaml.v2 v2.4.0
)

require (
github.com/kr/pretty v0.1.0 // indirect
github.com/kr/text v0.2.0 // indirect
golang.org/x/mod v0.14.0 // indirect
golang.org/x/mod v0.21.0 // indirect
golang.org/x/sync v0.8.0 // indirect
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect
)
15 changes: 8 additions & 7 deletions contrib/tools/workflowcheck/go.sum
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 h1:GoHiUyI/Tp2nVkLI2mCxVkOjsbSXD66ic0XW0js0R9g=
golang.org/x/exp v0.0.0-20230905200255-921286631fa9/go.mod h1:S2oDrQGGwySpoQPVqRShND87VCbxmc6bL1Yd2oYrm6k=
golang.org/x/mod v0.14.0 h1:dGoOF9QVLYng8IHTm7BAyWqCqSheQ5pYWGhzW00YJr0=
golang.org/x/mod v0.14.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/sync v0.6.0 h1:5BMeUDZ7vkXGfEr1x9B4bRcTH4lpkTkpdh0T/J+qjbQ=
golang.org/x/tools v0.17.0 h1:FvmRgNOcs3kOa+T20R1uhfP9F6HgG2mfxDv1vrx1Htc=
golang.org/x/tools v0.17.0/go.mod h1:xsh6VxdV005rRVaS6SSAf9oiAqljS7UZUacMZ8Bnsps=
golang.org/x/mod v0.21.0 h1:vvrHzRwRfVKSiLrG+d4FMl/Qi4ukBCE6kZlTUkDYRT0=
golang.org/x/mod v0.21.0/go.mod h1:6SkKJ3Xj0I0BrPOZoBy3bdMptDDU9oJrpohJ3eWZ1fY=
golang.org/x/sync v0.8.0 h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ=
golang.org/x/sync v0.8.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/tools v0.25.0 h1:oFU9pkj/iJgs+0DT+VMHrx+oBKs/LJMV+Uvg78sl+fE=
golang.org/x/tools v0.25.0/go.mod h1:/vtpO8WL1N9cQC3FN5zPqb//fRXskFHbLKk4OW1Q7rg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
Expand Down
15 changes: 11 additions & 4 deletions contrib/tools/workflowcheck/workflow/checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ func (c *Checker) debugf(f string, v ...interface{}) {
func (c *Checker) NewAnalyzer() *analysis.Analyzer {
a := &analysis.Analyzer{
Name: "workflowcheck",
Doc: "Analyzes all RegisterWorkflow functions for non-determinism",
Doc: "Analyzes all Workflow functions for non-determinism",
Run: func(p *analysis.Pass) (interface{}, error) { return nil, c.Run(p) },
FactTypes: []analysis.Fact{&determinism.PackageNonDeterminisms{}, &determinism.NonDeterminisms{}},
}
Expand Down Expand Up @@ -197,17 +197,24 @@ func (c *Checker) Run(pass *analysis.Pass) error {
}

// isWorkflowFunc checks if f has workflow.Context as a first parameter.
func isWorkflowFunc(f *ast.FuncDecl, pass *analysis.Pass) bool {
func isWorkflowFunc(f *ast.FuncDecl, pass *analysis.Pass) (b bool) {
if f.Type.Params == nil || len(f.Type.Params.List) == 0 {
return false
}
firstParam := f.Type.Params.List[0]
typeInfo := pass.TypesInfo.TypeOf(firstParam.Type)
named, _ := typeInfo.(*types.Named)
if named == nil {
alias, _ := typeInfo.(*types.Alias)
if named == nil && alias == nil {
return false
}
obj := named.Obj()
var obj *types.TypeName
if named != nil {
obj = named.Obj()
}
if alias != nil {
obj = alias.Obj()
}
if obj.Pkg() == nil || obj.Name() != "Context" {
return false
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package internal

type Context interface{}
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package workflow

import "time"
import (
"time"

"go.temporal.io/sdk/internal"
)

type RegisterOptions struct{}

type Context interface{}
type Context = internal.Context

func AwaitWithTimeout(ctx Context, timeout time.Duration, condition func() bool) (ok bool, err error) {
// Intentionally simulate non-deterministic call internally
Expand Down

0 comments on commit d10e871

Please sign in to comment.