diff --git a/core/container/externalbuilder/externalbuilder.go b/core/container/externalbuilder/externalbuilder.go index 1bb429cead6..3195b69fb67 100644 --- a/core/container/externalbuilder/externalbuilder.go +++ b/core/container/externalbuilder/externalbuilder.go @@ -153,12 +153,14 @@ func (d *Detector) Build(ccid string, mdBytes []byte, codeStream io.Reader) (*In durableReleaseDir := filepath.Join(durablePath, "release") err = CopyDir(logger, buildContext.ReleaseDir, durableReleaseDir) if err != nil { + os.RemoveAll(durablePath) return nil, errors.WithMessagef(err, "could not move or copy build context release to persistent location '%s'", durablePath) } durableBldDir := filepath.Join(durablePath, "bld") err = CopyDir(logger, buildContext.BldDir, durableBldDir) if err != nil { + os.RemoveAll(durablePath) return nil, errors.WithMessagef(err, "could not move or copy build context bld to persistent location '%s'", durablePath) } diff --git a/core/container/externalbuilder/externalbuilder_test.go b/core/container/externalbuilder/externalbuilder_test.go index 993f79b5bfe..30a07097c0a 100644 --- a/core/container/externalbuilder/externalbuilder_test.go +++ b/core/container/externalbuilder/externalbuilder_test.go @@ -155,6 +155,27 @@ var _ = Describe("externalbuilder", func() { Expect(err).To(MatchError("could not create dir 'path/to/nowhere/fake-package-id' to persist build output: mkdir path/to/nowhere/fake-package-id: no such file or directory")) }) }) + + Context("when the build output cannot be copied to the durable path", func() { + BeforeEach(func() { + detector.Builders = externalbuilder.CreateBuilders([]peer.ExternalBuilder{ + {Path: "testdata/symlinkbuilder", Name: "symlinkbuilder"}, + }, "mspid") + }) + + It("removes the partial build output", func() { + _, err := detector.Build("fake-package-id", md, codePackage) + Expect(err).To(MatchError(ContainSubstring("could not move or copy build context bld to persistent location"))) + Expect(filepath.Join(durablePath, "fake-package-id")).NotTo(BeAnExistingFile()) + + Expect(codePackage.Close()).To(Succeed()) + codePackage, err = os.Open("testdata/normal_archive.tar.gz") + Expect(err).NotTo(HaveOccurred()) + instance, err := detector.Build("fake-package-id", md, codePackage) + Expect(err).To(MatchError(ContainSubstring("could not move or copy build context bld to persistent location"))) + Expect(instance).To(BeNil()) + }) + }) }) Describe("CachedBuild", func() { diff --git a/core/container/externalbuilder/testdata/symlinkbuilder/bin/build b/core/container/externalbuilder/testdata/symlinkbuilder/bin/build new file mode 100755 index 00000000000..19f46bd48b6 --- /dev/null +++ b/core/container/externalbuilder/testdata/symlinkbuilder/bin/build @@ -0,0 +1,3 @@ +#!/bin/bash + +ln -s /etc/hosts "$3/absolute-link" diff --git a/core/container/externalbuilder/testdata/symlinkbuilder/bin/detect b/core/container/externalbuilder/testdata/symlinkbuilder/bin/detect new file mode 100755 index 00000000000..685ed195a73 --- /dev/null +++ b/core/container/externalbuilder/testdata/symlinkbuilder/bin/detect @@ -0,0 +1,7 @@ +#!/bin/bash + +if grep -q "fake-metadata" "$2/metadata.json" ; then + exit 0 +fi + +exit 1