Skip to content

Commit d5648c7

Browse files
authored
Validate the CUE Value used in cog go type pipeline (#1297)
## What Changed? Why? When using the latest version of `cog` (v0.1.4) the `GoTypes` jenny can take an extremely long time to complete, causing CI to fail. This is likely due to two issues that are exacerbated by the newer cog versions: * CUE lazy-evaluates values, and calls to `Subsume()` and `Equals()` do not cache the evaluations * `imports.Process` has to load the dependency graph of all imports, and newer cog versions include a larger graph This PR aims to solve the first of these two issues, by calling `Validate()` on the `cue.Value` used in cog go type pipeline before running it to force CUE to traverse the graph and cache it, reducing downstream function call times by cog. As a side-effect of this, it also is now enforcing a valid cue.Value before passing it off to cog (though at this point in the pipeline is using the parser in `codegen/cuekind`, it should already be valid). We can't use `Eval()` instead of `Validate()` (as they both force a graph traversal and cache), because `Eval()` will resolve reference values, which can impact output typing from cog. This PR was spun off of #1295 to allow it to only do the dependency upgrades. ### How was it tested? Locally, using `make regenerate-codegen-test-files`, `make test`, and `make generate` in `examples/apiserver`, verifying that no changes have been made to the generated files, and no new errors emerge. The speed-up with the latest cog has been tested in #1295 ### Where did you document your changes? Code comments explain the reasoning behind the call to `Validate()`. ### Notes to Reviewers
1 parent dd334c5 commit d5648c7

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

codegen/jennies/gotypes.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ func (g *GoTypes) generateFilesAtDepth(v cue.Value, schemaPath cue.Path, currDep
199199
AnyAsInterface: g.AnyAsInterface,
200200
}, len(v.Path().Selectors())-(g.Depth-g.NamingDepth), namerFunc)
201201
if err != nil {
202-
return nil, err
202+
return nil, fmt.Errorf("error converting schema path %s.%s to go type: %w", schemaPath.String(), fieldName, err)
203203
}
204204

205205
return codejen.Files{codejen.File{
@@ -260,6 +260,13 @@ func GoTypesFromCUE(v cue.Value, cfg CUEGoConfig, maxNamingDepth int, namerFunc
260260
}
261261
return name
262262
}
263+
// Force CUE to evaluate the value tree without setting isData
264+
// This speeds up subsequent Subsume() and Equals() calls being done by cog, as otherwise this jenny's cost can become prohibitive.
265+
// TODO: this should probably be a fix in cog rather than here
266+
err := v.Validate(cue.All())
267+
if err != nil {
268+
return nil, fmt.Errorf("invalid CUE value at %s: %w", v.Path(), err)
269+
}
263270
codegenPipeline := cog.TypesFromSchema().
264271
CUEValue(cfg.PackageName, v, cog.ForceEnvelope(cfg.Name), cog.NameFunc(nameFunc)).
265272
SchemaTransformations(cog.PrefixObjectsNames(cfg.NamePrefix)).

0 commit comments

Comments
 (0)