Skip to content

Conversation

b-brankovics
Copy link
Contributor

@b-brankovics b-brankovics commented Jul 10, 2025

fixes #3556

I noticed that DAG._dot() renders the edges correctly, but DAG._mermaid_js() makes a mess out of it.

  • I copied the bit from the _dot part to the _mermaid_js part
  • modified the f"" stretch to use the correct numbers

This fixed the mermaid output so it matches the dot one.

QC

  • The PR contains a test case for the changes or the changes are already covered by an existing test case.
  • The documentation (docs/) is updated to reflect the changes or this is not necessary (e.g. if the change does neither modify the language nor the behavior or functionalities of Snakemake).

Summary by CodeRabbit

  • Bug Fixes
    • Corrected the direction of edges in the Mermaid.js graph representation, ensuring dependencies are accurately visualized.
  • New Features
    • Added a new test workflow to validate complex dependency behaviors.
    • Introduced a visual DAG representation for the new test workflow.
    • Added a test case to verify the updated DAG generation with Mermaid.js output.

I noticed that `DAG._dot()` renders the edges correctly, but
`DAG._mermaid_js()` makes a mess out of it.

- I copied the bit from the _dot part to the _mermaid_js part
- modified the f"" stretch to use the correct numbers

This fixed the mermaid output so it matches the dor one.
Copy link
Contributor

coderabbitai bot commented Jul 10, 2025

📝 Walkthrough

Walkthrough

The internal logic of the _mermaid_js method in the DAG class was updated to alter how edges are generated in the Mermaid.js graph representation. The change reverses the direction of the edges to correctly display dependencies, using a node-to-index mapping for reference.

Additionally, a new test workflow and corresponding Mermaid diagram were added to verify the correct behavior related to GitHub issues #3556 and #946. A new test function was introduced to run Snakemake with the Mermaid.js DAG output for this workflow.

Changes

File Change Summary
src/snakemake/dag.py Modified _mermaid_js method to reverse edge direction in Mermaid.js graph using node indices
tests/test_github_issue3556/Snakefile Added new Snakemake workflow with rules A, B, C, D, and all to test issues #3556 and #946
tests/test_github_issue3556/expected-results/dag.mmd Added Mermaid.js DAG diagram representing the workflow dependencies
tests/tests.py Added test_github_issue3556 function to run Snakemake with --dag mermaid-js on the new test workflow

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant DAG
    participant MermaidJS

    User->>DAG: Request Mermaid.js graph
    DAG->>DAG: Map nodes to indices (ids)
    DAG->>DAG: Iterate over nodes and dependencies
    DAG->>MermaidJS: Generate edges as dep --> node using ids
    DAG->>User: Return Mermaid.js graph string
Loading

Possibly related PRs

Suggested reviewers

  • johanneskoester

📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between bdc4fb8 and 9c0bf69.

📒 Files selected for processing (3)
  • tests/test_github_issue3556/Snakefile (1 hunks)
  • tests/test_github_issue3556/expected-results/dag.mmd (1 hunks)
  • tests/tests.py (1 hunks)
✅ Files skipped from review due to trivial changes (1)
  • tests/test_github_issue3556/expected-results/dag.mmd
