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

Can't use system as default with --use-on-cd and --version-file-strategy recursive #681

Open
NicolasWebDev opened this issue Feb 23, 2022 · 3 comments
Labels
bug Something isn't working

Comments

@NicolasWebDev
Copy link

NicolasWebDev commented Feb 23, 2022

Hi,

With eval "$(fnm env --version-file-strategy recursive --use-on-cd)" on my bashrc and system set as default, when opening a shell I have the following error:

error: Could not find any version to use. Maybe you don't have a default version set?
Try running `fnm default <VERSION>` to set one, or create a .node-version file inside your project to declare a Node.js version.

Creating a .node-version with "system" in my home directory doesn't solve the problem.

If I remove one of both flags, then it works. If I set default to another version, then it works. fnm default system doesn't give any error. fnm use system either.

I am using fnm-bin 1.31.0 on arch.

My use case is that I would like to always use the system version of node, and only use fnm for my work and personal projects.
Thus I can globally always have the version of node auto-update, through my package manager.

@Schniz Schniz added the bug Something isn't working label Mar 6, 2022
@sarmong
Copy link

sarmong commented Nov 2, 2022

Same problem.

@aquacash5
Copy link
Contributor

aquacash5 commented Oct 14, 2023

After looking over the code, this error seems to come from the fact that --version-file-strategy recursive runs on every cd, not just the ones that it may be useful like --version-file-strategy local.

Local:

__fnm_use_if_file_found() {{
    if [[ -f .node-version || -f .nvmrc ]]; then
        fnm use --silent-if-unchanged
    fi
}}

__fnmcd() {{
    \cd "$@" || return $?
    __fnm_use_if_file_found
}}

alias cd=__fnmcd

Recursive:

__fnm_use_if_file_found() {{
    fnm use --silent-if-unchanged
}}

__fnmcd() {{
    \cd "$@" || return $?
    __fnm_use_if_file_found
}}

alias cd=__fnmcd

A solution is to suppress the CantInferVersion error if silent_if_unchanged is true. Instead fnm could add a debug line saying that fnm is suppressing the error due to the use of silent_if_unchanged. This would only be visible when the user sets the RUST_LOG environment variable.

Technically, if fnm does not find a version, fnm does not change the version, so fnm could silence the error with silent_if_unchanged.

This is wrong and unnecessary.

@aquacash5
Copy link
Contributor

The issue is if you make your default system. On *nix systems, fnm sets the default alias to symlink to /dev/null/installation. When fnm tries to get the default version, fnm calls canonicalize on that symlink.

Since canonicalize follows symlinks, we get a NotADirectory error; Instead of, the system alias.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants