Skip to content

feat: keep temp files in case they are an input of target rules that that don't have their own output#3536

Merged
johanneskoester merged 3 commits intomainfrom
feat/keep-temp-target
Apr 16, 2025
Merged

feat: keep temp files in case they are an input of target rules that that don't have their own output#3536
johanneskoester merged 3 commits intomainfrom
feat/keep-temp-target

Conversation

@johanneskoester
Copy link
Copy Markdown
Contributor

@johanneskoester johanneskoester commented Apr 16, 2025

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

  • New Features
    • Improved handling of temporary files to prevent premature removal when they are needed as final outputs.
  • Tests
    • Added a new workflow and corresponding test to verify correct behavior when temporary files are also specified as final targets.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Apr 16, 2025

📝 Walkthrough
## Walkthrough

The changes introduce enhancements to the DAG class in the Snakemake workflow engine, specifically by tracking derived target files to refine the logic for determining whether temporary files are still needed. The `derived_targetfiles` attribute is added and populated during DAG initialization. The logic in `is_needed_tempfile` is updated to consider these derived target files, preventing their premature removal. Additionally, a new test workflow and corresponding test case are added to verify the correct handling of temporary files that are also final targets.

## Changes

| File(s)                                   | Change Summary                                                                                                 |
|-------------------------------------------|----------------------------------------------------------------------------------------------------------------|
| src/snakemake/dag.py                      | Added `derived_targetfiles` attribute to the DAG class; updated initialization and tempfile necessity logic.    |
| tests/test_temp_and_all_input/Snakefile   | Introduced a workflow with a temp output that is also a final target, using two rules: `all` and `a`.           |
| tests/tests.py                            | Added a test function `test_temp_and_all_input` to run the new workflow and verify tempfile handling.           |

## Sequence Diagram(s)

