From de79cfa1fc4770242ef9792fe6f16ce99d4c1a19 Mon Sep 17 00:00:00 2001 From: Wilken Rivera Date: Fri, 13 Oct 2023 12:22:48 -0400 Subject: [PATCH] Fix filepath.Clean for file prefixed URLs --- multistep/commonsteps/step_download.go | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/multistep/commonsteps/step_download.go b/multistep/commonsteps/step_download.go index e0a4272cd..a096b9834 100644 --- a/multistep/commonsteps/step_download.go +++ b/multistep/commonsteps/step_download.go @@ -221,11 +221,16 @@ func (s *StepDownload) download(ctx context.Context, ui packersdk.Ui, source str } src := u.String() if u.Scheme == "" || strings.ToLower(u.Scheme) == "file" { - // If a local filepath, then we need to preprocess to make sure the - // path doens't have any multiple successive path separators; if it + // If a local filepath, then we need to pre-process to make sure the + // path doesn't have any multiple successive path separators; if it // does, go-getter will read this as a specialized go-getter-specific // subdirectory command, which it most likely isn't. src = filepath.Clean(u.String()) + // Starting with Go 1.21.0 filepath.Clean on Windows treats paths containing file: as invalid + // so they are prefixed with a dot (.) followed by the os.PathSeparator + if runtime.GOOS == "windows" && (src[0] == '.' && src[1] == os.PathSeparator) { + src = strings.TrimLeft(src, ".\\") + } if _, err := os.Stat(filepath.Clean(u.Path)); err != nil { // Cleaned path isn't present on system so it must be some other // scheme. Don't error right away; see if go-getter can figure it