Skip to content

Commit

Permalink
fix: issue #1882 WorkflowError: Metadata can't be created as it alrea…
Browse files Browse the repository at this point in the history
…dy exists (Windows) (#1971)

### Description

Fixes issue #1882: WorkflowError: Metadata can't be created as it
already exists (Windows)

The underlying cause of the problem is that `os.rename` was used to
overwrite the target file. Unfortunately, it has a different behavior
under Unix/Linux (overwrites target) and Windows (fails). `os.replace`
is an alternative to `os.rename` that simulates the Unix behavior.

The required change is extremely small (commit 4ad1ab1) and I have also
added a test case covering it (commit e12955f).

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

Co-authored-by: Johannes Köster <johannes.koester@tu-dortmund.de>
  • Loading branch information
stanmart and johanneskoester committed Dec 6, 2022
1 parent 3e6675d commit d4484e6
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 1 deletion.
2 changes: 1 addition & 1 deletion snakemake/persistence.py
Expand Up @@ -459,7 +459,7 @@ def _record(self, subject, json_value, id):
suffix=f".{os.path.basename(recpath)[:8]}",
) as tmpfile:
json.dump(json_value, tmpfile)
os.rename(tmpfile.name, recpath)
os.replace(tmpfile.name, recpath)

def _delete_record(self, subject, id):
try:
Expand Down
5 changes: 5 additions & 0 deletions tests/test_github_issue1882/Snakefile
@@ -0,0 +1,5 @@
rule all:
output:
"foo.txt"
run:
with open('foo.txt', 'w') as f: pass
Empty file.
8 changes: 8 additions & 0 deletions tests/tests.py
Expand Up @@ -2000,3 +2000,11 @@ def test_github_issue929():
# and pointing to the problem in the code!
# Huge thanks to Ronald Lehnigk for pointing me to the issue!
run(dpath("test_github_issue929"), targets=["childrule_2"])


def test_github_issue1882():
try:
tmpdir = run(dpath("test_github_issue1882"), cleanup=False)
run(tmpdir, forceall=True)
finally:
shutil.rmtree(tmpdir)

0 comments on commit d4484e6

Please sign in to comment.