Skip to content

ScratchDir("test_dir", copy_to_current_on_exit=True) would wipe current working directory #760

@DanielYang59

Description

@DanielYang59

@Andrew-S-Rosen Maybe I could hear your comment on this behaviour?

ScratchDir with copy_to_current_on_exit and delete_removed_files (True by default now) seems to completely replace current working directory with content within the temp dir, which looks very suprising and dangerous to me.

As a user, I would expect the behaviour of ScratchDir("test_dir", copy_to_current_on_exit=True) to (for example): run a VASP job within a temp dir, and copy (add/append) generated output files to current working directory. However the currently behaviour is: everything in CWD would be replaced by the newly generated files.

At the end of the day, ScratchDir should provide a safe sandbox to run jobs instead of silently wiping CWD?

Also in this case, I didn't really "remove any file" from the temp dir?

Whether to delete files in the cwd that are removed from the tmp dir.

https://github.com/materialsvirtuallab/monty/blob/0dd2588667e4bfa0a8436b282b9e1f371ad5abc6/src/monty/tempfile.py#L75-L84

https://github.com/materialsvirtuallab/monty/blob/0dd2588667e4bfa0a8436b282b9e1f371ad5abc6/src/monty/tempfile.py#L122-L126


Recreate

Don't run the following script it would wipe your current working directory!
Don't run the following script it would wipe your current working directory!
Don't run the following script it would wipe your current working directory!

(temp) ➜  temp tree .
.
├── test_dir
│   └── world
└── test_monty.py

2 directories, 2 files

(temp) ➜  temp cat test_monty.py 
from monty.tempfile import ScratchDir

with ScratchDir("test_dir", copy_to_current_on_exit=True) as d:
    with open(f"{d}/hello", mode="w") as f:
        f.write("hello")

(temp) ➜  temp uv run test_monty.py 
Traceback (most recent call last):
  File "/Users/yang/developer/temp/test_monty.py", line 3, in <module>
  File "/Users/yang/developer/temp/.venv/lib/python3.12/site-packages/monty/tempfile.py", line 129, in __exit__
    return prefix, suffix, dir, output_type
            ^^^^^^^^^^^^^^^^^^^^
  File "/Users/yang/developer/temp/.venv/lib/python3.12/site-packages/monty/shutil.py", line 203, in remove
    while buf := fsrc_read(length):
        ^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/Cellar/python@3.12/3.12.11/Frameworks/Python.framework/Versions/3.12/lib/python3.12/shutil.py", line 759, in rmtree
    _rmtree_safe_fd(stack, onexc)
  File "/opt/homebrew/Cellar/python@3.12/3.12.11/Frameworks/Python.framework/Versions/3.12/lib/python3.12/shutil.py", line 703, in _rmtree_safe_fd
    onexc(func, path, err)
  File "/opt/homebrew/Cellar/python@3.12/3.12.11/Frameworks/Python.framework/Versions/3.12/lib/python3.12/shutil.py", line 669, in _rmtree_safe_fd
    orig_st = os.lstat(name, dir_fd=dirfd)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
FileNotFoundError: [Errno 2] No such file or directory: PosixPath('/Users/yang/developer/temp/test_dir/tmp4iabgoy6')

(temp) ➜  temp ls
hello

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions