Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Symlink node_modules/.cache to tempdir #661

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,10 @@ file that looks like the following:

## Configuration

| Environment Variable | Description |
|-------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `$BP_NPM_VERSION` | If set, this custom version of `npm` will be used instead of the one provided by the `nodejs` installation. |
| Environment Variable | Description |
|-----------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `$BP_NPM_VERSION` | If set, this custom version of `npm` will be used instead of the one provided by the `nodejs` installation. |
| `$BP_KEEP_NODE_BUILD_CACHE` | If set to `true` (default `false`), the folder `node_modules/.cache` will not be removed after the build, but will be readonly at runtime. |

## Usage

Expand Down
17 changes: 17 additions & 0 deletions build.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,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)
Expand Down Expand Up @@ -282,6 +284,21 @@ func Build(entryResolver EntryResolver,
return packit.BuildResult{}, err
}

keepBuildCache, _ := environment.Lookup("BP_KEEP_NODE_BUILD_CACHE")
if keepBuildCache != "true" {
linkName := filepath.Join(layer.Path, "node_modules", ".cache")
err = os.RemoveAll(linkName)
if err != nil {
return packit.BuildResult{}, err
}

cacheFolder := filepath.Join(os.TempDir(), NODE_MODULES_CACHE)
err = os.Symlink(cacheFolder, linkName)
if err != nil {
return packit.BuildResult{}, err
}
}

if build {
err = symlinkResolver.Copy(filepath.Join(projectPath, "package-lock.json"), buildLayerPath, layer.Path)
if err != nil {
Expand Down
73 changes: 73 additions & 0 deletions build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ func testBuild(t *testing.T, context spec.G, it spec.S) {
layersDir string
workingDir string
cnbDir string
tempDir string

processLayerDir string
processWorkingDir string
Expand Down Expand Up @@ -60,6 +61,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{}
Expand Down Expand Up @@ -423,6 +427,38 @@ func testBuild(t *testing.T, context spec.G, it spec.S) {
Expect(symlinkResolver.ResolveCall.Receives.LockfilePath).To(Equal(filepath.Join(workingDir, "package-lock.json")))
Expect(symlinkResolver.ResolveCall.Receives.LayerPath).To(Equal(filepath.Join(layersDir, "launch-modules")))
})

it("symlinks node_modules/.cache to tmp/node_modules_cache in order to work for the run user", func() {
err := os.MkdirAll(filepath.Join(tempDir, "node_modules_cache", "temp-content"), os.ModePerm)
Expect(err).NotTo(HaveOccurred())
result, err := build(packit.BuildContext{
BuildpackInfo: packit.BuildpackInfo{
SBOMFormats: []string{"application/vnd.cyclonedx+json", "application/spdx+json", "application/vnd.syft+json"},
},
Platform: packit.Platform{
Path: "some-platform-path",
},
WorkingDir: workingDir,
Layers: packit.Layers{Path: layersDir},
CNBPath: cnbDir,
Plan: packit.BuildpackPlan{
Entries: []packit.BuildpackPlanEntry{
{Name: "node_modules"},
},
},
})
Expect(err).NotTo(HaveOccurred())

Expect(len(result.Layers)).To(Equal(2))

launchLayer := result.Layers[0]
Expect(launchLayer.Name).To(Equal("launch-modules"))
Expect(launchLayer.Path).To(Equal(filepath.Join(layersDir, "launch-modules")))

linkedTmpContent := filepath.Join(launchLayer.Path, "node_modules", ".cache", "temp-content")
_, err = os.Stat(linkedTmpContent)
Expect(err).NotTo(HaveOccurred())
})
})

context("when node_modules is required at build and launch", func() {
Expand Down Expand Up @@ -674,6 +710,39 @@ func testBuild(t *testing.T, context spec.G, it spec.S) {
Expect(symlinkResolver.CopyCall.Receives.SourceLayerPath).To(Equal(filepath.Join(buildLayer.Path)))
Expect(symlinkResolver.CopyCall.Receives.TargetLayerPath).To(Equal(filepath.Join(launchLayer.Path)))
})

it("symlinks node_modules/.cache to tmp/node_modules_cache in order to work for the run user", func() {
err := os.MkdirAll(filepath.Join(tempDir, "node_modules_cache", "temp-content"), os.ModePerm)
Expect(err).NotTo(HaveOccurred())
result, err := build(packit.BuildContext{
BuildpackInfo: packit.BuildpackInfo{
SBOMFormats: []string{"application/vnd.cyclonedx+json", "application/spdx+json", "application/vnd.syft+json"},
},
Platform: packit.Platform{
Path: "some-platform-path",
},
WorkingDir: workingDir,
Layers: packit.Layers{Path: layersDir},
CNBPath: cnbDir,
Plan: packit.BuildpackPlan{
Entries: []packit.BuildpackPlanEntry{
{Name: "node_modules"},
},
},
})
Expect(err).NotTo(HaveOccurred())

Expect(len(result.Layers)).To(Equal(3))

launchLayer := result.Layers[1]
Expect(launchLayer.Name).To(Equal("launch-modules"))
Expect(launchLayer.Path).To(Equal(filepath.Join(layersDir, "launch-modules")))

linkedTmpContent := filepath.Join(launchLayer.Path, "node_modules", ".cache", "temp-content")
_, err = os.Stat(linkedTmpContent)
Expect(err).NotTo(HaveOccurred())
})

})

