pyiron / pyiron_workflow

Graph-and-node based workflows
BSD 3-Clause "New" or "Revised" License
10 stars 1 forks source link

:bug: `working_directory` does not play nicely with macros #316

Open liamhuber opened 1 month ago

liamhuber commented 1 month ago

If you try to leverage .working_directory inside a macro definition, it is sensitive to whether or not that macro is being intialized/used with(out) a parent. This is because the working_directory leverages the semantic path to create a file path, but once created it gets frozen. So if you create then parent, the working directory will miss out on all the path stuff from the (eventual) parent. Because of the order of setup, if you pass the parent at initialization, it works ok.

Example:

If we instantiate the macro by itself, then assign it to a workflow, we get the wrong path

from pyiron_workflow import Workflow

@Workflow.wrap.as_function_node("path")
def ResolveWorkingDirectory(wd):
    return str(wd.path.resolve())

@Workflow.wrap.as_macro_node()
def UsesItsWorkingDirectory(self):
    self.path = ResolveWorkingDirectory(self.working_directory)
    return self.path

wf = Workflow("create_then_parent")
wf.m = UsesItsWorkingDirectory(label="no_parent")
wf()
>>> {'m__path': '/Users/huber/work/pyiron/notebooks/no_parent'}

Desired(???): {'m__path': '/Users/huber/work/pyiron/notebooks/create_then_parent/no_parent'}

But by passing the workflow as a parent at initialization everything is ok

wf = Workflow("parent_inside_init")
wf.m = UsesItsWorkingDirectory(label="with_parent", parent=wf)
wf()
>>> {'m__path': '/Users/huber/work/pyiron/notebooks/parent_inside_init/with_parent'}

TBH, I'm not actually sure what behaviour I want here. The working directory/semantic path interaction is not something I have totally worked out in my head yet. But the current behaviour is definitely a nasty gotcha.