Nvm-like support for switching between different versions of dfx

To switch between dfx versions, I currently do the following:

DFX_VERSION=<version_I_want_to_use> sh -ci "$(curl -fsSL https://internetcomputer.org/install.sh)"

Is there an easier way of hopping between different dfx versions?

If it doesn’t already exist, I would love nvm like support for switching between dfx versions, with the following commands:

dfx versions ls → lists all installed dfx versions
dfx versions use <version> → attempts to switch to use the version passed
dfx versions install <version> → attempts to install the version passed

One can then use this nifty module to write backwards compatibility tests for their developer tooling in order to see if a tool’s workflow will break between different dfx versions.

This module could also be extended, to have commands like

dfx versions alias <alias_name> <version> to be able to alias different dfx versions representing different project, or levels of confidence (i.e. stable, latest, etc.)

4 Likes

If you have e.g. "dfx": "0.11.2" in your dfx.json, dfx will always use that version for calls made from this project folder (or error out if the version is not installed). If this is working properly, dfx calls will show this warning text:

Warning: The version of DFX used (0.11.2) is different than the version being run (0.12.0-beta.3).
This might happen because your dfx.json specifies an older version, or DFX_VERSION is set in your environment.
We are forwarding the command line to the old version. To disable this warning, set the DFX_WARNING=-version_check environment variable.

Do you think this is an appropriate solution?

How does this work with the new global dfx?

Since the new dfx is running on a different port, could one could have 2 different dfx servers with different versions running simultaneously?

Maybe I’m in the minority, but I’d rather have dfx throw an error and have me switch my dfx version rather than trying to swap it under the hood.

I use Nix to achieve this. The following allows me to use sandboxed versions of dfx in various projects and could be used to test different releases.

1 Like

Good question, I haven’t thought too much about that so far… If you define a network in your project’s dfx.json everything will behave as before. If you use the system-wide dfx things get a bit more hairy: If you run dfx start in some random location on your system, it will run the currently active version. If you run dfx start in a project that’s pinned to a dfx version, it will run the pinned version of `dfx start.

CC @ericswanson probably worth thinking a bit more about how this is supposed to work

Yes, it all depends on how you define your networks. This has been this way for quite a while now, you just have to set the "bind" property to a different port

No. Pointing dfx.json to a version does no ensure to exactly that particular version is used.

For NodeJS I use n therefore I agree with @icme, being able to switch between version easily (including stable and beta ), particularly for those who participate to more than one project, would be handy.

Ok, I created this feature request on our Discussions page so that we can discuss possible implementations there. Please also upvote the discussion if you’re interested in that so that we have some signal to prioritise features.

2 Likes

Thanks, commented and upvoted :+1: