From 68d984b1acf974394436f8a38dbf5977b969dca8 Mon Sep 17 00:00:00 2001 From: Brian Goff Date: Thu, 18 Jul 2024 10:52:36 -0700 Subject: [PATCH] Fix patching from dir with gomod generator This is just a difference in how the states are stored when there is a subpath in a directory that is the patch source (as compared to a patch file). Signed-off-by: Brian Goff --- source.go | 8 ++++++- test/source_test.go | 54 +++++++++++++++++++++++++++++++++------------ 2 files changed, 47 insertions(+), 15 deletions(-) diff --git a/source.go b/source.go index ed928989..2e546add 100644 --- a/source.go +++ b/source.go @@ -527,8 +527,14 @@ func patchSource(worker, sourceState llb.State, sourceToState map[string]llb.Sta patchState := sourceToState[p.Source] // on each iteration, mount source state to /src to run `patch`, and // set the state under /src to be the source state for the next iteration + + subPath := p.Source + if p.Path != "" { + subPath = p.Path + } + sourceState = worker.Run( - llb.AddMount("/patch", patchState, llb.Readonly, llb.SourcePath(filepath.Join(p.Source, p.Path))), + llb.AddMount("/patch", patchState, llb.Readonly, llb.SourcePath(subPath)), llb.Dir("src"), ShArgs(fmt.Sprintf("patch -p%d < /patch", *p.Strip)), WithConstraints(opts...), diff --git a/test/source_test.go b/test/source_test.go index 155aa2ff..f9ccb79f 100644 --- a/test/source_test.go +++ b/test/source_test.go @@ -260,6 +260,7 @@ index ea874f5..ba38f84 100644 // Note: module here should be moduyle+version because this is checking the go module path on disk checkModule := func(ctx context.Context, gwc gwclient.Client, module string, spec *dalec.Spec) { + t.Helper() res, err := gwc.Solve(ctx, newSolveRequest(withBuildTarget("debug/gomods"), withSpec(ctx, t, spec))) if err != nil { t.Fatal(err) @@ -314,24 +315,49 @@ index ea874f5..ba38f84 100644 }) t.Run("with patch", func(t *testing.T) { - t.Parallel() - testEnv.RunTest(baseCtx, t, func(ctx context.Context, gwc gwclient.Client) { - spec := baseSpec() + t.Run("file", func(t *testing.T) { + t.Parallel() + testEnv.RunTest(baseCtx, t, func(ctx context.Context, gwc gwclient.Client) { + spec := baseSpec() - patchName := "patch" - spec.Sources[patchName] = dalec.Source{ - Inline: &dalec.SourceInline{ - File: &dalec.SourceInlineFile{ - Contents: downgradePatch, + patchName := "patch" + spec.Sources[patchName] = dalec.Source{ + Inline: &dalec.SourceInline{ + File: &dalec.SourceInlineFile{ + Contents: downgradePatch, + }, }, - }, - } + } - spec.Patches = map[string][]dalec.PatchSpec{ - srcName: {{Source: patchName}}, - } + spec.Patches = map[string][]dalec.PatchSpec{ + srcName: {{Source: patchName}}, + } - checkModule(ctx, gwc, "github.com/cpuguy83/tar2go@v0.3.0", spec) + checkModule(ctx, gwc, "github.com/cpuguy83/tar2go@v0.3.0", spec) + }) + }) + t.Run("dir", func(t *testing.T) { + t.Parallel() + testEnv.RunTest(baseCtx, t, func(ctx context.Context, gwc gwclient.Client) { + spec := baseSpec() + + patchName := "patch" + spec.Sources[patchName] = dalec.Source{ + Inline: &dalec.SourceInline{ + Dir: &dalec.SourceInlineDir{ + Files: map[string]*dalec.SourceInlineFile{ + "patch-file": {Contents: downgradePatch}, + }, + }, + }, + } + + spec.Patches = map[string][]dalec.PatchSpec{ + srcName: {{Source: patchName, Path: "patch-file"}}, + } + + checkModule(ctx, gwc, "github.com/cpuguy83/tar2go@v0.3.0", spec) + }) }) }) }