diff --git a/dev-tools/mage/dockerbuilder.go b/dev-tools/mage/dockerbuilder.go index f99f4f1dcdb..0e0bd2078ca 100644 --- a/dev-tools/mage/dockerbuilder.go +++ b/dev-tools/mage/dockerbuilder.go @@ -46,7 +46,7 @@ func (b *dockerBuilder) Build() error { } if err := b.copyFiles(); err != nil { - return err + return fmt.Errorf("error copying files for docker variant %q: %w", b.DockerVariant, err) } if err := b.prepareBuild(); err != nil { diff --git a/magefile.go b/magefile.go index 03aecaeb620..670920cf128 100644 --- a/magefile.go +++ b/magefile.go @@ -1537,7 +1537,7 @@ func downloadBinary(ctx context.Context, project string, packageName string, bin } compl.Add(1) - fmt.Printf("Done downloading %s\n", packageName) + fmt.Printf("Done downloading %s into %s\n", packageName, targetPath) return nil } } @@ -1590,7 +1590,8 @@ func movePackagesToArchive(dropPath string, platformPackageSuffixes []string, pa if mg.Verbose() { log.Printf("--- Evaluating moving dependency %s to archive path %s\n", f, archivePath) } - if !strings.Contains(f, packageSuffix) && !isPythonWheelPackage(f, packageVersion) { + // if the matched file name does not contain the platform suffix and it's not a platform-independent package, skip it + if !strings.Contains(f, packageSuffix) && !isPlatformIndependentPackage(f, packageVersion) { if mg.Verbose() { log.Printf("--- Skipped moving dependency %s to archive path\n", f) } @@ -1613,9 +1614,18 @@ func movePackagesToArchive(dropPath string, platformPackageSuffixes []string, pa if err := os.MkdirAll(targetDir, 0750); err != nil { fmt.Printf("warning: failed to create directory %s: %s", targetDir, err) } - if err := os.Rename(f, targetPath); err != nil { - panic(fmt.Errorf("failed renaming file: %w", err)) + + // Platform-independent packages need to be placed in the archive sub-folders for all platforms, copy instead of moving + if isPlatformIndependentPackage(f, packageVersion) { + if err := copyFile(f, targetPath); err != nil { + panic(fmt.Errorf("failed copying file: %w", err)) + } + } else { + if err := os.Rename(f, targetPath); err != nil { + panic(fmt.Errorf("failed renaming file: %w", err)) + } } + if mg.Verbose() { log.Printf("--- Moved dependency in archive path %s => %s\n", f, targetPath) } @@ -1625,10 +1635,37 @@ func movePackagesToArchive(dropPath string, platformPackageSuffixes []string, pa return archivePath } -func isPythonWheelPackage(f string, packageVersion string) bool { +func copyFile(src, dst string) error { + srcStat, err := os.Stat(src) + if err != nil { + return fmt.Errorf("stat source file %q: %w", src, err) + } + + srcF, err := os.Open(src) + if err != nil { + return fmt.Errorf("opening source file %q: %w", src, err) + } + defer srcF.Close() + + dstF, err := os.OpenFile(dst, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, srcStat.Mode()|os.ModePerm) + if err != nil { + return fmt.Errorf("opening/creating destination file %q: %w", dst, err) + } + defer dstF.Close() + + _, err = io.Copy(dstF, srcF) + if err != nil { + return fmt.Errorf("copying file %q to %q: %w", src, dst, err) + } + + return nil +} + +func isPlatformIndependentPackage(f string, packageVersion string) bool { fileBaseName := filepath.Base(f) for _, spec := range manifest.ExpectedBinaries { packageName := spec.GetPackageName(packageVersion, "") + // as of now only python wheels packages are platform-independent if spec.PythonWheel && (fileBaseName == packageName || fileBaseName == packageName+sha512FileExt) { return true }