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

Allow version matching against local downloaded versions, instead of always matching remote versions #727

Closed
hubertnnn opened this issue May 26, 2022 · 8 comments

Comments

@hubertnnn
Copy link

hubertnnn commented May 26, 2022

Bug Report

Edit: original issue title was "n should use latest available version of node if installation is impossible".

Summary

When a new version of node gets released then n will start throwing errors instead of using latest available version

Steps to Reproduce

# Assuming 14.19.3 is the latest version of branch 14
# Uninstall latest version
sudo n rm 14.19.3
# Install a slightly older one
sudo n 14.19.2
# Run an application using latest version of v14 branch
n exec 14 node --version

Expected Behaviour

v14.19.2

Application should run showing v14.19.2, since that is the latest installed version of node
and you don't have permissions to install a newer one (not root).

Actual Behaviour

Error: '14' (14.19.3) not in downloads cache

Other Information

I am not sure if choosing between downloading and using preinstalled node should be done automatically.
Possibly n exec should never download node since in most cases it will not be possible either way
(n exec <app> should never be run as root and only root can install new node version)

Maybe having a switch to download/use local would be an option here as well.

Configuration Details

$ n --version
8.2.0

$ command -v node
/usr/local/bin/node

$ node -p process.platform
linux
@shadowspawn
Copy link
Collaborator

shadowspawn commented May 26, 2022

Short version: working as intended

Long version

The current behaviour of version lookups is to consult the available downloads, rather than the local cached versions.

By default n run and n exec only use cached versions and do not perform a download. The message you saw about "not in downloads cache" was not related to permissions.

There isn't currently an option to use the latest matching cached version. Another scenario that was raised in an earlier issue is no internet access: #404.

You can check what versions are available locally with n ls.

@ikrong
Copy link

ikrong commented Jun 10, 2022

When there is version 14 on the user's computer, even if it is not the latest version, the local version should be used first when executing n exec 14, and an error should not be reported. A warning can be prompted; when there is no version 14 on the user's computer, an error should be reported when executing n exec 14.

local version 14.19.1
remote latest version 14.19.3

n exec 14 npm -v

expect behaviour

6.14.12

actual behaviour

Error: '14' (14.19.3) not in downloads cache

Both n run and n exec should prefer the local version

@shadowspawn
Copy link
Collaborator

shadowspawn commented Jun 11, 2022

I don't think it likely I will change the default behaviour. There could be a new option to change the matching from "latest version according to Node.js" to "latest version already downloaded", as suggested in original post:

Maybe having a switch to download/use local would be an option here as well.

Would it affect all the version matching? e.g.14, lts, current, fermium, engine

@hubertnnn
Copy link
Author

Switch would be ok, but if possible make the switch available in a config file as well, so existing code will not break.
This way as an administrator I will be able to change the behavior for the entire server instead of having to change in the code of every single application that is running on the server.

@shadowspawn
Copy link
Collaborator

The n ls output is deliberately free of escape codes, so for now you can roll your own lookup like:

target_version=$(n ls | grep 'node/14.' | tail -n 1)
n exec "${target_version}" node --version

@shadowspawn
Copy link
Collaborator

shadowspawn commented Jul 16, 2022

The current behaviour is that unless the target version for a command is a fully qualified version number, then it will be resolved against the remote downloadable versions of Node.js. This works the same way for install and which and run and exec and even rm. This allows specifying versions like 16 or 16.2 or lts or current or carbon or nightly or auto, and you get the same resolved version across all the commands.

Historical note: I did consider using local matches when adding the partial match support for which and run and exec back in n v6.0.0, but decided to use the remote resolution. This keeps things consistent across all the commands and version labels.

In v6.0.0 run would never download but that is now supported with --download, which made the remote lookup more convenient to use without checking whether the target version is already downloaded.

There could perhaps be a --local which would cause version resolution to match against the local already downloaded versions rather than remote versions. I think it would be quite a bit of work to do fully, as there are a lot of labels to consider!

I'll leave this open for longer to see if there is more interest.

@shadowspawn shadowspawn changed the title n should use latest available version of node if installation is impossible Allow matching against local downloaded versions instead of always resolving against remote versions Jul 16, 2022
@shadowspawn shadowspawn changed the title Allow matching against local downloaded versions instead of always resolving against remote versions Allow version matching against local downloaded versions, instead of always matching remote versions Jul 16, 2022
@shadowspawn
Copy link
Collaborator

This issue has not had any further activity in six months.

Feel free to open a new issue if it comes up again, with new information and renewed interest.

Thank you for your contributions.

@shadowspawn
Copy link
Collaborator

Added --offline option in n v9.2.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants