From 5d7a418a8a6922b5c2a57c99553eaccd3a663732 Mon Sep 17 00:00:00 2001 From: Andrea Tartaglia Date: Mon, 28 Jul 2025 08:38:17 +0200 Subject: [PATCH] Correctly handle dependency copy Dependencies were not being copied correctly when `--untar` was set because when the archive is uncompressed it creates a directory structure which was skipped by the copy code. This updates it so it correctly handles the directories (when the untar option is set). Additionally I found an issue when testing which prevented the archive from being expanded because the directory was being removed before it could be processed. This has been fixed by changing the `os.RemoveAll` to `os.Remove` in the untar handling code. The tmp directory `tmpPath` is already marked for deletion when it's created Signed-off-by: Andrea Tartaglia --- pkg/downloader/manager.go | 40 ++++++++++++++++++++++++++++++--------- 1 file changed, 31 insertions(+), 9 deletions(-) diff --git a/pkg/downloader/manager.go b/pkg/downloader/manager.go index 37fed0efe..1b8597bac 100644 --- a/pkg/downloader/manager.go +++ b/pkg/downloader/manager.go @@ -353,7 +353,7 @@ func (m *Manager) downloadAll(deps []*chart.Dependency) error { getter.WithTagName(version)) } - _, v, err := dl.DownloadTo(churl, version, tmpPath) + d, v, err := dl.DownloadTo(churl, version, tmpPath) if err != nil { saveErrors = append(saveErrors, fmt.Errorf("could not download %s: %w", churl, err)) break @@ -368,16 +368,11 @@ func (m *Manager) downloadAll(deps []*chart.Dependency) error { } if m.Untar { - tmpDest, err := os.MkdirTemp("", "helm-dependency-") - if err != nil { - saveErrors = append(saveErrors, fmt.Errorf("failed to create temp dir: %w", err)) - break - } - if err := chartutil.ExpandFile(tmpDest, tmpPath); err != nil { + if err := chartutil.ExpandFile(tmpPath, d); err != nil { saveErrors = append(saveErrors, fmt.Errorf("failed to expand chart %s: %w", tmpPath, err)) break } - defer os.RemoveAll(tmpDest) + defer os.Remove(d) } churls[churl] = struct{}{} @@ -438,9 +433,36 @@ func (m *Manager) safeMoveDeps(deps []*chart.Dependency, source, dest string) er } for _, file := range sourceFiles { - if file.IsDir() { + + if file.IsDir() && m.Untar { + dirName := file.Name() + sourcedir := filepath.Join(source, dirName) + destdir := filepath.Join(dest, dirName) + + // Ensure destination directory exists + if err := os.MkdirAll(destdir, 0755); err != nil { + fmt.Fprintf(m.Out, "Could not create directory %s: %s (Skipping)\n", destdir, err) + continue + } + + // Load the chart to verify it's valid + _, err := loader.Load(sourcedir) + if err != nil { + fmt.Fprintf(m.Out, "Could not verify %s for moving: %s (Skipping)\n", sourcedir, err) + continue + } + + // Mark this chart as existing in source + existsInSourceDirectory[dirName] = true + + // Copy directory contents to destination + if err := fs.CopyDir(sourcedir, destdir); err != nil { + fmt.Fprintf(m.Out, "Unable to copy %s to charts dir: %s (Skipping)\n", sourcedir, err) + continue + } continue } + filename := file.Name() sourcefile := filepath.Join(source, filename) destfile := filepath.Join(dest, filename)