From 2b7c953c6995ed36e284a32fe2ddcf790f8ab6fc Mon Sep 17 00:00:00 2001 From: Hugues Alary Date: Wed, 2 Oct 2024 17:46:05 -0700 Subject: [PATCH] Also fix relative path normalization When the bundle file is passed via stdin (i.e. `-f -`), a `file://` with a relative path would be normalized to the tmpfolder where stdin was saved. This fixes it. --- cmd/timoni/bundle_apply.go | 9 +++++++- cmd/timoni/bundle_build.go | 9 +++++++- cmd/timoni/bundle_vet.go | 9 +++++++- internal/engine/bundle_builder.go | 29 +++++++++++--------------- internal/engine/bundle_builder_test.go | 2 +- 5 files changed, 37 insertions(+), 21 deletions(-) diff --git a/cmd/timoni/bundle_apply.go b/cmd/timoni/bundle_apply.go index 889b3e68..9d5326ec 100644 --- a/cmd/timoni/bundle_apply.go +++ b/cmd/timoni/bundle_apply.go @@ -125,8 +125,15 @@ func runBundleApplyCmd(cmd *cobra.Command, _ []string) error { ctx, cancel := context.WithTimeout(cmd.Context(), rootArgs.timeout) defer cancel() + // Figure out the current working directory + // Required by the BundleBuilder for resolving relative paths + cwd, err := os.Getwd() + if err != nil { + return err + } + cuectx := cuecontext.New() - bm := engine.NewBundleBuilder(cuectx, files) + bm := engine.NewBundleBuilder(cuectx, files, cwd) runtimeValues := make(map[string]string) diff --git a/cmd/timoni/bundle_build.go b/cmd/timoni/bundle_build.go index bec63d55..6a1bdcb8 100644 --- a/cmd/timoni/bundle_build.go +++ b/cmd/timoni/bundle_build.go @@ -97,8 +97,15 @@ func runBundleBuildCmd(cmd *cobra.Command, _ []string) error { } defer os.RemoveAll(tmpDir) + // Figure out the current working directory + // Required by the BundleBuilder for resolving relative paths + cwd, err := os.Getwd() + if err != nil { + return err + } + ctx := cuecontext.New() - bm := engine.NewBundleBuilder(ctx, files) + bm := engine.NewBundleBuilder(ctx, files, cwd) runtimeValues := make(map[string]string) diff --git a/cmd/timoni/bundle_vet.go b/cmd/timoni/bundle_vet.go index 9b6db109..64df231b 100644 --- a/cmd/timoni/bundle_vet.go +++ b/cmd/timoni/bundle_vet.go @@ -104,8 +104,15 @@ func runBundleVetCmd(cmd *cobra.Command, args []string) error { } defer os.RemoveAll(tmpDir) + // Figure out the current working directory + // Required by the BundleBuilder for resolving relative paths + cwd, err := os.Getwd() + if err != nil { + return err + } + cuectx := cuecontext.New() - bm := engine.NewBundleBuilder(cuectx, files) + bm := engine.NewBundleBuilder(cuectx, files, cwd) runtimeValues := make(map[string]string) diff --git a/internal/engine/bundle_builder.go b/internal/engine/bundle_builder.go index 95f0e1bc..df62e49e 100644 --- a/internal/engine/bundle_builder.go +++ b/internal/engine/bundle_builder.go @@ -35,11 +35,11 @@ import ( // BundleBuilder compiles CUE definitions to Go Bundle objects. type BundleBuilder struct { - ctx *cue.Context - files []string - workspaceFiles map[string][]string - mapSourceToOrigin map[string]string - injector *RuntimeInjector + ctx *cue.Context + cwd string + files []string + workspaceFiles map[string][]string + injector *RuntimeInjector } type Bundle struct { @@ -57,16 +57,16 @@ type BundleInstance struct { } // NewBundleBuilder creates a BundleBuilder for the given module and package. -func NewBundleBuilder(ctx *cue.Context, files []string) *BundleBuilder { +func NewBundleBuilder(ctx *cue.Context, files []string, cwd string) *BundleBuilder { if ctx == nil { ctx = cuecontext.New() } b := &BundleBuilder{ - ctx: ctx, - files: files, - workspaceFiles: make(map[string][]string), - mapSourceToOrigin: make(map[string]string, len(files)), - injector: NewRuntimeInjector(ctx), + ctx: ctx, + cwd: cwd, + files: files, + workspaceFiles: make(map[string][]string), + injector: NewRuntimeInjector(ctx), } return b } @@ -113,7 +113,6 @@ func (b *BundleBuilder) InitWorkspace(workspace string, runtimeValues map[string if err := os.WriteFile(dstFile, data, os.ModePerm); err != nil { return fmt.Errorf("failed to write %s: %w", fn, err) } - b.mapSourceToOrigin[dstFile] = file files = append(files, dstFile) } @@ -162,11 +161,7 @@ func (b *BundleBuilder) Build(workspace string) (cue.Value, error) { func (b *BundleBuilder) getInstanceUrl(v cue.Value) string { url, _ := v.String() if path := strings.TrimPrefix(url, apiv1.LocalPrefix); IsFileUrl(url) && !filepath.IsAbs(path) { - source := v.Pos().Filename() - if origin, ok := b.mapSourceToOrigin[source]; ok { - source = origin - } - url = apiv1.LocalPrefix + filepath.Clean(filepath.Join(filepath.Dir(source), path)) + url = apiv1.LocalPrefix + filepath.Clean(filepath.Join(b.cwd, path)) } return url } diff --git a/internal/engine/bundle_builder_test.go b/internal/engine/bundle_builder_test.go index bb76049c..f3e37653 100644 --- a/internal/engine/bundle_builder_test.go +++ b/internal/engine/bundle_builder_test.go @@ -55,7 +55,7 @@ bundle: { } ` v := ctx.CompileString(bundle) - builder := NewBundleBuilder(ctx, []string{}) + builder := NewBundleBuilder(ctx, []string{}, "") b, err := builder.GetBundle(v) g.Expect(err).ToNot(HaveOccurred()) g.Expect(b.Instances).To(HaveLen(2))