@@ -24,7 +24,9 @@ import (
24
24
"os"
25
25
"os/exec"
26
26
"path/filepath"
27
+ "strings"
27
28
29
+ apkofs "chainguard.dev/apko/pkg/apk/fs"
28
30
"chainguard.dev/melange/pkg/build"
29
31
"chainguard.dev/melange/pkg/config"
30
32
"github.com/chainguard-dev/clog"
@@ -102,6 +104,12 @@ func extractMelangeYamlFromTarball(apkPath, destDir string) error {
102
104
func FetchSourceFromMelange (ctx context.Context , filePath , destDir string ) (* config.Configuration , error ) {
103
105
log := clog .FromContext (ctx )
104
106
107
+ // Make sure destDir is an absolute path
108
+ destDir , err := filepath .Abs (destDir )
109
+ if err != nil {
110
+ return nil , fmt .Errorf ("failed to get absolute path for destination directory: %w" , err )
111
+ }
112
+
105
113
// Temporary directory for all ephemeral stuff. Best to keep this separate
106
114
// as we'll be extracting our pipelines code there, and we wouldn't want
107
115
// anyone to cause harm by overwriting the pipelines code.
@@ -176,6 +184,24 @@ func FetchSourceFromMelange(ctx context.Context, filePath, destDir string) (*con
176
184
}
177
185
defer os .Chdir (wd ) //nolint:errcheck
178
186
187
+ // Also, if we're handling a melange yaml file, check if next to it there is a
188
+ // directory called the same name as the melange yaml file, and if so, copy its
189
+ // contents to the destination directory. This is needed for patch and others.
190
+ if ! isApk {
191
+ pkgd := strings .TrimSuffix (filePath , filepath .Ext (filePath ))
192
+
193
+ if _ , err := os .Stat (pkgd ); err == nil {
194
+ log .Infof ("Found melange directory: %s\n Copying contents to %s\n " , pkgd , destDir )
195
+ srcFS := apkofs .DirFS (ctx , pkgd )
196
+ err := os .CopyFS (destDir , srcFS )
197
+ if err != nil {
198
+ return nil , fmt .Errorf ("failed to copy melange directory contents: %v" , err )
199
+ }
200
+ } else if ! os .IsNotExist (err ) {
201
+ return nil , fmt .Errorf ("error checking melange directory: %v" , err )
202
+ }
203
+ }
204
+
179
205
// Iterate over the pipeline steps and look for any source fetching steps.
180
206
for _ , step := range cfg .Pipeline {
181
207
if step .Uses == "patch" && isApk {
0 commit comments