Skip to content

feat: prune orphaned Node.js versions after install, fixes #7325 #7326

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

Merged
merged 3 commits into from
May 29, 2025

Conversation

lozcalver
Copy link
Contributor

The Issue

How This PR Solves The Issue

Runs the n prune command

Manual Testing Instructions

Open the ddev-global-cache volume and look at a long-standing project’s directory: n_prefix/<project-name>/n/versions/node/. It’ll likely already contain multiple Node.js versions.

After this change, the next time you start DDEV only the most recent Node.js version should be present

Automated Testing Overview

I’m not really sure how you’d test this, but given that this is a non-essential enhancement (everything’s running fine without this change, it just saves some disk space) I don’t think additional testing is necessary

Release/Deployment Notes

N/A

@lozcalver lozcalver requested a review from a team as a code owner May 22, 2025 09:16
@@ -47,6 +47,9 @@ if [ "${n_install_result}" = "false" ]; then
rm -f "${N_PREFIX}" && exit 6
fi

# prune orphaned Node.js versions
log-stderr.sh n prune || true
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wasn’t sure if including log-stderr.sh here was worth it given that this command isn’t really essential. Happy to remove it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's fine to use log-stderr.sh here.

Copy link
Member

@stasadev stasadev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

I'm pushing a new Docker image now.

Important: test starting/stopping the project, then disable Internet and start the project again. It should work as before.

@@ -47,6 +47,9 @@ if [ "${n_install_result}" = "false" ]; then
rm -f "${N_PREFIX}" && exit 6
fi

# prune orphaned Node.js versions
log-stderr.sh n prune || true
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's fine to use log-stderr.sh here.

@github-actions github-actions bot added the dependencies Pull requests that update a dependency file label May 22, 2025
@stasadev
Copy link
Member

A new Docker image is ready, build artifacts will appear shortly below.

Copy link

github-actions bot commented May 22, 2025

@lozcalver
Copy link
Contributor Author

Hmm, it’s not working properly - it removes all the versions. Node still “works” but when restarting without an internet connection it fails over to Node 22 (presumably whatever is installed with the image by default).

I think this may be because the prune command needs to be moved after the symlinks are added here:

# create symlinks on success
for node_binary in "${N_PREFIX}/bin/"*; do
if [ -f "${node_binary}" ]; then
ln -sf "${node_binary}" "${system_node_dir}"
fi
done
ln -sf "${system_node_dir}/node" "${system_node_dir}/nodejs"

n’s prune command will detect the current “active” node version before it runs, and it does so like this:

https://github.com/tj/n/blob/f52d2172f12cd76f0efe9524690723f52ab74f40/bin/n#L477-L494

I’ve pushed a commit that moves the command. If that works I can squash the commits + force push if you’d like, not sure if you’d prefer that done upfront or if you’ll squash + merge

@stasadev
Copy link
Member

I’ve pushed a commit that moves the command. If that works I can squash the commits + force push if you’d like, not sure if you’d prefer that done upfront or if you’ll squash + merge

We always squash on merge, no need to do it here.

Pushing a new image.

@stasadev
Copy link
Member

The new Docker image is ready.

Before testing run:

ddev poweroff
docker pull ddev/ddev-webserver:20250522_lozcalver_n_prune

@lozcalver
Copy link
Contributor Author

Great, it’s working now. With a Node 18 project, only 18.20.8 remains after starting DDEV. Tested disabling my internet connection before starting, that works too:

Screenshot 2025-05-22 at 11 40 01

@stasadev
Copy link
Member

Great!

I'll test it later today as well.

(Ignore the "Container tests" failure: it's unrelated to this PR.)

@stasadev stasadev self-requested a review May 22, 2025 13:38
@stasadev stasadev force-pushed the prune-node-versions branch from 0480dbd to 02c6ff9 Compare May 22, 2025 15:39
@stasadev
Copy link
Member

Rebased to fix tests.

Copy link
Member

@stasadev stasadev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

There might be cases where someone wants to install multiple Node versions, but they can work around it by using a different N_PREFIX.

The logic behaves as expected:

$ ddev config --nodejs-version="14.1.0"
$ ddev start

$ ddev exec node -v
v14.1.0

$ ddev exec "n install 14 && n install 16 && n install 18"

$ ddev exec n ls
node/14.1.0
node/14.21.3
node/16.20.2
node/18.20.8

$ ddev config --nodejs-version="14"
$ ddev restart

$ ddev exec node -v
v14.21.3

# only one version as expected
$ ddev exec n ls
node/14.21.3

$ ddev exec "n install 14.21.2 && n install 14.21.1 && n install 14.21.0"

$ ddev exec n ls
node/14.21.0
node/14.21.1
node/14.21.2
node/14.21.3

$ ddev poweroff

# turn off the Internet

$ ddev start

$ ddev exec node -v
v14.21.3

# only one version as expected
$ ddev exec n ls
node/14.21.3

$ ddev restart

$ ddev exec node -v
v14.21.3

# only one version as expected
$ ddev exec n ls
node/14.21.3

@stasadev stasadev merged commit 570918f into ddev:main May 29, 2025
21 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
dependencies Pull requests that update a dependency file enhancement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants