Просмотр исходного кода

Merge pull request #41327 from thaJeztah/improve_build_errors

Builder: print relative path if COPY/ADD source path was not found
Sebastiaan van Stijn 5 лет назад
Родитель
Сommit
0db8310ec1
2 измененных файлов с 7 добавлено и 6 удалено
  1. 6 0
      builder/dockerfile/copy.go
  2. 1 6
      integration-cli/docker_cli_build_test.go

+ 6 - 0
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
 	}
 

+ 1 - 6
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",
 	})
 }