From fb5a3e35dadebdf0d1fff006461fda271c551b58 Mon Sep 17 00:00:00 2001 From: Ralf Pannemans Date: Fri, 12 Apr 2024 14:37:48 +0200 Subject: [PATCH] Symlink node_modules/.cache to tempdir --- build.go | 19 +++++++++++++++++++ build_test.go | 11 +++++++++++ cmd/setup-symlinks/internal/run.go | 8 +++++++- cmd/setup-symlinks/internal/run_test.go | 1 + 4 files changed, 38 insertions(+), 1 deletion(-) diff --git a/build.go b/build.go index 0b08afb0..d4b8e79f 100644 --- a/build.go +++ b/build.go @@ -14,6 +14,8 @@ import ( "github.com/paketo-buildpacks/packit/v2/scribe" ) +const NODE_MODULES_CACHE = "node_modules_cache" + //go:generate faux --interface BuildManager --output fakes/build_manager.go type BuildManager interface { Resolve(workingDir string) (BuildProcess, bool, error) @@ -134,6 +136,23 @@ func Build(entryResolver EntryResolver, return packit.BuildResult{}, err } + cacheFolder := filepath.Join(os.TempDir(), NODE_MODULES_CACHE) + err = os.Mkdir(cacheFolder, os.ModePerm) + if err != nil { + return packit.BuildResult{}, err + } + + linkName := filepath.Join(layer.Path, "node_modules", ".cache") + err = os.RemoveAll(linkName) + if err != nil { + return packit.BuildResult{}, err + } + + err = os.Symlink(cacheFolder, linkName) + if err != nil { + return packit.BuildResult{}, err + } + err = linker.Link(filepath.Join(projectPath, "node_modules"), filepath.Join(layer.Path, "node_modules")) if err != nil { return packit.BuildResult{}, err diff --git a/build_test.go b/build_test.go index c6ec01b6..43ef72ff 100644 --- a/build_test.go +++ b/build_test.go @@ -60,6 +60,9 @@ func testBuild(t *testing.T, context spec.G, it spec.S) { cnbDir, err = os.MkdirTemp("", "cnb") Expect(err).NotTo(HaveOccurred()) + tempDir := t.TempDir() + t.Setenv("TMPDIR", tempDir) + t.Setenv("BP_NODE_PROJECT_PATH", "") buildProcess = &fakes.BuildProcess{} @@ -117,6 +120,7 @@ func testBuild(t *testing.T, context spec.G, it spec.S) { environment, symlinkResolver, ) + }) it.After(func() { @@ -154,6 +158,13 @@ func testBuild(t *testing.T, context spec.G, it spec.S) { buildLayer := result.Layers[0] Expect(buildLayer.Name).To(Equal("build-modules")) Expect(buildLayer.Path).To(Equal(filepath.Join(layersDir, "build-modules"))) + + nodeModuleCache := filepath.Join(layersDir, "build-modules", "node_modules", ".cache") + _, err = os.Stat(nodeModuleCache) + Expect(err).NotTo(HaveOccurred()) + // FIXME: Why does this not work? + //Expect(info.Mode()&os.ModeSymlink == os.ModeSymlink).To(BeTrue()) + Expect(buildLayer.SharedEnv).To(Equal(packit.Environment{})) Expect(buildLayer.BuildEnv).To(Equal(packit.Environment{ "PATH.append": filepath.Join(layersDir, "build-modules", "node_modules", ".bin"), diff --git a/cmd/setup-symlinks/internal/run.go b/cmd/setup-symlinks/internal/run.go index 0099cd9d..3d09f7f7 100644 --- a/cmd/setup-symlinks/internal/run.go +++ b/cmd/setup-symlinks/internal/run.go @@ -42,7 +42,13 @@ func Run(executablePath, appDir string, symlinkResolver npminstall.SymlinkResolv return err } - return createSymlink(filepath.Join(layerPath, "node_modules"), linkPath) + err = createSymlink(filepath.Join(layerPath, "node_modules"), linkPath) + if err != nil { + return err + } + + cacheFolder := filepath.Join(os.TempDir(), npminstall.NODE_MODULES_CACHE) + return os.Mkdir(cacheFolder, os.ModePerm) } func resolveWorkspaceModules(symlinkResolver npminstall.SymlinkResolver, appDir, layerPath string) error { diff --git a/cmd/setup-symlinks/internal/run_test.go b/cmd/setup-symlinks/internal/run_test.go index 40e924ab..d7695979 100644 --- a/cmd/setup-symlinks/internal/run_test.go +++ b/cmd/setup-symlinks/internal/run_test.go @@ -41,6 +41,7 @@ func testRun(t *testing.T, context spec.G, it spec.S) { tmpDir, err = os.MkdirTemp("", "tmp") Expect(err).NotTo(HaveOccurred()) + t.Setenv("TMPDIR", tmpDir) Expect(os.Symlink(filepath.Join(tmpDir, "node_modules"), filepath.Join(appDir, "node_modules"))).To(Succeed())