Skip to content

Commit 842db8a

Browse files
committed
vgo: Loader initial commit
1 parent 9879d50 commit 842db8a

File tree

10 files changed

+280
-30
lines changed

10 files changed

+280
-30
lines changed

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,19 @@
11
### `myitcv.io/...` mono-repo
22

3+
<!-- __TEMPLATE: go list -f "{{${DOLLAR}ip := .ImportPath}}{{range .Deps}}{{if (eq \"myitcv.io/vgo\" .)}}{{${DOLLAR}ip}}{{end}}{{end}}" ./...
4+
{{ with . }}
5+
Please note the following packages current rely on `vgo` with https://go-review.googlesource.com/c/vgo/+/105855 applied:
6+
7+
```
8+
{{. -}}
9+
```
10+
{{end -}}
11+
-->
12+
13+
Please note the following packages current rely on `vgo` with https://go-review.googlesource.com/c/vgo/+/105855 applied:
14+
15+
```
16+
myitcv.io/immutable/cmd/immutableVet
17+
```
18+
<!-- END -->
19+

_scripts/run_tests.sh

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,11 @@ if [ "${CI:-}" == "true" ]
1010
then
1111
go get -u golang.org/x/vgo
1212
pushd $(go list -f "{{.Dir}}" golang.org/x/vgo) > /dev/null
13-
git checkout -qf $VGO_COMMIT
13+
14+
# git checkout -qf $VGO_COMMIT
15+
git fetch -q https://go.googlesource.com/vgo refs/changes/55/105855/3 && git checkout -qf FETCH_HEAD
1416
go install
17+
1518
popd > /dev/null
1619

1720
# so we can access Github without hitting rate limits
@@ -48,7 +51,6 @@ fi
4851

4952
export PATH=$GOPATH/bin:$PATH
5053

51-
5254
# get all packages that do not belong to a module that has its
5355
# own _scripts/run_tests.sh file
5456
for i in $(find -mindepth 2 -iname go.mod -exec dirname '{}' \;)
@@ -59,17 +61,33 @@ do
5961
then
6062
./_scripts/run_tests.sh
6163
else
64+
if [ -f ./_scripts/pre_run_tests.sh ]
65+
then
66+
./_scripts/pre_run_tests.sh
67+
fi
68+
6269
$go generate ./...
6370
$go test ./...
6471

65-
# we can remove this once we resolve https://github.com/golang/go/issues/24661
66-
$go install ./...
72+
if [ -f ./_scripts/post_run_tests.sh ]
73+
then
74+
./_scripts/post_run_tests.sh
75+
fi
6776
fi
6877
popd > /dev/null
6978
echo "----"
7079
echo ""
7180
done
7281

82+
# we use regular go to list here because of https://github.com/golang/go/issues/24749;
83+
# this is also the reason why we need to change to the directory to do the vgo install
84+
for i in $(go list -f "{{if eq .Name \"main\"}}{{.Dir}}{{end}}" ./...)
85+
do
86+
pushd $i > /dev/null
87+
$go install
88+
popd > /dev/null
89+
done
90+
7391
echo Checking markdown files are current
7492
# by this point we will have mdreplace installed. Hence check that
7593
# committed .md files are "fresh"

