diff --git a/cmd/crossplane/dependency/cache.go b/cmd/crossplane/dependency/cache.go index c31b09f3..3cee514b 100644 --- a/cmd/crossplane/dependency/cache.go +++ b/cmd/crossplane/dependency/cache.go @@ -34,6 +34,8 @@ import ( "github.com/crossplane/cli/v2/internal/git" "github.com/crossplane/cli/v2/internal/project/projectfile" "github.com/crossplane/cli/v2/internal/schemas/generator" + "github.com/crossplane/cli/v2/internal/schemas/manager" + "github.com/crossplane/cli/v2/internal/schemas/runner" "github.com/crossplane/cli/v2/internal/terminal" clixpkg "github.com/crossplane/cli/v2/internal/xpkg" @@ -76,15 +78,17 @@ func (c *updateCacheCmd) Run(logger logging.Logger, sp terminal.SpinnerPrinter, return errors.Wrap(err, "cannot create xpkg client") } + generators := generator.Filter( + generator.AllLanguages( + generator.WithGoModelAccessors(cfg.Features.GenerateGoModelAccessors), + generator.WithGoRuntimeObjects(cfg.Features.GenerateGoRuntimeObjects), + ), + proj.Spec.Schemas.GetLanguages(), + ) + opts := []dependency.ManagerOption{ dependency.WithProjectFile(c.ProjectFile), - dependency.WithSchemaGenerators(generator.Filter( - generator.AllLanguages( - generator.WithGoModelAccessors(cfg.Features.GenerateGoModelAccessors), - generator.WithGoRuntimeObjects(cfg.Features.GenerateGoRuntimeObjects), - ), - proj.Spec.Schemas.GetLanguages(), - )), + dependency.WithSchemaGenerators(generators), dependency.WithXpkgClient(client), } @@ -99,7 +103,30 @@ func (c *updateCacheCmd) Run(logger logging.Logger, sp terminal.SpinnerPrinter, logger.Debug("Updating all dependencies") return sp.WrapAsyncWithSuccessSpinners(func(ch async.EventChannel) error { - return m.RefreshAll(ctx, ch) + if err := m.RefreshAll(ctx, ch); err != nil { + return err + } + + // Also regenerate schemas for the project's own APIs, so the models + // are complete without a build (e.g. for language tooling in + // functions and tests that import them). + apisExist, err := afero.DirExists(projFS, proj.Spec.Paths.APIs) + if err != nil || !apisExist { + return err + } + + ch.SendEvent("Generating project API schemas", async.EventStatusStarted) + schemasFS := afero.NewBasePathFs(projFS, proj.Spec.Paths.Schemas) + schemaRunner := runner.NewRealSchemaRunner(runner.WithImageConfig(proj.Spec.ImageConfigs)) + schemaMgr := manager.New(schemasFS, generators, schemaRunner) + apisFS := afero.NewBasePathFs(projFS, proj.Spec.Paths.APIs) + if _, err := schemaMgr.Generate(ctx, manager.NewFSSource(proj.Spec.Paths.APIs, apisFS)); err != nil { + ch.SendEvent("Generating project API schemas", async.EventStatusFailure) + return errors.Wrap(err, "failed to generate project API schemas") + } + ch.SendEvent("Generating project API schemas", async.EventStatusSuccess) + + return nil }) } diff --git a/cmd/crossplane/function/generate.go b/cmd/crossplane/function/generate.go index cb7585a4..1460f753 100644 --- a/cmd/crossplane/function/generate.go +++ b/cmd/crossplane/function/generate.go @@ -292,6 +292,10 @@ func (c *generateCmd) generateKCLFiles(fs afero.Fs) error { Alias: alias, }) } + // Sort for deterministic output; imports is a map. + slices.SortFunc(importStatements, func(a, b kclImportStatement) int { + return strings.Compare(a.ImportPath, b.ImportPath) + }) tmplData := kclTemplateData{ ModName: c.Name, diff --git a/internal/project/functions/kcl.go b/internal/project/functions/kcl.go index a6e268b3..5227278f 100644 --- a/internal/project/functions/kcl.go +++ b/internal/project/functions/kcl.go @@ -224,7 +224,7 @@ func setImageEnvvars(image v1.Image, envVars map[string]string) (v1.Image, error func newKCLBuilder(imageConfigs []v1beta1.ImageConfig) *kclBuilder { return &kclBuilder{ - baseImage: "xpkg.crossplane.io/crossplane-contrib/function-kcl:v0.12.1", + baseImage: "xpkg.crossplane.io/crossplane-contrib/function-kcl:v0.12.3", transport: http.DefaultTransport, configStore: clixpkg.NewStaticImageConfigStore(imageConfigs), } diff --git a/internal/project/projectfile/projectfile.go b/internal/project/projectfile/projectfile.go index b79fc5a3..ffd6a7e5 100644 --- a/internal/project/projectfile/projectfile.go +++ b/internal/project/projectfile/projectfile.go @@ -112,5 +112,28 @@ func Update(projFS afero.Fs, projFile string, fn func(*v1alpha1.Project)) error if err != nil { return errors.Wrap(err, "failed to marshal project") } + bs = stripNullCreationTimestamp(bs) return afero.WriteFile(projFS, projFile, bs, 0o644) } + +// stripNullCreationTimestamp removes the explicit null creationTimestamp the +// zero ObjectMeta marshals with, so it doesn't accumulate in project files. +func stripNullCreationTimestamp(bs []byte) []byte { + var doc map[string]any + if err := yaml.Unmarshal(bs, &doc); err != nil { + return bs + } + md, ok := doc["metadata"].(map[string]any) + if !ok { + return bs + } + if ts, exists := md["creationTimestamp"]; !exists || ts != nil { + return bs + } + delete(md, "creationTimestamp") + clean, err := yaml.Marshal(doc) + if err != nil { + return bs + } + return clean +}