diff --git a/builder/dockerfile/copy.go b/builder/dockerfile/copy.go index d3613dcb3e..1982ec9bde 100644 --- a/builder/dockerfile/copy.go +++ b/builder/dockerfile/copy.go @@ -242,6 +242,8 @@ func (o *copier) calcCopyInfo(origPath string, allowWildcards bool) ([]copyInfo, // Deal with the single file case copyInfo, err := copyInfoForFile(o.source, origPath) switch { + case imageSource == nil && errors.Is(err, os.ErrNotExist): + return nil, errors.Wrapf(err, "file not found in build context or excluded by .dockerignore") case err != nil: return nil, err case copyInfo.hash != "": @@ -315,6 +317,10 @@ func (o *copier) copyWithWildcards(origPath string) ([]copyInfo, error) { func copyInfoForFile(source builder.Source, path string) (copyInfo, error) { fi, err := remotecontext.StatAt(source, path) if err != nil { + if errors.Is(err, os.ErrNotExist) { + // return the relative path in the error, which is more user-friendly than the full path to the tmp-dir + return copyInfo{}, errors.WithStack(&os.PathError{Op: "stat", Path: path, Err: os.ErrNotExist}) + } return copyInfo{}, err } diff --git a/integration-cli/docker_cli_build_test.go b/integration-cli/docker_cli_build_test.go index 977271baee..1acc49106b 100644 --- a/integration-cli/docker_cli_build_test.go +++ b/integration-cli/docker_cli_build_test.go @@ -2189,18 +2189,13 @@ func (s *DockerSuite) TestBuildEntrypointRunCleanup(c *testing.T) { func (s *DockerSuite) TestBuildAddFileNotFound(c *testing.T) { name := "testbuildaddnotfound" - expected := "foo: no such file or directory" - - if testEnv.OSType == "windows" { - expected = "foo: The system cannot find the file specified" - } buildImage(name, build.WithBuildContext(c, build.WithFile("Dockerfile", `FROM `+minimalBaseImage()+` ADD foo /usr/local/bar`), build.WithFile("bar", "hello"))).Assert(c, icmd.Expected{ ExitCode: 1, - Err: expected, + Err: "stat foo: file does not exist", }) }