Skip to content

Commit

Permalink
lib, lang, docs: Add --only-unify option
Browse files Browse the repository at this point in the history
This adds a new run flag for the lang frontend to exit immediately
following type unification. This makes it easier to use this as a step
in CI, and also to type the execution for performance comparison
reasons.
  • Loading branch information
purpleidea committed Jan 2, 2024
1 parent 32ca815 commit 24054f9
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
15 changes: 15 additions & 0 deletions docs/faq.md
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,21 @@ requires a number of seconds as an argument.
./mgmt run lang examples/lang/hello0.mcl --converged-timeout=5
```

### Can I run `mgmt` for type-checking only?

Yes, you can, add the `--only-unify` option to the lang frontend while using the
`run` command, and it will exit after type unification.

#### Example:

```
./mgmt run --tmp-prefix lang --only-unify examples/lang/hello0.mcl
```

It will also print how long it took on either success or failure. Keep in mind
that even if you pass type unification, an `mgmt` run can still fail later on
for other reasons, although these are mostly runtime considerations.

### Why does my file resource error with `no such file or directory`?

If you create a file resource and only specify the content like this:
Expand Down
20 changes: 18 additions & 2 deletions lang/gapi/gapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"fmt"
"strings"
"sync"
"time"

"github.com/purpleidea/mgmt/gapi"
"github.com/purpleidea/mgmt/lang"
Expand Down Expand Up @@ -97,6 +98,10 @@ func (obj *GAPI) CliFlags(command string) []cli.Flag {
Name: "update",
Usage: "update all dependencies to the latest versions",
},
&cli.BoolFlag{
Name: "only-unify",
Usage: "stop after type unification",
},
}
result = append(result, runFlags...)
}
Expand Down Expand Up @@ -318,14 +323,25 @@ func (obj *GAPI) Cli(cliInfo *gapi.CliInfo) (*gapi.Deploy, error) {
}
}
logf("running type unification...")
startTime := time.Now()
unifier := &unification.Unifier{
AST: iast,
Solver: unification.SimpleInvariantSolverLogger(unificationLogf),
Debug: debug,
Logf: unificationLogf,
}
if err := unifier.Unify(); err != nil {
return nil, errwrap.Wrapf(err, "could not unify types")
unifyErr := unifier.Unify()
delta := time.Since(startTime)
if unifyErr != nil {
if c.Bool("only-unify") {
logf("type unification failed after %s", delta)
}
return nil, errwrap.Wrapf(unifyErr, "could not unify types")
}

if c.Bool("only-unify") {
logf("type unification succeeded in %s", delta)
return nil, nil // we end early
}

// get the list of needed files (this is available after SetScope)
Expand Down
3 changes: 3 additions & 0 deletions lib/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ func run(c *cli.Context, name string, gapiObj gapi.GAPI) error {
if err != nil {
return errwrap.Wrapf(err, "cli parse error")
}
if c.Bool("only-unify") && deploy == nil {
return nil // we end early
}
obj.Deploy = deploy
if obj.Deploy == nil {
// nobody activated, but we'll still watch the etcd deploy chan,
Expand Down

0 comments on commit 24054f9

Please sign in to comment.