From 2106142b25fec7c0b0851fdd58b45ba99c318b5a Mon Sep 17 00:00:00 2001 From: Pavel Busko Date: Tue, 15 Aug 2023 11:29:31 +0200 Subject: [PATCH] Ignore removed workspace folders in setup-symlinks helper Co-authored-by: Ralf Pannemans --- cmd/setup-symlinks/internal/run.go | 13 +++++----- cmd/setup-symlinks/internal/run_test.go | 32 +++++++++++++++++++++++-- 2 files changed, 37 insertions(+), 8 deletions(-) diff --git a/cmd/setup-symlinks/internal/run.go b/cmd/setup-symlinks/internal/run.go index 1c6e3f37..0099cd9d 100644 --- a/cmd/setup-symlinks/internal/run.go +++ b/cmd/setup-symlinks/internal/run.go @@ -46,22 +46,23 @@ func Run(executablePath, appDir string, symlinkResolver npminstall.SymlinkResolv } func resolveWorkspaceModules(symlinkResolver npminstall.SymlinkResolver, appDir, layerPath string) error { - lockFile, err := symlinkResolver.ParseLockfile(filepath.Join(appDir, "package-lock.json")) if err != nil { return err } - dir := filepath.Dir(filepath.Join(appDir, "package-lock.json")) for _, pkg := range lockFile.Packages { if pkg.Link { - - linkPath, err := os.Readlink(filepath.Join(dir, pkg.Resolved)) + linkPath, err := os.Readlink(filepath.Join(appDir, pkg.Resolved)) if err != nil { - return err + if errors.Is(err, fs.ErrNotExist) { + continue + } else { + return err + } } - err = createSymlink(filepath.Join(layerPath, pkg.Resolved), filepath.Join(linkPath, pkg.Resolved)) + err = createSymlink(filepath.Join(layerPath, pkg.Resolved), linkPath) if err != nil { return err } diff --git a/cmd/setup-symlinks/internal/run_test.go b/cmd/setup-symlinks/internal/run_test.go index 0a000697..40e924ab 100644 --- a/cmd/setup-symlinks/internal/run_test.go +++ b/cmd/setup-symlinks/internal/run_test.go @@ -1,6 +1,7 @@ package internal_test import ( + "io/fs" "os" "path/filepath" "testing" @@ -143,9 +144,7 @@ func testRun(t *testing.T, context spec.G, it spec.S) { }) context("when appDir contains workspace packages ", func() { - it("ensures symlinks to the layer exist at launch time", func() { - err := resolver.Resolve(filepath.Join(appDir, "package-lock.json"), layerDir) Expect(err).NotTo(HaveOccurred()) @@ -175,7 +174,36 @@ func testRun(t *testing.T, context spec.G, it spec.S) { link, err = os.Readlink(link) Expect(err).NotTo(HaveOccurred()) Expect(link).To(Equal(filepath.Join(layerDir, "module-5"))) + }) + + it("ensures symlinks to the layer exists if the resolved path is missing", func() { + err := resolver.Resolve(filepath.Join(appDir, "package-lock.json"), layerDir) + Expect(err).NotTo(HaveOccurred()) + + err = os.RemoveAll(filepath.Join(appDir, "module-5")) + Expect(err).NotTo(HaveOccurred()) + + err = internal.Run(executablePath, appDir, resolver) + Expect(err).NotTo(HaveOccurred()) + + link, err := os.Readlink(filepath.Join(appDir, "src", "packages", "module-1")) + Expect(err).NotTo(HaveOccurred()) + Expect(link).To(Equal(filepath.Join(tmpDir, "src", "packages", "module-1"))) + + link, err = os.Readlink(link) + Expect(err).NotTo(HaveOccurred()) + Expect(link).To(Equal(filepath.Join(layerDir, "src", "packages", "module-1"))) + + link, err = os.Readlink(filepath.Join(appDir, "workspaces", "example", "module-3")) + Expect(err).NotTo(HaveOccurred()) + Expect(link).To(Equal(filepath.Join(tmpDir, "workspaces", "example", "module-3"))) + + link, err = os.Readlink(link) + Expect(err).NotTo(HaveOccurred()) + Expect(link).To(Equal(filepath.Join(layerDir, "workspaces", "example", "module-3"))) + _, err = os.Readlink(filepath.Join(appDir, "module-5")) + Expect(err).To(MatchError(fs.ErrNotExist)) }) })