🧰 Additional context used
📓 Path-based instructions (1)
**/*.py

Instructions used from:

Sources:
⚙️ CodeRabbit Configuration File

🧠 Learnings (3)
📓 Common learnings
Learnt from: leoschwarz
PR: snakemake/snakemake#3176
File: tests/test_output_index.py:99-157
Timestamp: 2025-01-17T12:00:09.368Z
Learning: New test dependencies for Snakemake should be introduced in separate PRs rather than being added as part of feature or refactoring PRs.
Learnt from: johanneskoester
PR: snakemake/snakemake#3148
File: snakemake/dag.py:1332-1336
Timestamp: 2024-11-12T12:08:20.342Z
Learning: In `snakemake/dag.py`, when code is outdated and will disappear upon resolving merge conflicts, avoid making code review suggestions on that code.
Learnt from: johanneskoester
PR: snakemake/snakemake#3140
File: snakemake/dag.py:1308-1308
Timestamp: 2024-10-14T09:42:11.571Z
Learning: In `snakemake/dag.py`, avoid flagging missing lines or indentation issues when there is no clear syntax or logical error to prevent false positives.
tests/tests.py (8)
Learnt from: johanneskoester
PR: snakemake/snakemake#3117
File: tests/test_wrapper/Snakefile:11-11
Timestamp: 2024-10-06T14:09:54.370Z
Learning: Changes made within test cases, such as in `tests/test_wrapper/Snakefile`, are for testing purposes and do not require updates to the project documentation.
Learnt from: leoschwarz
PR: snakemake/snakemake#3176
File: tests/test_output_index.py:99-157
Timestamp: 2025-01-17T12:00:09.368Z
Learning: New test dependencies for Snakemake should be introduced in separate PRs rather than being added as part of feature or refactoring PRs.
Learnt from: johanneskoester
PR: snakemake/snakemake#2925
File: tests/test_nonstr_params/Snakefile:14-15
Timestamp: 2024-12-21T15:10:31.992Z
Learning: The file "test.jsonl" in tests/test_nonstr_params is automatically created by Snakemake, rather than manually generated in the Snakefile.
Learnt from: johanneskoester
PR: snakemake/snakemake#2985
File: tests/tests.py:2051-2051
Timestamp: 2024-10-08T17:41:54.542Z
Learning: In the Snakemake repository, avoid suggesting return type annotations for test functions.
Learnt from: johanneskoester
PR: snakemake/snakemake#2985
File: tests/tests.py:2051-2051
Timestamp: 2024-08-13T09:25:24.046Z
Learning: In the Snakemake repository, avoid suggesting return type annotations for test functions.
Learnt from: johanneskoester
PR: snakemake/snakemake#3204
File: tests/test_cores_cluster/qsub:1-6
Timestamp: 2024-11-12T20:22:54.184Z
Learning: In the Snakemake codebase, the `tests/test_cores_cluster/qsub` script is a dummy script for testing, and input validation and error handling are not required in such scripts.
Learnt from: johanneskoester
PR: snakemake/snakemake#3140
File: snakemake/dag.py:1308-1308
Timestamp: 2024-10-14T09:42:11.571Z
Learning: In `snakemake/dag.py`, avoid flagging missing lines or indentation issues when there is no clear syntax or logical error to prevent false positives.
Learnt from: mbhall88
PR: snakemake/snakemake#3188
File: tests/test_script/scripts/test.sh:4-4
Timestamp: 2024-11-07T00:32:44.137Z
Learning: In test scripts within the Snakemake project, concise code is preferred over verbose error handling when simplicity suffices.
tests/test_github_issue3556/Snakefile (10)
Learnt from: leoschwarz
PR: snakemake/snakemake#3176
File: tests/test_output_index.py:99-157
Timestamp: 2025-01-17T12:00:09.368Z
Learning: New test dependencies for Snakemake should be introduced in separate PRs rather than being added as part of feature or refactoring PRs.
Learnt from: johanneskoester
PR: snakemake/snakemake#3117
File: tests/test_wrapper/Snakefile:11-11
Timestamp: 2024-10-06T14:09:54.370Z
Learning: Changes made within test cases, such as in `tests/test_wrapper/Snakefile`, are for testing purposes and do not require updates to the project documentation.
Learnt from: johanneskoester
PR: snakemake/snakemake#2925
File: tests/test_nonstr_params/Snakefile:14-15
Timestamp: 2024-12-21T15:10:31.992Z
Learning: The file "test.jsonl" in tests/test_nonstr_params is automatically created by Snakemake, rather than manually generated in the Snakefile.
Learnt from: mbhall88
PR: snakemake/snakemake#3188
File: tests/test_script/scripts/test.sh:4-4
Timestamp: 2024-11-07T00:32:44.137Z
Learning: In test scripts within the Snakemake project, concise code is preferred over verbose error handling when simplicity suffices.
Learnt from: johanneskoester
PR: snakemake/snakemake#3204
File: tests/test_cores_cluster/qsub:1-6
Timestamp: 2024-11-12T20:22:54.184Z
Learning: In the Snakemake codebase, the `tests/test_cores_cluster/qsub` script is a dummy script for testing, and input validation and error handling are not required in such scripts.
Learnt from: johanneskoester
PR: snakemake/snakemake#2927
File: tests/test_subpath/Snakefile:11-11
Timestamp: 2024-12-14T13:10:48.450Z
Learning: In Snakemake, when a rule defines a single output file (even if the output is specified as a dictionary), `{output}` in the shell command can be used directly to refer to that file.
Learnt from: johanneskoester
PR: snakemake/snakemake#2985
File: tests/tests.py:2051-2051
Timestamp: 2024-10-08T17:41:54.542Z
Learning: In the Snakemake repository, avoid suggesting return type annotations for test functions.
Learnt from: johanneskoester
PR: snakemake/snakemake#2985
File: tests/tests.py:2051-2051
Timestamp: 2024-08-13T09:25:24.046Z
Learning: In the Snakemake repository, avoid suggesting return type annotations for test functions.
Learnt from: johanneskoester
PR: snakemake/snakemake#2927
File: snakemake/ioutils/branch.py:44-51
Timestamp: 2024-12-14T13:13:09.273Z
Learning: Snakemake needs to maintain compatibility with Python 3.7; avoid using syntax or features introduced after Python 3.7.
Learnt from: johanneskoester
PR: snakemake/snakemake#3135
File: snakemake/profiles.py:54-55
Timestamp: 2024-10-13T14:29:38.902Z
Learning: In the Snakemake test suite, tests for configuration keys like 'group-components' may already be covered within existing tests for 'groups'. Ensure to verify this before suggesting additional tests.
🧬 Code Graph Analysis (1)
tests/tests.py (1)
tests/common.py (2)
  • run (151-495)
  • dpath (32-35)
🔇 Additional comments (2)
tests/tests.py (1)

2444-2445: LGTM! Test implementation follows established patterns.

The test function correctly uses the run helper with shell command execution to test the DAG Mermaid.js output generation. This will effectively verify that the fix for GitHub issue #3556 produces the expected Mermaid diagram format.

tests/test_github_issue3556/Snakefile (1)

1-40: Well-designed test workflow for DAG edge rendering.

The Snakefile creates an effective test case for verifying DAG edge direction fixes:

The workflow structure appropriately tests the edge cases that would reveal problems with Mermaid.js DAG visualization.

✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (1)
src/snakemake/dag.py (1)

2668-2674: Avoid triple enumeration of graph for index consistency & micro-perf
graph is enumerated three times (headers, styles, ids). Any mutation of the dict between calls would desynchronise indices. A single pass keeps things deterministic and shaves a bit of work:

-        nodes_headers = [
-            f"\tid{index}[{node2label(node)}]" for index, node in enumerate(graph)
-        ]
-        nodes_styles = [
-            f"\tstyle id{index} fill:{rulecolor[str(node)]},stroke-width:2px,color:#333333{node2style(node)}"
-            for index, node in enumerate(graph)
-        ]
-        # node ids
-        ids = {node: i for i, node in enumerate(graph)}
+        nodes = list(graph.keys())
+        ids = {node: i for i, node in enumerate(nodes)}
+        nodes_headers = [f"\tid{ids[node]}[{node2label(node)}]" for node in nodes]
+        nodes_styles = [
+            f"\tstyle id{ids[node]} "
+            f"fill:{rulecolor[str(node)]},stroke-width:2px,color:#333333{node2style(node)}"
+            for node in nodes
+        ]

Keeps all index look-ups consistent and makes intent clearer.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 7f2606e and 5c88873.

📒 Files selected for processing (1)
  • src/snakemake/dag.py (1 hunks)
🧰 Additional context used
📓 Path-based instructions (1)
`**/*.py`: Do not try to improve formatting. Do not suggest type annotations for...

**/*.py: Do not try to improve formatting.
Do not suggest type annotations for functions that are defined inside of functions or methods.
Do not suggest type annotation of the self argument of methods.
Do not suggest type annotation of the cls argument of classmethods.
Do not suggest return type annotation if a function or method does not contain a return statement.

⚙️ Source: CodeRabbit Configuration File

List of files the instruction was applied to:

  • src/snakemake/dag.py
🧠 Learnings (2)
📓 Common learnings
Learnt from: johanneskoester
PR: snakemake/snakemake#3148
File: snakemake/dag.py:1332-1336
Timestamp: 2024-11-12T12:08:20.342Z
Learning: In `snakemake/dag.py`, when code is outdated and will disappear upon resolving merge conflicts, avoid making code review suggestions on that code.
Learnt from: johanneskoester
PR: snakemake/snakemake#3140
File: snakemake/dag.py:1308-1308
Timestamp: 2024-10-14T09:42:11.571Z
Learning: In `snakemake/dag.py`, avoid flagging missing lines or indentation issues when there is no clear syntax or logical error to prevent false positives.
Learnt from: leoschwarz
PR: snakemake/snakemake#3176
File: tests/test_output_index.py:99-157
Timestamp: 2025-01-17T12:00:09.368Z
Learning: New test dependencies for Snakemake should be introduced in separate PRs rather than being added as part of feature or refactoring PRs.
src/snakemake/dag.py (2)

<retrieved_learning>
Learnt from: johanneskoester
PR: #3148
File: snakemake/dag.py:1332-1336
Timestamp: 2024-11-12T12:08:20.342Z
Learning: In snakemake/dag.py, when code is outdated and will disappear upon resolving merge conflicts, avoid making code review suggestions on that code.
</retrieved_learning>

<retrieved_learning>
Learnt from: johanneskoester
PR: #3140
File: snakemake/dag.py:1308-1308
Timestamp: 2024-10-14T09:42:11.571Z
Learning: In snakemake/dag.py, avoid flagging missing lines or indentation issues when there is no clear syntax or logical error to prevent false positives.
</retrieved_learning>

🔇 Additional comments (1)
src/snakemake/dag.py (1)

2671-2673: Edge direction now matches DOT output – good fix
Changing the arrow to id{ids[dep]} --> id{ids[node]} aligns Mermaid with the DOT representation and renders dependencies in the expected upstream → downstream direction.

@b-brankovics b-brankovics changed the title Fix:dag mermaid-js edges (bug:#3556) fix: dag mermaid-js edges (bug:#3556) Jul 10, 2025
@b-brankovics
Copy link
Contributor Author

I have managed to get a test that passes with the fix and fails without the fix. I don't know how to integrate it using PRs, but here is the link to my commit: b-brankovics/snakemake@e715f3e

@johanneskoester
Copy link
Contributor

@b-brankovics just commit your test to the same branch as that of this PR. Then, it will automatically appear here.

@b-brankovics
Copy link
Contributor Author

Thank you @johanneskoester! Merged the test commit and updated the PR to show that there is a test included.

@johanneskoester johanneskoester enabled auto-merge (squash) July 28, 2025 09:01
@johanneskoester johanneskoester merged commit c6e2e71 into snakemake:main Jul 28, 2025
53 checks passed
@b-brankovics b-brankovics deleted the fix/dag_mermaid_js_edges branch July 28, 2025 09:39
johanneskoester pushed a commit that referenced this pull request Jul 29, 2025
🤖 I have created a release *beep* *boop*
---


##
[9.9.0](v9.8.2...v9.9.0)
(2025-07-29)


### Features

* python 3.13 support
([#3602](#3602))
([429cff4](429cff4))


### Bug Fixes

* dag mermaid-js edges
(bug:[#3556](#3556))
([#3669](#3669))
([c6e2e71](c6e2e71))
* if asset is present and no sha256 provided assume pass
([#3685](#3685))
([7731175](7731175))

---
This PR was generated with [Release
Please](https://github.com/googleapis/release-please). See
[documentation](https://github.com/googleapis/release-please#release-please).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Mermaid-js output flow connections are incorrect
2 participants