Skip to content

Commit

Permalink
Fix patching from dir with gomod generator
Browse files Browse the repository at this point in the history
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 <cpuguy83@gmail.com>
  • Loading branch information
cpuguy83 committed Jul 18, 2024
1 parent 20db3e7 commit 68d984b
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 15 deletions.
8 changes: 7 additions & 1 deletion source.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...),
Expand Down
54 changes: 40 additions & 14 deletions test/source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
})
})
})
}

0 comments on commit 68d984b

Please sign in to comment.