diff --git a/lib/spack/spack/environment/environment.py b/lib/spack/spack/environment/environment.py index a2670df27a9..f423cbd7e4d 100644 --- a/lib/spack/spack/environment/environment.py +++ b/lib/spack/spack/environment/environment.py @@ -248,6 +248,12 @@ def read(name): def create(name, init_file=None, with_view=None, keep_relative=False): """Create a named environment in Spack.""" + if not os.path.isdir(env_root_path()): + # double check that the root path exists before we check the full environment name + raise SpackEnvironmentError( + "The environments_root '{er}' does not exist. Please create the directory or provide " + "a valid path in the config.yaml".format(er=env_root_path()) + ) validate_env_name(name) if exists(name): raise SpackEnvironmentError("'%s': environment already exists" % name) diff --git a/lib/spack/spack/test/config.py b/lib/spack/spack/test/config.py index 75ed622a9cf..6df16a64654 100644 --- a/lib/spack/spack/test/config.py +++ b/lib/spack/spack/test/config.py @@ -1441,12 +1441,25 @@ def test_environment_created_in_users_location(mutable_config, tmpdir): """Test that an environment is created in a location based on the config""" spack.config.set("config:environments_root", str(tmpdir.join("envs"))) env_dir = spack.config.get("config:environments_root") + assert tmpdir.strpath in env_dir assert not os.path.isdir(env_dir) + os.makedirs(env_dir) env("create", "test") out = env("list") - assert "test" in out + assert "test" in out assert env_dir in ev.root("test") assert os.path.isdir(os.path.join(env_dir, "test")) + + +def test_environment_errors_if_root_missing(mutable_config, tmpdir): + spack.config.set("config:environments_root", str(tmpdir.join("envs"))) + env_dir = spack.config.get("config:environments_root") + + assert tmpdir.strpath in env_dir + assert not os.path.isdir(env_dir) + + with pytest.raises(ev.SpackEnvironmentError, match=env_dir): + env("create", "test")