# Buggy code
def dataset_states_and_extensions_summary(self):
    if not hasattr(self, "_dataset_states_and_extensions_summary"):
        db_session = object_session(self)

        dc = alias(DatasetCollection.table)
        de = alias(DatasetCollectionElement.table)
        hda = alias(HistoryDatasetAssociation.table)
        dataset = alias(Dataset.table)

        select_from = dc.outerjoin(de, de.c.dataset_collection_id == dc.c.id)

        depth_collection_type = self.collection_type
        while ":" in depth_collection_type:
            child_collection = alias(DatasetCollection.table)
            child_collection_element = alias(DatasetCollectionElement.table)
            select_from = select_from.outerjoin(
                child_collection, child_collection.c.id == de.c.child_collection_id
            )
            select_from = select_from.outerjoin(
                child_collection_element,
                child_collection_element.c.dataset_collection_id
                == child_collection.c.id,
            )

            de = child_collection_element
            depth_collection_type = depth_collection_type.split(":", 1)[1]

        select_from = select_from.outerjoin(hda, hda.c.id == de.c.hda_id).outerjoin(
            dataset, hda.c.dataset_id == dataset.c.id
        )
        select_stmt = (
            select([hda.c.extension, dataset.c.state])
            .select_from(select_from)
            .where(dc.c.id == self.id)
            .distinct()
        )
        extensions = set()
        states = set()
        for extension, state in db_session.execute(select_stmt).fetchall():
            states.add(state)
            extensions.add(extension)

        self._dataset_states_and_extensions_summary = (states, extensions)

    return self._dataset_states_and_extensions_summary