```mermaid
sequenceDiagram
    participant User
    participant Snakemake
    participant DAG
    participant Job

    User->>Snakemake: Run workflow
    Snakemake->>DAG: Initialize DAG (init)
    DAG->>DAG: Set derived_targetfiles
    loop For each job
        Snakemake->>DAG: Check if tempfile is needed (is_needed_tempfile)
        alt Tempfile is needed by downstream job or in derived_targetfiles
            DAG-->>Snakemake: Return True
        else
            DAG-->>Snakemake: Return False
        end
    end
    Snakemake->>Job: Execute jobs as per workflow
    Snakemake->>DAG: Remove temp files if not needed

</details>

<!-- walkthrough_end -->


---

<details>
<summary>📜 Recent review details</summary>

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

<details>
<summary>📥 Commits</summary>

Reviewing files that changed from the base of the PR and between 954d6a705439dd7468503db2e4d452b36bf2b4b1 and 5bfe4cc363f987870707213e13a5aaa3d6b574c7.

</details>

<details>
<summary>📒 Files selected for processing (1)</summary>

* `src/snakemake/dag.py` (3 hunks)

</details>

<details>
<summary>🚧 Files skipped from review as they are similar to previous changes (1)</summary>

* src/snakemake/dag.py

</details>

<details>
<summary>⏰ Context from checks skipped due to timeout of 90000ms (2)</summary>

* GitHub Check: tests (10, windows-latest, py312)
* GitHub Check: tests (1, macos-latest, py312)

</details>

</details>
<!-- internal state start -->


<!-- DwQgtGAEAqAWCWBnSTIEMB26CuAXA9mAOYCmGJATmriQCaQDG+Ats2bgFyQAOFk+AIwBWJBrngA3EsgEBPRvlqU0AgfFwA6NPEgQAfACgjoCEYDEZyAAUASpETZWaCrKNwSPbABsvkCiQBHbGlcSHFcLzpIACIAMxJqLgBrEhJuMJJmdNj4SOR4LAY0RA9cWBJ5Zw9MFAxuPH5YsOdSUIpvaTDYakhafAwAclDuqS6SeD58AHcsfDx63GjIKeL7bAFmdRp6OTHIbBK+IXxujHJEJPwQynRkW0gMRwEbgGYAVheANn4sMo8AZQwaBSzGBHgA4uoABLrPxpfCIdT4FwaGDlRinUi1XAURTYBidNCQeLUbD+OFeahRAh7U60LwFIiNDJZZHOeQ5PLLdQILCAsGglLLZFJWJeaaIVH/biieA5Io+WQAGhQoTIDn8yDKPRorKoLmJuUJ5P8uG05HocrG8kOo1WBQWWvwzQorT8HS13VCf2YJS8UmQfQe+FCvDxBPQGHkc1wjtRcFQaFoQgOuDYGFCSdompKyEQBKBFHgCOW5XJuu4bINnM6myIs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NwagTLAOsdvnpzjDo8AgbhIgfA/o13yErkFsoB8Y7oAwlhfow6gtUDWZQD8fBemUDtaAqotRGETRVVmLLWB+FxcpABHNsUzKhCftZTA8g3NuPUz/HK/A+CaJxPF4kFKD0SePTE/oET0RKAvVN0Bz0b1JKiCk9Yj7Rz+SU1AAAYjZ0rGWJpcFKV+SDbHQWRchbgK1jHwN/JE7LMT/GKbQ0yU9JoiNXorpFl9dgXAqA41kzpAm/sVDEwhmTQwr2vqm2SogA2/gkru2vheZwoRboGFewATjeAAFloJ8NAAB2AADHjl4WOMJE9x58AAHG8EnLxaACAAEwkHx7jt4LOBBfAELEbnuOBAAEZIeY/upAN4fP2eMS+C8WIWPadE8VyT0nROWdC5eCQDXaA3hoD10zz4Ag3jU4YET0XYuIAKFYLDmhCOSBI6mNfR6kOgA=== -->

<!-- internal state end -->
<!-- tips_start -->

---



<details>
<summary>🪧 Tips</summary>

### Chat

There are 3 ways to chat with [CodeRabbit](https://coderabbit.ai?utm_source=oss&utm_medium=github&utm_campaign=snakemake/snakemake&utm_content=3536):

- Review comments: Directly reply to a review comment made by CodeRabbit. Example:
  - `I pushed a fix in commit <commit_id>, please review it.`
  - `Generate unit testing code for this file.`
  - `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 generate unit testing code for this file.`
  -	`@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 generate unit testing code.`
  - `@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.`

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](https://docs.coderabbit.ai/finishing-touches/docstrings) for 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](https://docs.coderabbit.ai/guides/configure-coderabbit) 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](https://docs.coderabbit.ai) for detailed information on how to use CodeRabbit.
- Join our [Discord Community](http://discord.gg/coderabbit) to get help, request features, and share feedback.
- Follow us on [X/Twitter](https://twitter.com/coderabbitai) for updates and announcements.

</details>

<!-- tips_end -->

Copy link
Copy Markdown
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: 1

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between a2333a8 and 954d6a7.

📒 Files selected for processing (3)
  • src/snakemake/dag.py (3 hunks)
  • tests/test_temp_and_all_input/Snakefile (1 hunks)
  • tests/tests.py (1 hunks)
🧰 Additional context used
📓 Path-based instructions (1)
`**/*.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 `s...

**/*.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.

  • tests/tests.py
  • src/snakemake/dag.py
🧬 Code Graph Analysis (1)
tests/tests.py (1)
tests/common.py (2)
  • run (151-495)
  • dpath (32-35)
⏰ Context from checks skipped due to timeout of 90000ms (36)
  • GitHub Check: tests (10, macos-latest, py312)
  • GitHub Check: tests (10, windows-latest, py312)
  • GitHub Check: tests (10, ubuntu-latest, py312)
  • GitHub Check: tests (10, ubuntu-latest, py311)
  • GitHub Check: tests (9, windows-latest, py312)
  • GitHub Check: tests (9, ubuntu-latest, py312)
  • GitHub Check: tests (9, ubuntu-latest, py311)
  • GitHub Check: tests (8, windows-latest, py312)
  • GitHub Check: tests (8, ubuntu-latest, py312)
  • GitHub Check: tests (8, ubuntu-latest, py311)
  • GitHub Check: tests (7, windows-latest, py312)
  • GitHub Check: tests (7, ubuntu-latest, py312)
  • GitHub Check: tests (7, ubuntu-latest, py311)
  • GitHub Check: tests (6, windows-latest, py312)
  • GitHub Check: tests (6, ubuntu-latest, py312)
  • GitHub Check: tests (6, ubuntu-latest, py311)
  • GitHub Check: tests (5, windows-latest, py312)
  • GitHub Check: tests (5, ubuntu-latest, py312)
  • GitHub Check: tests (5, ubuntu-latest, py311)
  • GitHub Check: tests (4, macos-latest, py312)
  • GitHub Check: tests (4, windows-latest, py312)
  • GitHub Check: tests (4, ubuntu-latest, py312)
  • GitHub Check: tests (4, ubuntu-latest, py311)
  • GitHub Check: tests (3, macos-latest, py312)
  • GitHub Check: tests (3, windows-latest, py312)
  • GitHub Check: tests (3, ubuntu-latest, py312)
  • GitHub Check: tests (3, ubuntu-latest, py311)
  • GitHub Check: tests (2, macos-latest, py312)
  • GitHub Check: tests (2, windows-latest, py312)
  • GitHub Check: tests (2, ubuntu-latest, py312)
  • GitHub Check: tests (2, ubuntu-latest, py311)
  • GitHub Check: tests (1, macos-latest, py312)
  • GitHub Check: tests (1, windows-latest, py312)
  • GitHub Check: tests (1, ubuntu-latest, py312)
  • GitHub Check: apidocs
  • GitHub Check: tests (1, ubuntu-latest, py311)
