What does `pkg> activate` do?

I think this is a relatively simple question, and I think I already know the answer but thought it might be worth asking anyway to get a more in depth understanding (which I don’t currently have) or have someone correct me if I’m completely wrong.

What does

pkg> activate .

actually do?

  • I know that it activates the current environment, if an environment exists in the current working directory.
  • My understanding is that this command searches for a Project.toml file in the current directory. I think this is the important point?
  • It doesn’t seem to matter if a Project.toml file does not exist in the current working directory, because the pkg REPL just reports that it has activated an environment with the current working directory name. (But no files are changed on disk.)
  • If Project.toml does not exist in the current working directory, then a new environment is created in the current working directory. This doesn’t actually do anything unless a pkg> add PACKAGE is run, which will create the Project.toml file, because it does not exist.

Consider the following, possibly surprising behavior.

  1. Create a new Package with pkg> generate MYPACKAGE
  2. This creates a subdirectory called MYPACKAGE/src
  3. cd to src
  4. Run the Julia REPL, ] to change to the pkg REPL, then run activate .
  5. Rather than activating the environment in the parent directory, this creates a new environment in the src directory. Is that a bit odd?

Also to note: Activating a package does not seem to change the environment variable LOAD_PATH, which can be printed by accessing shell mode in the REPL and running

echo $LOAD_PATH

It sets the current working directory (which is what . means) as the active environment, full stop. It doesn’t matter if there’s an environment already there or not, or if there is one in the directory above it, etc. It’s a simple, direct command, it isn’t doing anything fancy.

Then, in a later command, if you add a package to an environment which is empty, it will create a Project.toml and Manifest.toml; if you add a package to an environment with an existing Project.toml, it will add the package to it, and so forth. But that’s unrelated to activate.

Note you don’t need to activate the current working directory; you can activate some other directory. E.g. pkg> activate MySubDirectory or pkg> activate .. to activate the directory above the current one, or pkg> activate /envs/myabspath/MyProject etc.

2 Likes