@@ -20,13 +20,13 @@ import (
2020 "fmt"
2121 "os"
2222 "path/filepath"
23+ "strings"
2324
25+ "github.com/GoogleContainerTools/kaniko/pkg/constants"
2426 "github.com/moby/buildkit/frontend/dockerfile/instructions"
2527 "github.com/pkg/errors"
2628 "github.com/sirupsen/logrus"
2729
28- "github.com/GoogleContainerTools/kaniko/pkg/constants"
29-
3030 "github.com/GoogleContainerTools/kaniko/pkg/dockerfile"
3131 "github.com/GoogleContainerTools/kaniko/pkg/util"
3232 v1 "github.com/google/go-containerregistry/pkg/v1"
@@ -59,11 +59,15 @@ func (c *CopyCommand) ExecuteCommand(config *v1.Config, buildArgs *dockerfile.Bu
5959 if err != nil {
6060 return err
6161 }
62+ if fi .IsDir () && ! strings .HasSuffix (fullPath , string (os .PathSeparator )) {
63+ fullPath += "/"
64+ }
6265 cwd := config .WorkingDir
6366 if cwd == "" {
6467 cwd = constants .RootDir
6568 }
66- destPath , err := util .DestinationFilepath (src , dest , cwd )
69+
70+ destPath , err := util .DestinationFilepath (fullPath , dest , cwd )
6771 if err != nil {
6872 return err
6973 }
@@ -76,11 +80,7 @@ func (c *CopyCommand) ExecuteCommand(config *v1.Config, buildArgs *dockerfile.Bu
7680 }
7781
7882 if fi .IsDir () {
79- if ! filepath .IsAbs (dest ) {
80- // we need to add '/' to the end to indicate the destination is a directory
81- dest = filepath .Join (cwd , dest ) + "/"
82- }
83- copiedFiles , err := util .CopyDir (fullPath , dest , c .buildcontext )
83+ copiedFiles , err := util .CopyDir (fullPath , destPath , c .buildcontext )
8484 if err != nil {
8585 return err
8686 }
@@ -197,21 +197,42 @@ func (cr *CachingCopyCommand) From() string {
197197}
198198
199199func resolveIfSymlink (destPath string ) (string , error ) {
200- baseDir := filepath .Dir (destPath )
201- if info , err := os .Lstat (baseDir ); err == nil {
202- switch mode := info .Mode (); {
203- case mode & os .ModeSymlink != 0 :
204- linkPath , err := os .Readlink (baseDir )
205- if err != nil {
206- return "" , errors .Wrap (err , "error reading symlink" )
200+ if ! filepath .IsAbs (destPath ) {
201+ return "" , errors .New ("dest path must be abs" )
202+ }
203+
204+ var nonexistentPaths []string
205+
206+ newPath := destPath
207+ for newPath != "/" {
208+ _ , err := os .Lstat (newPath )
209+ if err != nil {
210+ if os .IsNotExist (err ) {
211+ dir , file := filepath .Split (newPath )
212+ newPath = filepath .Clean (dir )
213+ nonexistentPaths = append (nonexistentPaths , file )
214+ continue
215+ } else {
216+ return "" , errors .Wrap (err , "failed to lstat" )
207217 }
208- absLinkPath := filepath .Join (filepath .Dir (baseDir ), linkPath )
209- newPath := filepath .Join (absLinkPath , filepath .Base (destPath ))
210- logrus .Tracef ("Updating destination path from %v to %v due to symlink" , destPath , newPath )
211- return newPath , nil
212218 }
219+
220+ newPath , err = filepath .EvalSymlinks (newPath )
221+ if err != nil {
222+ return "" , errors .Wrap (err , "failed to eval symlinks" )
223+ }
224+ break
213225 }
214- return destPath , nil
226+
227+ for i := len (nonexistentPaths ) - 1 ; i >= 0 ; i -- {
228+ newPath = filepath .Join (newPath , nonexistentPaths [i ])
229+ }
230+
231+ if destPath != newPath {
232+ logrus .Tracef ("Updating destination path from %v to %v due to symlink" , destPath , newPath )
233+ }
234+
235+ return filepath .Clean (newPath ), nil
215236}
216237
217238func copyCmdFilesUsedFromContext (
0 commit comments