# Traceback (Exception)
[pid: 63775|app: 0|req: 160/596] 127.0.0.1 () {50 vars in 1324 bytes} [Wed Jul 31 09:38:45 2019] GET /api/histories/1cd8e2f6b131e891/contents?details=3f5830403180d620&amp;order=hid&amp;v=dev&amp;q=update_time-ge&amp;q=deleted&amp;q=purged&amp;qv=2019-07-31T16%3A38%3A41.000Z&amp;qv=False&amp;qv=False => generated 2 bytes in 53 msecs (HTTP/1.1 200) 3 headers in 139 bytes (1 switches on core 3)
galaxy.workflow.scheduling_manager DEBUG 2019-07-31 09:38:45,835 [p:63775,w:2,m:0] [WorkflowRequestMonitor.monitor_thread] Attempting to schedule workflow invocation [(4,)]
galaxy.workflow.run DEBUG 2019-07-31 09:38:45,941 [p:63775,w:2,m:0] [WorkflowRequestMonitor.monitor_thread] Marking step 10 outputs of invocation 4 delayed (tool [__BUILD_LIST__] inputs are not ready, this special tool requires inputs to be ready)
galaxy.workflow.run DEBUG 2019-07-31 09:38:45,941 [p:63775,w:2,m:0] [WorkflowRequestMonitor.monitor_thread] Marking step 11 outputs of invocation 4 delayed (dependent step [10] delayed, so this step must be delayed)
galaxy.workflow.run DEBUG 2019-07-31 09:38:45,941 [p:63775,w:2,m:0] [WorkflowRequestMonitor.monitor_thread] Marking step 12 outputs of invocation 4 delayed (dependent step [11] delayed, so this step must be delayed)
galaxy.workflow.run DEBUG 2019-07-31 09:38:45,942 [p:63775,w:2,m:0] [WorkflowRequestMonitor.monitor_thread] Marking step 13 outputs of invocation 4 delayed (dependent step [11] delayed, so this step must be delayed)
galaxy.workflow.run DEBUG 2019-07-31 09:38:45,942 [p:63775,w:2,m:0] [WorkflowRequestMonitor.monitor_thread] Marking step 14 outputs of invocation 4 delayed (dependent step [13] delayed, so this step must be delayed)
galaxy.workflow.scheduling_manager DEBUG 2019-07-31 09:38:45,957 [p:63775,w:2,m:0] [WorkflowRequestMonitor.monitor_thread] Workflow invocation [4] scheduled
galaxy.workflow.scheduling_manager DEBUG 2019-07-31 09:38:46,960 [p:63775,w:2,m:0] [WorkflowRequestMonitor.monitor_thread] Attempting to schedule workflow invocation [(4,)]
galaxy.workflow.run DEBUG 2019-07-31 09:38:47,070 [p:63775,w:2,m:0] [WorkflowRequestMonitor.monitor_thread] Marking step 10 outputs of invocation 4 delayed (tool [__BUILD_LIST__] inputs are not ready, this special tool requires inputs to be ready)
galaxy.workflow.run DEBUG 2019-07-31 09:38:47,071 [p:63775,w:2,m:0] [WorkflowRequestMonitor.monitor_thread] Marking step 11 outputs of invocation 4 delayed (dependent step [10] delayed, so this step must be delayed)
galaxy.workflow.run DEBUG 2019-07-31 09:38:47,071 [p:63775,w:2,m:0] [WorkflowRequestMonitor.monitor_thread] Marking step 12 outputs of invocation 4 delayed (dependent step [11] delayed, so this step must be delayed)
galaxy.workflow.run DEBUG 2019-07-31 09:38:47,072 [p:63775,w:2,m:0] [WorkflowRequestMonitor.monitor_thread] Marking step 13 outputs of invocation 4 delayed (dependent step [11] delayed, so this step must be delayed)
galaxy.workflow.run DEBUG 2019-07-31 09:38:47,072 [p:63775,w:2,m:0] [WorkflowRequestMonitor.monitor_thread] Marking step 14 outputs of invocation 4 delayed (dependent step [13] delayed, so this step must be delayed)
galaxy.workflow.scheduling_manager DEBUG 2019-07-31 09:38:47,086 [p:63775,w:2,m:0] [WorkflowRequestMonitor.monitor_thread] Workflow invocation [4] scheduled
galaxy.jobs.runners.drmaa DEBUG 2019-07-31 09:38:47,465 [p:63775,w:2,m:0] [SlurmRunner.monitor_thread] (10/24674328) state change: job finished normally
galaxy.model.metadata DEBUG 2019-07-31 09:38:47,587 [p:63775,w:2,m:0] [SlurmRunner.work_thread-0] loading metadata from file for: HistoryDatasetAssociation 16
galaxy.jobs INFO 2019-07-31 09:38:47,692 [p:63775,w:2,m:0] [SlurmRunner.work_thread-0] Collecting metrics for Job 10 in /project/6004808/ncm3/galaxy-19.05-git/galaxy-database/jobs_directory/000/10
galaxy.jobs DEBUG 2019-07-31 09:38:47,703 [p:63775,w:2,m:0] [SlurmRunner.work_thread-0] job 10 ended (finish() executed in (159.904 ms))
galaxy.workflow.scheduling_manager DEBUG 2019-07-31 09:38:48,090 [p:63775,w:2,m:0] [WorkflowRequestMonitor.monitor_thread] Attempting to schedule workflow invocation [(4,)]
galaxy.managers.collections DEBUG 2019-07-31 09:38:48,247 [p:63775,w:2,m:0] [WorkflowRequestMonitor.monitor_thread] Created collection with 2 elements
/project/6004808/ncm3/galaxy-19.05-git/galaxy/.venv/lib/python2.7/site-packages/sqlalchemy/sql/sqltypes.py:268: SAWarning: Unicode type received non-unicode bind param value 'list'. (this warning may be suppressed after 10 occurrences)
(util.ellipses_string(value),),
/project/6004808/ncm3/galaxy-19.05-git/galaxy/.venv/lib/python2.7/site-packages/sqlalchemy/sql/sqltypes.py:268: SAWarning: Unicode type received non-unicode bind param value 'output'. (this warning may be suppressed after 10 occurrences)
(util.ellipses_string(value),),
galaxy.tools.actions.model_operations INFO 2019-07-31 09:38:48,343 [p:63775,w:2,m:0] [WorkflowRequestMonitor.monitor_thread] Calling produce_outputs, tool is <galaxy.tools.BuildListCollectionTool object at 0x7f32a9f2a690>
galaxy.tools.execute DEBUG 2019-07-31 09:38:48,348 [p:63775,w:2,m:0] [WorkflowRequestMonitor.monitor_thread] Tool [__BUILD_LIST__] created job [11] (153.141 ms)
galaxy.tools.execute DEBUG 2019-07-31 09:38:48,358 [p:63775,w:2,m:0] [WorkflowRequestMonitor.monitor_thread] Executed 1 job(s) for tool __BUILD_LIST__ request: (178.532 ms)
galaxy.workflow.run DEBUG 2019-07-31 09:38:48,368 [p:63775,w:2,m:0] [WorkflowRequestMonitor.monitor_thread] Workflow step 10 of invocation 4 invoked (189.270 ms)
galaxy.managers.collections DEBUG 2019-07-31 09:38:48,441 [p:63775,w:2,m:0] [WorkflowRequestMonitor.monitor_thread] Created collection with 0 elements
galaxy.managers.collections DEBUG 2019-07-31 09:38:48,469 [p:63775,w:2,m:0] [WorkflowRequestMonitor.monitor_thread] Created collection with 2 elements
/project/6004808/ncm3/galaxy-19.05-git/galaxy/.venv/lib/python2.7/site-packages/sqlalchemy/sql/sqltypes.py:268: SAWarning: Unicode type received non-unicode bind param value 'input'. (this warning may be suppressed after 10 occurrences)
(util.ellipses_string(value),),
/project/6004808/ncm3/galaxy-19.05-git/galaxy/.venv/lib/python2.7/site-packages/sqlalchemy/sql/sqltypes.py:268: SAWarning: Unicode type received non-unicode bind param value 'output_discarded'. (this warning may be suppressed after 10 occurrences)
(util.ellipses_string(value),),
galaxy.tools.actions.model_operations INFO 2019-07-31 09:38:48,512 [p:63775,w:2,m:0] [WorkflowRequestMonitor.monitor_thread] Calling produce_outputs, tool is <galaxy.tools.FilterFromFileTool object at 0x7f32a9f63810>
galaxy.tools.execute DEBUG 2019-07-31 09:38:48,516 [p:63775,w:2,m:0] [WorkflowRequestMonitor.monitor_thread] Tool [__FILTER_FROM_FILE__] created job [12] (106.710 ms)
galaxy.tools.execute DEBUG 2019-07-31 09:38:48,524 [p:63775,w:2,m:0] [WorkflowRequestMonitor.monitor_thread] Executed 1 job(s) for tool __FILTER_FROM_FILE__ request: (140.242 ms)
galaxy.workflow.run DEBUG 2019-07-31 09:38:48,533 [p:63775,w:2,m:0] [WorkflowRequestMonitor.monitor_thread] Workflow step 11 of invocation 4 invoked (164.122 ms)
galaxy.workflow.run ERROR 2019-07-31 09:38:48,553 [p:63775,w:2,m:0] [WorkflowRequestMonitor.monitor_thread] Failed to schedule Workflow[id=2,name=test merge empty (imported from uploaded file)], problem occurred on WorkflowStep[index=4,type=tool].
Traceback (most recent call last):
File "lib/galaxy/workflow/run.py", line 190, in invoke
incomplete_or_none = self._invoke_step(workflow_invocation_step)
File "lib/galaxy/workflow/run.py", line 266, in _invoke_step
use_cached_job=self.workflow_invocation.use_cached_job)
File "lib/galaxy/workflow/modules.py", line 1189, in execute
collection_info = self.compute_collection_info(progress, step, all_inputs)
File "lib/galaxy/workflow/modules.py", line 309, in compute_collection_info
progress, step, all_inputs
File "lib/galaxy/workflow/modules.py", line 327, in _find_collections_to_match
data = progress.replacement_for_input(step, input_dict)
File "lib/galaxy/workflow/run.py", line 337, in replacement_for_input
replacement = self.replacement_for_connection(connection[0], is_data=is_data)
File "lib/galaxy/workflow/run.py", line 358, in replacement_for_connection
raise Exception(message)
Exception: Workflow evaluation problem - failed to find output_name output in step_outputs {}
galaxy.workflow.run ERROR 2019-07-31 09:38:48,697 [p:63775,w:2,m:0] [WorkflowRequestMonitor.monitor_thread] Failed to execute scheduled workflow.
Traceback (most recent call last):
File "lib/galaxy/workflow/run.py", line 83, in __invoke
outputs = invoker.invoke()
File "lib/galaxy/workflow/run.py", line 190, in invoke
incomplete_or_none = self._invoke_step(workflow_invocation_step)
File "lib/galaxy/workflow/run.py", line 266, in _invoke_step
use_cached_job=self.workflow_invocation.use_cached_job)
File "lib/galaxy/workflow/modules.py", line 1189, in execute
collection_info = self.compute_collection_info(progress, step, all_inputs)
File "lib/galaxy/workflow/modules.py", line 309, in compute_collection_info
progress, step, all_inputs
File "lib/galaxy/workflow/modules.py", line 327, in _find_collections_to_match
data = progress.replacement_for_input(step, input_dict)
File "lib/galaxy/workflow/run.py", line 337, in replacement_for_input
replacement = self.replacement_for_connection(connection[0], is_data=is_data)
File "lib/galaxy/workflow/run.py", line 358, in replacement_for_connection
raise Exception(message)
Exception: Workflow evaluation problem - failed to find output_name output in step_outputs {}
galaxy.workflow.scheduling_manager DEBUG 2019-07-31 09:38:48,712 [p:63775,w:2,m:0] [WorkflowRequestMonitor.monitor_thread] Workflow invocation [4] scheduled

