# Buggy code
def ensure_implicit_collections_populated(self, history, params):
    if not self.collection_info:
        return

    history = history or self.tool.get_default_history_by_trans(self.trans)
    if self.invocation_step.is_new:
        self.precreate_output_collections(history, params)
    else:
        collections = {}
        for output_assoc in self.invocation_step.output_dataset_collections:
            implicit_collection = output_assoc.dataset_collection
            assert hasattr(
                implicit_collection, "history_content_type"
            )  # make sure it is an HDCA and not a DC
            collections[output_assoc.output_name] = output_assoc.dataset_collection
        self.implicit_collections = collections
    self.invocation_step.implicit_collection_jobs = self.implicit_collection_jobs


# Traceback (KeyError)
Dec 18 11:53:29 sn04.bi.uni-freiburg.de python[2113691]: galaxy.workflow.run ERROR 2020-12-18 11:53:28,997 Failed to schedule Workflow[id=74152,name=COVID-19: variation analysis reporting v2], problem occurred on WorkflowStep[index=25,type=tool].
Dec 18 11:53:29 sn04.bi.uni-freiburg.de python[2113691]: Traceback (most recent call last):
Dec 18 11:53:29 sn04.bi.uni-freiburg.de python[2113691]: File "/opt/galaxy/server/lib/galaxy/workflow/run.py", line 353, in replacement_for_connection
Dec 18 11:53:29 sn04.bi.uni-freiburg.de python[2113691]: replacement = step_outputs[output_name]
Dec 18 11:53:29 sn04.bi.uni-freiburg.de python[2113691]: KeyError: 'out_file1'
Dec 18 11:53:29 sn04.bi.uni-freiburg.de python[2113691]: During handling of the above exception, another exception occurred:
Dec 18 11:53:29 sn04.bi.uni-freiburg.de python[2113691]: Traceback (most recent call last):
Dec 18 11:53:29 sn04.bi.uni-freiburg.de python[2113691]: File "/opt/galaxy/server/lib/galaxy/workflow/run.py", line 190, in invoke
Dec 18 11:53:29 sn04.bi.uni-freiburg.de python[2113691]: incomplete_or_none = self._invoke_step(workflow_invocation_step)
Dec 18 11:53:29 sn04.bi.uni-freiburg.de python[2113691]: File "/opt/galaxy/server/lib/galaxy/workflow/run.py", line 266, in _invoke_step
Dec 18 11:53:29 sn04.bi.uni-freiburg.de python[2113691]: use_cached_job=self.workflow_invocation.use_cached_job)
Dec 18 11:53:29 sn04.bi.uni-freiburg.de python[2113691]: File "/opt/galaxy/server/lib/galaxy/workflow/modules.py", line 1666, in execute
Dec 18 11:53:29 sn04.bi.uni-freiburg.de python[2113691]: collection_info = self.compute_collection_info(progress, step, all_inputs)
Dec 18 11:53:29 sn04.bi.uni-freiburg.de python[2113691]: File "/opt/galaxy/server/lib/galaxy/workflow/modules.py", line 336, in compute_collection_info
Dec 18 11:53:29 sn04.bi.uni-freiburg.de python[2113691]: progress, step, all_inputs
Dec 18 11:53:29 sn04.bi.uni-freiburg.de python[2113691]: File "/opt/galaxy/server/lib/galaxy/workflow/modules.py", line 354, in _find_collections_to_match
Dec 18 11:53:29 sn04.bi.uni-freiburg.de python[2113691]: data = progress.replacement_for_input(step, input_dict)
Dec 18 11:53:29 sn04.bi.uni-freiburg.de python[2113691]: File "/opt/galaxy/server/lib/galaxy/workflow/run.py", line 337, in replacement_for_input
Dec 18 11:53:29 sn04.bi.uni-freiburg.de python[2113691]: replacement = self.replacement_for_connection(connection[0], is_data=is_data)
Dec 18 11:53:29 sn04.bi.uni-freiburg.de python[2113691]: File "/opt/galaxy/server/lib/galaxy/workflow/run.py", line 358, in replacement_for_connection
Dec 18 11:53:29 sn04.bi.uni-freiburg.de python[2113691]: raise Exception(message)
Dec 18 11:53:29 sn04.bi.uni-freiburg.de python[2113691]: Exception: Workflow evaluation problem - failed to find output_name out_file1 in step_outputs {}
Dec 18 11:53:29 sn04.bi.uni-freiburg.de python[2113691]: galaxy.workflow.run ERROR 2020-12-18 11:53:29,002 Failed to execute scheduled workflow.
Dec 18 11:53:29 sn04.bi.uni-freiburg.de python[2113691]: Traceback (most recent call last):
Dec 18 11:53:29 sn04.bi.uni-freiburg.de python[2113691]: File "/opt/galaxy/server/lib/galaxy/workflow/run.py", line 353, in replacement_for_connection
Dec 18 11:53:29 sn04.bi.uni-freiburg.de python[2113691]: replacement = step_outputs[output_name]
Dec 18 11:53:29 sn04.bi.uni-freiburg.de python[2113691]: KeyError: 'out_file1'
Dec 18 11:53:29 sn04.bi.uni-freiburg.de python[2113691]: During handling of the above exception, another exception occurred:
Dec 18 11:53:29 sn04.bi.uni-freiburg.de python[2113691]: Traceback (most recent call last):
Dec 18 11:53:29 sn04.bi.uni-freiburg.de python[2113691]: File "/opt/galaxy/server/lib/galaxy/workflow/run.py", line 83, in __invoke
Dec 18 11:53:29 sn04.bi.uni-freiburg.de python[2113691]: outputs = invoker.invoke()
Dec 18 11:53:29 sn04.bi.uni-freiburg.de python[2113691]: File "/opt/galaxy/server/lib/galaxy/workflow/run.py", line 190, in invoke
Dec 18 11:53:29 sn04.bi.uni-freiburg.de python[2113691]: incomplete_or_none = self._invoke_step(workflow_invocation_step)
Dec 18 11:53:29 sn04.bi.uni-freiburg.de python[2113691]: File "/opt/galaxy/server/lib/galaxy/workflow/run.py", line 266, in _invoke_step
Dec 18 11:53:29 sn04.bi.uni-freiburg.de python[2113691]: use_cached_job=self.workflow_invocation.use_cached_job)
Dec 18 11:53:29 sn04.bi.uni-freiburg.de python[2113691]: File "/opt/galaxy/server/lib/galaxy/workflow/modules.py", line 1666, in execute
Dec 18 11:53:29 sn04.bi.uni-freiburg.de python[2113691]: collection_info = self.compute_collection_info(progress, step, all_inputs)
Dec 18 11:53:29 sn04.bi.uni-freiburg.de python[2113691]: File "/opt/galaxy/server/lib/galaxy/workflow/modules.py", line 336, in compute_collection_info
Dec 18 11:53:29 sn04.bi.uni-freiburg.de python[2113691]: progress, step, all_inputs
Dec 18 11:53:29 sn04.bi.uni-freiburg.de python[2113691]: File "/opt/galaxy/server/lib/galaxy/workflow/modules.py", line 354, in _find_collections_to_match
Dec 18 11:53:29 sn04.bi.uni-freiburg.de python[2113691]: data = progress.replacement_for_input(step, input_dict)
Dec 18 11:53:29 sn04.bi.uni-freiburg.de python[2113691]: File "/opt/galaxy/server/lib/galaxy/workflow/run.py", line 337, in replacement_for_input
Dec 18 11:53:29 sn04.bi.uni-freiburg.de python[2113691]: replacement = self.replacement_for_connection(connection[0], is_data=is_data)
Dec 18 11:53:29 sn04.bi.uni-freiburg.de python[2113691]: File "/opt/galaxy/server/lib/galaxy/workflow/run.py", line 358, in replacement_for_connection
Dec 18 11:53:29 sn04.bi.uni-freiburg.de python[2113691]: raise Exception(message)
Dec 18 11:53:29 sn04.bi.uni-freiburg.de python[2113691]: Exception: Workflow evaluation problem - failed to find output_name out_file1 in step_outputs {}
Dec 18 11:53:29 sn04.bi.uni-freiburg.de python[2113691]: galaxy.workflow.scheduling_manager DEBUG 2020-12-18 11:53:29,021 Workflow invocation [129864] scheduled


# Fixed code
def ensure_implicit_collections_populated(self, history, params):
    if not self.collection_info:
        return

    history = history or self.tool.get_default_history_by_trans(self.trans)
    if self.invocation_step.is_new:
        self.precreate_output_collections(history, params)
        for output_name, implicit_collection in self.implicit_collections.items():
            self.invocation_step.add_output(output_name, implicit_collection)
    else:
        collections = {}
        for output_assoc in self.invocation_step.output_dataset_collections:
            implicit_collection = output_assoc.dataset_collection
            assert hasattr(
                implicit_collection, "history_content_type"
            )  # make sure it is an HDCA and not a DC
            collections[output_assoc.output_name] = output_assoc.dataset_collection
        self.implicit_collections = collections
    self.invocation_step.implicit_collection_jobs = self.implicit_collection_jobs