cmd/mdreplace/README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ _To see this in action, look at the [source of the
4444

4545
<!-- __TEMPLATE: mdreplace -h
4646
```
47-
{{.}}
47+
{{. -}}
4848
```
4949
-->
5050
```
@@ -56,11 +56,12 @@ Usage:
5656
When called with no file arguments, mdreplace works with stdin
5757
5858
Flags:
59+
-debug
60+
whether to print debug information of not
5961
-h show usage information
6062
-strip
6163
whether to strip special comments from the file
6264
-w whether to write back to input files (cannot be used when reading from stdin)
63-
6465
```
6566
<!-- END -->
6667

cmd/mdreplace/mdreplace.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ var (
4949
fHelp = flag.Bool("h", false, "show usage information")
5050
fWrite = flag.Bool("w", false, "whether to write back to input files (cannot be used when reading from stdin)")
5151
fStrip = flag.Bool("strip", false, "whether to strip special comments from the file")
52+
fDebug = flag.Bool("debug", false, "whether to print debug information of not")
5253

5354
usage string // gets populated at runtime
5455
)
@@ -142,7 +143,7 @@ func fatalf(format string, args ...interface{}) {
142143
}
143144

144145
func debugf(format string, args ...interface{}) {
145-
if debug {
146+
if debug || *fDebug {
146147
fmt.Fprintf(os.Stderr, format, args...)
147148
}
148149
}

immutable/_scripts/run_tests.sh

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,14 @@ rm -f !(_vendor)/**/gen_*.go
1010
$go install myitcv.io/immutable/cmd/immutableGen
1111
$go install myitcv.io/immutable/cmd/immutableVet
1212

13+
# this step is needed because _testFiles is not walked by ./...
1314
pushd cmd/immutableVet/_testFiles
1415
$go generate
1516
popd
1617

1718
$go generate ./...
18-
19-
# for immutable this step needs to be before the tests (because some of the
20-
# tests rely on the myitcv.io/immutable package to have been installed)
21-
22-
# we can remove this once we resolve https://github.com/golang/go/issues/24661
23-
$go install ./...
24-
2519
$go test ./...
2620

21+
$go install myitcv.io/immutable/cmd/immutableVet
2722

2823
immutableVet myitcv.io/immutable/example
29-

immutable/cmd/immutableVet/immutableVet.go

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"flag"
66
"fmt"
77
"go/ast"
8-
"go/importer"
98
"go/parser"
109
"go/printer"
1110
"go/token"
@@ -22,13 +21,16 @@ import (
2221
"myitcv.io/gogenerate"
2322
"myitcv.io/immutable"
2423
"myitcv.io/immutable/util"
24+
"myitcv.io/vgo"
2525
)
2626

2727
const (
2828
skipFileComment = "//" + immutable.CmdImmutableVet + ":skipFile"
2929
)
3030

31-
var fset = token.NewFileSet()
31+
var (
32+
fset = token.NewFileSet()
33+
)
3234

3335
type immutableVetter struct {
3436
pkgs map[string]*ast.Package
@@ -51,6 +53,9 @@ type immutableVetter struct {
5153
vcls map[*ast.CompositeLit]bool
5254

5355
errlist []immErr
56+
57+
importer types.ImporterFrom
58+
immIntf *types.Interface
5459
}
5560

5661
var typesCache = map[string]bool{
@@ -64,8 +69,6 @@ type immErr struct {
6469

6570
type errors []immErr
6671

67-
var immIntf *types.Interface
68-
6972
func main() {
7073
flag.Parse()
7174

@@ -87,9 +90,11 @@ func main() {
8790
}
8891
}
8992

90-
func loadImmIntf() {
93+
func loadImmIntf(dir string) (types.ImporterFrom, *types.Interface) {
9194
ip := "myitcv.io/immutable"
9295

96+
importer := vgo.NewTestLoader(dir)
97+
9398
bpkg, err := build.Import(ip, "", 0)
9499
if err != nil {
95100
fatalf("failed to import %v: %v", ip, err)
@@ -115,7 +120,7 @@ func loadImmIntf() {
115120

116121
conf := types.Config{
117122
FakeImportC: true,
118-
Importer: importer.For("gc", nil),
123+
Importer: importer,
119124
}
120125

121126
tpkg, err := conf.Check(ip, fset, files, nil)
@@ -144,14 +149,12 @@ func loadImmIntf() {
144149
fatalf("Underlying type is not a *types.Interface: %T", nmd.Underlying())
145150
}
146151

147-
immIntf = intf
152+
return importer, intf
148153
}
149154

150155
func vet(wd string, specs []string) []immErr {
151156
var emsgs []immErr
152157

153-
loadImmIntf()
154-
155158
// vetting phase: vet all packages packages passed in through the command line
156159
for _, spec := range specs {
157160

@@ -420,11 +423,15 @@ func newImmutableVetter(ipkg *build.Package, wd string) *immutableVetter {
420423
fatalf("could not parse package directory for %v", ipkg.Name)
421424
}
422425

426+
importer, immIntf := loadImmIntf(ipkg.Dir)
427+
423428
return &immutableVetter{
424429
pkgs: pkgs,
425430
bpkg: ipkg,
426431
vcls: make(map[*ast.CompositeLit]bool),
427432
wd: wd,
433+
immIntf: immIntf,
434+
importer: importer,
428435
skipFiles: make(map[string]bool),
429436
}
430437
}
@@ -451,7 +458,7 @@ func (iv *immutableVetter) vetPackages() []immErr {
451458

452459
// check types for the core package
453460
conf := types.Config{
454-
Importer: importer.Default(),
461+
Importer: iv.importer,
455462
}
456463
info := &types.Info{
457464
Selections: make(map[*ast.SelectorExpr]*types.Selection),
@@ -497,7 +504,7 @@ func (iv *immutableVetter) vetPackages() []immErr {
497504

498505
for i := 0; i < st.NumFields(); i++ {
499506
f := st.Field(i)
500-
if !isImmType(f.Type()) {
507+
if !iv.isImmType(f.Type()) {
501508
msg := fmt.Sprintf("immutable struct field must be immutable type; %v is not", f.Type())
502509
iv.errorf(f.Pos(), msg)
503510
}
@@ -575,7 +582,7 @@ func (iv *immutableVetter) vetPackages() []immErr {
575582
return iv.errlist
576583
}
577584

578-
func isImmType(t types.Type) bool {
585+
func (iv *immutableVetter) isImmType(t types.Type) bool {
579586
if v, ok := typesCache[t.String()]; ok {
580587
return v
581588
}
@@ -585,7 +592,7 @@ func isImmType(t types.Type) bool {
585592

586593
typesCache[t.String()] = true
587594

588-
v := isImmType(t.Underlying())
595+
v := iv.isImmType(t.Underlying())
589596
typesCache[t.String()] = v
590597

591598
return v
@@ -598,14 +605,14 @@ func isImmType(t types.Type) bool {
598605
case *types.Struct:
599606
for i := 0; i < t.NumFields(); i++ {
600607
f := t.Field(i)
601-
if !isImmType(f.Type()) {
608+
if !iv.isImmType(f.Type()) {
602609
return false
603610
}
604611
}
605612

606613
return true
607614
case *types.Interface:
608-
return types.Implements(t, immIntf)
615+
return types.Implements(t, iv.immIntf)
609616
case *types.Signature:
610617
return false
611618
default:

immutable/go.mod

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,9 @@ require (
44
"github.com/kisielk/gotool" v1.0.0
55
"golang.org/x/tools" v0.0.0-20180401165715-ac136b6c2db7
66
"myitcv.io/gogenerate" v0.0.0-20180405194116-cdcde380fd71d44b727692b77c152bb2e9df079c
7+
"myitcv.io/vgo" v0.0.0
8+
)
9+
10+
replace (
11+
"myitcv.io/vgo" v0.0.0 => "../vgo"
712
)

vgo/go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module "myitcv.io/vgo"
2+
3+
require "golang.org/x/net" v0.0.0-20180406214816-61147c48b25b

0 commit comments

Comments
 (0)