Skip to content

fs.copyFile creates empty file if source and destination are the same #34624

@spikeburton

Description

@spikeburton
  • Version: 12.18.3
  • Platform: MacOS (Darwin Kernel Version 19.6.0)
  • Subsystem: fs

What steps will reproduce the bug?

  1. Create a new file: echo "Hello World" >> test.txt
  2. Run the following snippet:
    const fs = require('fs');
    fs.copyFileSync('test.txt', 'test.txt');
  3. Observe output: cat test.txt

How often does it reproduce? Is there a required condition?

The behavior here appears to be consistent.

What is the expected behavior?

I expect to either receive an error that this action should not be performed, or that the file ends up in the same state as it was initially.

What do you see instead?

The file has been overwritten and is now empty. All data has been lost.

Additional information

This bug could potentially cause data loss in certain situations.

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