context("when one npmrc binding is detected", func() {
Expand Down Expand Up @@ -768,6 +837,10 @@ func testBuild(t *testing.T, context spec.G, it spec.S) {
if err != nil {
return err
}
err = os.MkdirAll(filepath.Join(ld, "node_modules"), os.ModePerm)
if err != nil {
return err
}

return nil
}
Expand Down
6 changes: 6 additions & 0 deletions buildpack.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ api = "0.7"
name = "BP_NPM_VERSION"
description = "configures a custom npm version"

[[metadata.configurations]]
name = "BP_KEEP_NODE_BUILD_CACHE"
default = "false"
description = "keep the 'node_modules/.cache' folder after the build (will be readonly at runtime)'"

[[metadata.configurations]]
name = "NODE_HOME"
description = "path the Node.js installation"
Expand All @@ -45,5 +50,6 @@ api = "0.7"
default = "error"
description = "configures npm output verbosity"


[[stacks]]
id = "*"
8 changes: 7 additions & 1 deletion cmd/setup-symlinks/internal/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions cmd/setup-symlinks/internal/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())

Expand Down
1 change: 1 addition & 0 deletions integration/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ func TestIntegration(t *testing.T) {
suite("EmptyNodeModules", testEmptyNodeModules)
suite("Logging", testLogging)
suite("NativeModules", testNativeModules)
suite("NodeModulesCache", testReact)
suite("NoNodeModules", testNoNodeModules)
suite("Npmrc", testNpmrc)
suite("PackageLockMismatch", testPackageLockMismatch)
Expand Down
99 changes: 99 additions & 0 deletions integration/react_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package integration_test

import (
"os"
"path/filepath"
"testing"
"time"

"github.com/paketo-buildpacks/occam"
"github.com/sclevine/spec"

. "github.com/onsi/gomega"
. "github.com/paketo-buildpacks/occam/matchers"
)

func testReact(t *testing.T, context spec.G, it spec.S) {
var (
Expect = NewWithT(t).Expect
Eventually = NewWithT(t).Eventually

pack occam.Pack
docker occam.Docker
imageIDs map[string]struct{}
containerIDs map[string]struct{}

name string
source string

pullPolicy = "never"
)

it.Before(func() {
imageIDs = make(map[string]struct{})
containerIDs = make(map[string]struct{})

pack = occam.NewPack().WithNoColor()
docker = occam.NewDocker()

var err error
name, err = occam.RandomName()
Expect(err).NotTo(HaveOccurred())

if settings.Extensions.UbiNodejsExtension.Online != "" {
pullPolicy = "always"
}

})

it.After(func() {
for id := range containerIDs {
Expect(docker.Container.Remove.Execute(id)).To(Succeed())
}

for id := range imageIDs {
Expect(docker.Image.Remove.Execute(id)).To(Succeed())
}

Expect(os.RemoveAll(source)).To(Succeed())
})

context("when the app is a react app", func() {
it("works", func() {
var err error
source, err = occam.Source(filepath.Join("testdata", "react_app"))
Expect(err).NotTo(HaveOccurred())

build := pack.Build.
WithPullPolicy(pullPolicy).
WithExtensions(
settings.Extensions.UbiNodejsExtension.Online,
).
WithBuildpacks(
settings.Buildpacks.NodeEngine.Online,
settings.Buildpacks.NPMInstall.Online,
settings.Buildpacks.BuildPlan.Online,
)

image, logs, err := build.Execute(name, source)
Expect(err).NotTo(HaveOccurred(), logs.String)

container, err := docker.Container.Run.
WithCommand("npm start").
WithEnv(map[string]string{"PORT": "8080"}).
WithPublish("8080").
Execute(image.ID)
Expect(err).NotTo(HaveOccurred())

containerIDs[container.ID] = struct{}{}

Eventually(container).Should(BeAvailable())

Eventually(func() string {
cLogs, err := docker.Container.Logs.Execute(container.ID)
Expect(err).NotTo(HaveOccurred())
return cLogs.String()
}).WithTimeout(3 * time.Second).Should(ContainSubstring("webpack compiled successfully"))
})
})
}
38 changes: 38 additions & 0 deletions integration/testdata/react_app/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"name": "react-app",
"version": "0.1.0",
"private": true,
"dependencies": {
"@testing-library/jest-dom": "^5.17.0",
"@testing-library/react": "^13.4.0",
"@testing-library/user-event": "^13.5.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-scripts": "5.0.1",
"web-vitals": "^2.1.4"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
}
11 changes: 11 additions & 0 deletions integration/testdata/react_app/plan.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[[requires]]
name = "node"

[requires.metadata]
launch = true

[[requires]]
name = "node_modules"

[requires.metadata]
launch = true
Binary file not shown.
43 changes: 43 additions & 0 deletions integration/testdata/react_app/public/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="icon" href="%PUBLIC_URL%/favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="theme-color" content="#000000" />
<meta
name="description"
content="Web site created using create-react-app"
/>
<link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" />
<!--
manifest.json provides metadata used when your web app is installed on a
user's mobile device or desktop. See https://developers.google.com/web/fundamentals/web-app-manifest/
-->
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
<!--
Notice the use of %PUBLIC_URL% in the tags above.
It will be replaced with the URL of the `public` folder during the build.
Only files inside the `public` folder can be referenced from the HTML.
Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will
work correctly both with client-side routing and a non-root public URL.
Learn how to configure a non-root public URL by running `npm run build`.
-->
<title>React App</title>
</head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<div id="root"></div>
<!--
This HTML file is a template.
If you open it directly in the browser, you will see an empty page.
You can add webfonts, meta tags, or analytics to this file.
The build step will place the bundled scripts into the <body> tag.
To begin the development, run `npm start` or `yarn start`.
To create a production bundle, use `npm run build` or `yarn build`.
-->
</body>
</html>
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Loading