🔇 Additional comments (5)
src/snakemake/dag.py (2)

141-141: Added new attribute to track derived target files.

This new attribute helps manage temporary files that are inputs to rules without outputs.


917-924: Improved logic to prevent premature removal of temporary files.

The feature now correctly preserves temporary files that are inputs to target rules without outputs by checking if the tempfile is included in the derived_targetfiles set.

tests/tests.py (1)

2333-2335: LGTM! This test verifies the new feature for handling temp files.

This test function validates the behavior mentioned in the PR objectives: ensuring that temporary files are retained when they serve as inputs to target rules without their own outputs. The test runs a simple workflow defined in the "test_temp_and_all_input" directory.

tests/test_temp_and_all_input/Snakefile (2)

1-4: LGTM! Appropriate target rule for testing temp file handling.

This rule correctly defines a target with no outputs that depends on a temporary file. It's the perfect setup to test the feature mentioned in the PR: keeping temporary files when they are inputs to target rules without outputs.


5-9: LGTM! Well-defined rule with a temporary output.

This rule appropriately uses the temp() flag to mark "foo.txt" as a temporary file. The simple shell command is sufficient for creating the test file. This configuration is crucial for testing the new feature that prevents the premature removal of temporary files when they serve as inputs to target rules without outputs.

@johanneskoester johanneskoester merged commit a2caa0b into main Apr 16, 2025
49 checks passed
@johanneskoester johanneskoester deleted the feat/keep-temp-target branch April 16, 2025 09:57
johanneskoester pushed a commit that referenced this pull request Apr 16, 2025
🤖 I have created a release *beep* *boop*
---


##
[9.3.0](v9.2.1...v9.3.0)
(2025-04-16)


### Features

* keep temp files in case they are an input of target rules that that
don't have their own output
([#3536](#3536))
([a2caa0b](a2caa0b))
* make shutil available in resource expressions (i.e. for
--default-resources and --set-resources), can be used to e.g.
dynamically decide about the tmpdir to be used based on
shutil.disk_usage(system_tmpdir)
([#3535](#3535))
([aeab67f](aeab67f))

---
This PR was generated with [Release
Please](https://github.com/googleapis/release-please). See
[documentation](https://github.com/googleapis/release-please#release-please).
kjohnsen pushed a commit to kjohnsen/snakemake that referenced this pull request Dec 15, 2025
…that don't have their own output (snakemake#3536)

<!--Add a description of your PR here-->

### QC
<!-- Make sure that you can tick the boxes below. -->

* [x] The PR contains a test case for the changes or the changes are
already covered by an existing test case.
* [x] 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).


<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

- **New Features**
- Improved handling of temporary files to prevent premature removal when
they are needed as final outputs.
- **Tests**
- Added a new workflow and corresponding test to verify correct behavior
when temporary files are also specified as final targets.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
kjohnsen pushed a commit to kjohnsen/snakemake that referenced this pull request Dec 15, 2025
🤖 I have created a release *beep* *boop*
---


##
[9.3.0](snakemake/snakemake@v9.2.1...v9.3.0)
(2025-04-16)


### Features

* keep temp files in case they are an input of target rules that that
don't have their own output
([snakemake#3536](snakemake#3536))
([a2caa0b](snakemake@a2caa0b))
* make shutil available in resource expressions (i.e. for
--default-resources and --set-resources), can be used to e.g.
dynamically decide about the tmpdir to be used based on
shutil.disk_usage(system_tmpdir)
([snakemake#3535](snakemake#3535))
([aeab67f](snakemake@aeab67f))

---
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.

1 participant