Hydra: [Bug] get_original_cwd breaks without hydra

Created on 17 Mar 2020  路  4Comments  路  Source: facebookresearch/hydra

馃悰 Bug

get_original_cwd and to_absolute_path raises AttributeError, when used without hydra.
Here is the definition of get_original_cwd in utils.py

def get_original_cwd() -> str:
    ret = HydraConfig.instance().hydra.runtime.cwd # AttributeError
    assert ret is not None and isinstance(ret, str) # assertion is not catching anything
    return ret

Since HydraConfig.instance().hydra is already None when hydra is not on. Accessing the runtime attribute raises AttributeError here.
It shoud be changed to

def get_original_cwd():
    hydra_cfg = hydra.utils.HydraConfig().hydra
    if hydra_cfg is not None:
        ret = Path(hydra_cfg.runtime.cwd)
    else:
        ret = Path.cwd()
    return ret

To reproduce

* Minimal Code/Config snippet to reproduce *

import hydra

hydra.utils.get_original_cwd() # Attribute Error
hydra.utils.to_absolute_path('some_path') # this also breaks due to the same error

* Stack trace/error message *

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-24-3c0b668b9ce4> in <module>
----> 1 hydra.utils.to_absolute_path('something')

/opt/conda/lib/python3.7/site-packages/hydra/utils.py in to_absolute_path(path)
     77         ret = path
     78     else:
---> 79         ret = Path(get_original_cwd()) / path
     80     return str(ret)

/opt/conda/lib/python3.7/site-packages/hydra/utils.py in get_original_cwd()
     62 
     63 def get_original_cwd():
---> 64     return HydraConfig().hydra.runtime.cwd
     65 
     66 

AttributeError: 'NoneType' object has no attribute 'runtime'

Expected Behavior

I expect get_original_cwd to just return cwd, and to_absolute_path works fine even if hydra config is not set.

System information

  • Hydra Version : '0.11.3'
  • Python version : 3.7.4
  • Virtual environment type and version :
  • Operating system : Ubuntu 18.04.3 LTS (GNU/Linux 4.20.13-1.el7.elrepo.x86_64 x86_64)

Additional context

Add any other context about the problem here.

bug

All 4 comments

Thanks for reporting. for now you can use your own implementation outside of Hydra. I will address it with the next release.

What do you think about not cd into hydra dir during instantiation of object from the config?

This way class object or function can be written without any ..to_absolute_path() or get_original_cwd(), therefore can be instantiated with hydra or normally without hydra.

Only after all the instantiation in the config is done, then hydra cd into run dir.

What do you think about not cd into hydra dir during instantiation of object from the config?

This way class object or function can be written without any ..to_absolute_path() or get_original_cwd(), therefore can be instantiated with hydra or normally without hydra.

Only after all the instantiation in the config is done, then hydra cd into run dir.

While I understand why you want it, I think it's a terrible idea.
there is no "instantiation phase", your code calls it whenever it wants.

What will be better is to allow you to more easily define a path as absolute in your config, enabling your instantiated code to not depend on Hydra's utils.to_absolute_path().

Issue #325 is related to this (it will allow accessing Hydra's config itself in interpolations, this contains (or can contain) the original working directory.

for get_original_cwd(), I don't think it makes sense for it to return cwd because the semantics are not the same:
cwd can change, original cwd can not change.

My fix will throw a ValueError for get_original_cwd() if Hydra config is not initialized.

For to_absolute_path(), I will fix it to return a path relative to cwd().

Was this page helpful?
0 / 5 - 0 ratings