# Fixed code
def dataset_states_and_extensions_summary(self):
    if not hasattr(self, "_dataset_states_and_extensions_summary"):
        db_session = object_session(self)

        dc = alias(DatasetCollection.table)
        de = alias(DatasetCollectionElement.table)
        hda = alias(HistoryDatasetAssociation.table)
        dataset = alias(Dataset.table)

        select_from = dc.outerjoin(de, de.c.dataset_collection_id == dc.c.id)

        depth_collection_type = self.collection_type
        while ":" in depth_collection_type:
            child_collection = alias(DatasetCollection.table)
            child_collection_element = alias(DatasetCollectionElement.table)
            select_from = select_from.outerjoin(
                child_collection, child_collection.c.id == de.c.child_collection_id
            )
            select_from = select_from.outerjoin(
                child_collection_element,
                child_collection_element.c.dataset_collection_id
                == child_collection.c.id,
            )

            de = child_collection_element
            depth_collection_type = depth_collection_type.split(":", 1)[1]

        select_from = select_from.outerjoin(hda, hda.c.id == de.c.hda_id).outerjoin(
            dataset, hda.c.dataset_id == dataset.c.id
        )
        select_stmt = (
            select([hda.c.extension, dataset.c.state])
            .select_from(select_from)
            .where(dc.c.id == self.id)
            .distinct()
        )
        extensions = set()
        states = set()
        for extension, state in db_session.execute(select_stmt).fetchall():
            if state is not None:
                # query may return (None, None) if not collection elements present
                states.add(state)
                extensions.add(extension)

        self._dataset_states_and_extensions_summary = (states, extensions)

    return self._dataset_states_and_extensions_summary