From 121e384fabdf01ca191c5a4da3df82094dd2fce7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Agn=C3=A8s=20Toulet?= <35176601+AgnesToulet@users.noreply.github.com> Date: Thu, 2 Feb 2023 21:06:55 +0100 Subject: [PATCH] kindsys: Fix CUE code generation on Windows (#62812) * fix Windows paths * fix remaining paths issues * clean up multiple use of Dir() * fix headers in generated files --- pkg/codegen/generators.go | 2 +- pkg/cuectx/ctx.go | 4 ++-- pkg/plugins/pfs/decl_parser.go | 2 +- pkg/plugins/pfs/pfs.go | 2 +- pkg/plugins/plugindef/gen.go | 5 ++--- public/app/plugins/gen.go | 3 +-- 6 files changed, 8 insertions(+), 10 deletions(-) diff --git a/pkg/codegen/generators.go b/pkg/codegen/generators.go index 8f1771b4c17..5bbb3a03b57 100644 --- a/pkg/codegen/generators.go +++ b/pkg/codegen/generators.go @@ -47,7 +47,7 @@ func SlashHeaderMapper(maingen string) codejen.FileMapper { buf := new(bytes.Buffer) if err := tmpls.Lookup("gen_header.tmpl").Execute(buf, tvars_gen_header{ - MainGenerator: maingen, + MainGenerator: filepath.ToSlash(maingen), Using: f.From, Leader: leader, }); err != nil { diff --git a/pkg/cuectx/ctx.go b/pkg/cuectx/ctx.go index db61d5b7438..2d07a2c5d04 100644 --- a/pkg/cuectx/ctx.go +++ b/pkg/cuectx/ctx.go @@ -116,7 +116,7 @@ func prefixFS(prefix string, fsys fs.FS) (fs.FS, error) { return nil } - b, err := fs.ReadFile(fsys, path) + b, err := fs.ReadFile(fsys, filepath.ToSlash(path)) if err != nil { return err } @@ -217,7 +217,7 @@ func BuildGrafanaInstance(ctx *cue.Context, relpath string, pkg string, overlay // // NOTE This function will be deprecated in favor of a more generic loader func LoadInstanceWithGrafana(fsys fs.FS, dir string, opts ...load.Option) (*build.Instance, error) { - if modf, err := fs.ReadFile(fsys, filepath.Join("cue.mod", "module.cue")); err != nil { + if modf, err := fs.ReadFile(fsys, "cue.mod/module.cue"); err != nil { // delegate error handling return load.InstanceWithThema(fsys, dir, opts...) } else if modname, err := cuecontext.New().CompileBytes(modf).LookupPath(cue.MakePath(cue.Str("module"))).String(); err != nil { diff --git a/pkg/plugins/pfs/decl_parser.go b/pkg/plugins/pfs/decl_parser.go index 299bc9a7ed0..fa9f72ac1eb 100644 --- a/pkg/plugins/pfs/decl_parser.go +++ b/pkg/plugins/pfs/decl_parser.go @@ -33,7 +33,7 @@ func (psr *declParser) Parse(root fs.FS) ([]*PluginDecl, error) { decls := make([]*PluginDecl, 0) for _, plugin := range plugins { - path := filepath.Dir(plugin) + path := filepath.ToSlash(filepath.Dir(plugin)) base := filepath.Base(path) if skip, ok := psr.skip[base]; ok && skip { continue diff --git a/pkg/plugins/pfs/pfs.go b/pkg/plugins/pfs/pfs.go index c6c5d4b8ccc..f6b4116e249 100644 --- a/pkg/plugins/pfs/pfs.go +++ b/pkg/plugins/pfs/pfs.go @@ -235,7 +235,7 @@ func ParsePluginFS(fsys fs.FS, rt *thema.Runtime) (ParsedPlugin, error) { } func ensureCueMod(fsys fs.FS, pdef plugindef.PluginDef) (fs.FS, error) { - if modf, err := fs.ReadFile(fsys, filepath.Join("cue.mod", "module.cue")); err != nil { + if modf, err := fs.ReadFile(fsys, "cue.mod/module.cue"); err != nil { if !errors.Is(err, fs.ErrNotExist) { return nil, err } diff --git a/pkg/plugins/plugindef/gen.go b/pkg/plugins/plugindef/gen.go index c72795da2e9..710a6734fa8 100644 --- a/pkg/plugins/plugindef/gen.go +++ b/pkg/plugins/plugindef/gen.go @@ -10,7 +10,6 @@ import ( "fmt" "os" "path/filepath" - "strings" "cuelang.org/go/cue/cuecontext" "github.com/dave/dst" @@ -40,8 +39,8 @@ func main() { fmt.Fprintf(os.Stderr, "could not get working directory: %s", err) os.Exit(1) } - grootp := strings.Split(cwd, string(os.PathSeparator)) - groot := filepath.Join(string(os.PathSeparator), filepath.Join(grootp[:len(grootp)-3]...)) + + groot := filepath.Clean(filepath.Join(cwd, "../../..")) jfs := elsedie(jl.GenerateFS(lin))("plugindef jenny pipeline failed") if _, set := os.LookupEnv("CODEGEN_VERIFY"); set { diff --git a/public/app/plugins/gen.go b/public/app/plugins/gen.go index 83a67330f29..c3bdaefc4b8 100644 --- a/public/app/plugins/gen.go +++ b/public/app/plugins/gen.go @@ -43,8 +43,7 @@ func main() { if err != nil { log.Fatal(fmt.Errorf("could not get working directory: %s", err)) } - grootp := strings.Split(cwd, sep) - groot := filepath.Join(sep, filepath.Join(grootp[:len(grootp)-3]...)) + groot := filepath.Clean(filepath.Join(cwd, "../../..")) rt := cuectx.GrafanaThemaRuntime() pluginKindGen := codejen.JennyListWithNamer(func(d *pfs.PluginDecl) string {