-
Notifications
You must be signed in to change notification settings - Fork 50
fix: allow node_modules removal in build phase #259
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
Open
iloveitaly
wants to merge
11
commits into
railwayapp:main
Choose a base branch
from
iloveitaly:node-modules-cache
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
d6ac84a
test: test install in build phase
iloveitaly bf8b7cc
docs: make it clear that cache folders cannot be removed
iloveitaly 837b858
refactor: remove obvious comments
iloveitaly fba2780
fix: detect node_modules removal and avoid caching node_modules/.cache
iloveitaly d05f694
test: add new snapshot
iloveitaly 3be40c9
build: adding go debugging tool and helper task
iloveitaly 3f644d0
docs: minor comments
iloveitaly 9afb262
refactor: node_modules detection is working
iloveitaly a7e1947
test: test install in build phase
iloveitaly bb280c7
refactor: pull plan cleansing into a separate file
iloveitaly 955d8ff
refactor: ai cleanup
iloveitaly File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
155 changes: 155 additions & 0 deletions
155
core/__snapshots__/TestGenerateBuildPlanForExamples_node-npm-install-in-build_1.snap.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,155 @@ | ||
| { | ||
| "caches": { | ||
| "node-modules": { | ||
| "directory": "/app/node_modules/.cache", | ||
| "type": "shared" | ||
| }, | ||
| "npm-install": { | ||
| "directory": "/root/.npm", | ||
| "type": "shared" | ||
| } | ||
| }, | ||
| "deploy": { | ||
| "base": { | ||
| "image": "ghcr.io/railwayapp/railpack-runtime:latest" | ||
| }, | ||
| "inputs": [ | ||
| { | ||
| "include": [ | ||
| "/mise/shims", | ||
| "/mise/installs", | ||
| "/usr/local/bin/mise", | ||
| "/etc/mise/config.toml", | ||
| "/root/.local/state/mise" | ||
| ], | ||
| "step": "packages:mise" | ||
| }, | ||
| { | ||
| "include": [ | ||
| "/app/node_modules" | ||
| ], | ||
| "step": "build" | ||
| }, | ||
| { | ||
| "exclude": [ | ||
| "node_modules", | ||
| ".yarn" | ||
| ], | ||
| "include": [ | ||
| "/root/.cache", | ||
| "." | ||
| ], | ||
| "step": "build" | ||
| }, | ||
| { | ||
| "include": [ | ||
| "." | ||
| ], | ||
| "step": "build" | ||
| } | ||
| ], | ||
| "startCommand": "npm run start", | ||
| "variables": { | ||
| "CI": "true", | ||
| "NODE_ENV": "production", | ||
| "NPM_CONFIG_FUND": "false", | ||
| "NPM_CONFIG_PRODUCTION": "false", | ||
| "NPM_CONFIG_UPDATE_NOTIFIER": "false" | ||
| } | ||
| }, | ||
| "steps": [ | ||
| { | ||
| "assets": { | ||
| "mise.toml": "[mise.toml]" | ||
| }, | ||
| "commands": [ | ||
| { | ||
| "path": "/mise/shims" | ||
| }, | ||
| { | ||
| "customName": "create mise config", | ||
| "name": "mise.toml", | ||
| "path": "/etc/mise/config.toml" | ||
| }, | ||
| { | ||
| "cmd": "sh -c 'mise trust -a \u0026\u0026 mise install'", | ||
| "customName": "install mise packages: node" | ||
| } | ||
| ], | ||
| "inputs": [ | ||
| { | ||
| "image": "ghcr.io/railwayapp/railpack-builder:latest" | ||
| } | ||
| ], | ||
| "name": "packages:mise", | ||
| "variables": { | ||
| "MISE_CACHE_DIR": "/mise/cache", | ||
| "MISE_CONFIG_DIR": "/mise", | ||
| "MISE_DATA_DIR": "/mise", | ||
| "MISE_INSTALLS_DIR": "/mise/installs", | ||
| "MISE_NODE_VERIFY": "false", | ||
| "MISE_SHIMS_DIR": "/mise/shims" | ||
| } | ||
| }, | ||
| { | ||
| "caches": [ | ||
| "npm-install" | ||
| ], | ||
| "commands": [ | ||
| { | ||
| "path": "/app/node_modules/.bin" | ||
| }, | ||
| { | ||
| "cmd": "mkdir -p /app/node_modules/.cache" | ||
| }, | ||
| { | ||
| "dest": "package.json", | ||
| "src": "package.json" | ||
| }, | ||
| { | ||
| "dest": "package-lock.json", | ||
| "src": "package-lock.json" | ||
| }, | ||
| { | ||
| "cmd": "npm ci" | ||
| } | ||
| ], | ||
| "inputs": [ | ||
| { | ||
| "step": "packages:mise" | ||
| } | ||
| ], | ||
| "name": "install", | ||
| "variables": { | ||
| "CI": "true", | ||
| "NODE_ENV": "production", | ||
| "NPM_CONFIG_FUND": "false", | ||
| "NPM_CONFIG_PRODUCTION": "false", | ||
| "NPM_CONFIG_UPDATE_NOTIFIER": "false" | ||
| } | ||
| }, | ||
| { | ||
| "commands": [ | ||
| { | ||
| "cmd": "sh -c 'npm ci'", | ||
| "customName": "npm ci" | ||
| } | ||
| ], | ||
| "inputs": [ | ||
| { | ||
| "step": "install" | ||
| }, | ||
| { | ||
| "include": [ | ||
| "." | ||
| ], | ||
| "local": true | ||
| } | ||
| ], | ||
| "name": "build", | ||
| "secrets": [ | ||
| "*" | ||
| ] | ||
| } | ||
| ] | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| package core | ||
|
|
||
| import ( | ||
| "regexp" | ||
|
|
||
| "github.com/railwayapp/railpack/core/logger" | ||
| "github.com/railwayapp/railpack/core/plan" | ||
| "github.com/railwayapp/railpack/core/providers/node" | ||
| ) | ||
|
|
||
| // Regexes for matching commands that intentionally remove node_modules or perform | ||
| // clean installs (which implicitly delete the directory) so we can avoid mounting | ||
| // the node_modules cache in those steps. | ||
| var ( | ||
| // Matches "npm ci" with flexible whitespace, using word boundaries | ||
| npmCiCommandRegex = regexp.MustCompile(`(?i)\bnpm\s+ci\b`) | ||
|
|
||
| // Matches common delete commands targeting node_modules | ||
| removeNodeModulesRegex = regexp.MustCompile(`(?i)\b(?:rm\s+-r[f]?|rmdir|rimraf)\s+(?:\S*\/)?node_modules\b`) | ||
| ) | ||
|
|
||
| // willRemoveNodeModules determines if any command in the provided slice removes | ||
| // the node_modules directory either directly (rm/rimraf) or indirectly (npm ci). | ||
| // this is brittle & imperfect: https://github.com/railwayapp/railpack/pull/259 | ||
| func willRemoveNodeModules(commands []plan.Command) bool { | ||
| for _, cmd := range commands { | ||
| if execCmd, ok := cmd.(plan.ExecCommand); ok { | ||
| if npmCiCommandRegex.MatchString(execCmd.Cmd) || removeNodeModulesRegex.MatchString(execCmd.Cmd) { | ||
| return true | ||
| } | ||
| } | ||
| } | ||
| return false | ||
| } | ||
|
|
||
| // cleansePlanStructure applies mutations to the build plan structure after it | ||
| // is generated but before validation / serialization. Today this focuses on | ||
| // detaching the node_modules cache from steps that explicitly remove | ||
| // node_modules so the global cache isn't invalidated unintentionally. | ||
| func cleansePlanStructure(buildPlan *plan.BuildPlan, logger *logger.Logger) { | ||
| // let's get the cache key name that has a Directory of NODE_MODULES_CACHE | ||
| var nodeModulesCacheKey string | ||
| for cacheName, cacheDef := range buildPlan.Caches { | ||
| if cacheDef.Directory == node.NODE_MODULES_CACHE { | ||
| nodeModulesCacheKey = cacheName | ||
| break | ||
| } | ||
| } | ||
|
|
||
| if nodeModulesCacheKey == "" { | ||
| // no node_modules cache defined, nothing to do | ||
| return | ||
| } | ||
|
|
||
| // Only detach the node modules cache from steps that remove node_modules themselves. | ||
| // Keep the global cache definition so earlier steps (like install) can still mount it. | ||
| for i, step := range buildPlan.Steps { | ||
| if step.Name == "install" || !willRemoveNodeModules(step.Commands) { | ||
| continue | ||
| } | ||
|
|
||
| before := len(step.Caches) | ||
| if before == 0 { | ||
| continue | ||
| } | ||
|
|
||
| // It's important that we do not result in an array with a zeroed string, which is why we are using this ugly loop | ||
| var newCaches []string | ||
| for _, name := range step.Caches { | ||
| if name != "" && name != nodeModulesCacheKey { | ||
| newCaches = append(newCaches, name) | ||
| } | ||
| } | ||
|
|
||
| buildPlan.Steps[i].Caches = newCaches | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| package core | ||
|
|
||
| import ( | ||
| "reflect" | ||
| "testing" | ||
|
|
||
| "github.com/railwayapp/railpack/core/logger" | ||
| "github.com/railwayapp/railpack/core/plan" | ||
| "github.com/railwayapp/railpack/core/providers/node" | ||
| ) | ||
|
|
||
| func newTestLogger() *logger.Logger { return logger.NewLogger() } | ||
|
|
||
| // helper to create a basic build plan with a node_modules cache (when withCache true) | ||
| func buildPlan(withCache bool) *plan.BuildPlan { | ||
| p := plan.NewBuildPlan() | ||
| if withCache { | ||
| p.Caches["node_modules"] = &plan.Cache{Directory: node.NODE_MODULES_CACHE, Type: plan.CacheTypeShared} | ||
| } | ||
| return p | ||
| } | ||
|
|
||
| func TestCleanse_CachePresent_StepDoesNotRemoveNodeModules(t *testing.T) { | ||
| p := buildPlan(true) | ||
| step := plan.Step{Name: "build", Caches: []string{"node_modules"}} | ||
| step.Commands = []plan.Command{plan.NewExecShellCommand("echo 'nothing to see'")} | ||
| p.Steps = append(p.Steps, step) | ||
|
|
||
| cleansePlanStructure(p, newTestLogger()) | ||
|
|
||
| // should remain mounted | ||
| if !reflect.DeepEqual(p.Steps[0].Caches, []string{"node_modules"}) { | ||
| t.Fatalf("expected cache to remain since step doesn't remove node_modules, got %#v", p.Steps[0].Caches) | ||
| } | ||
| } | ||
|
|
||
| func TestCleanse_CachePresent_StepRemovesNodeModules(t *testing.T) { | ||
| p := buildPlan(true) | ||
| step := plan.Step{Name: "build", Caches: []string{"node_modules"}} | ||
| step.Commands = []plan.Command{plan.NewExecShellCommand("rm -rf node_modules && echo done")} | ||
| p.Steps = append(p.Steps, step) | ||
|
|
||
| cleansePlanStructure(p, newTestLogger()) | ||
|
|
||
| if len(p.Steps[0].Caches) != 0 { // should be removed (allow nil or empty) | ||
| t.Fatalf("expected cache to be removed (nil or empty), got %#v", p.Steps[0].Caches) | ||
| } | ||
| } | ||
|
|
||
| func TestCleanse_InstallStepAlwaysKeepsCache(t *testing.T) { | ||
| p := buildPlan(true) | ||
| install := plan.Step{Name: "install", Caches: []string{"node_modules"}} | ||
| install.Commands = []plan.Command{plan.NewExecShellCommand("npm ci")} | ||
| p.Steps = append(p.Steps, install) | ||
|
|
||
| cleansePlanStructure(p, newTestLogger()) | ||
|
|
||
| if !reflect.DeepEqual(p.Steps[0].Caches, []string{"node_modules"}) { // should remain even though npm ci matches removal heuristic | ||
| t.Fatalf("expected install step cache to remain, got %#v", p.Steps[0].Caches) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure that this is the correct place for this file. Or maybe we should have an abstraction so that language providers can hook into cleansing. It just feels a bit off having node/npm specific stuff in the core/cleanse.go file when up until now all node and language specific logic has been isolated to the provider directories.