Skip to content

fs: keep timestamps of files skipped by cpSync - #66305

Open
christianaurichzm wants to merge 1 commit into
nodejs:mainfrom
christianaurichzm:cpsync-skip-timestamps
Open

christianaurichzm wants to merge 1 commit into
nodejs:mainfrom
christianaurichzm:cpsync-skip-timestamps

Conversation

@christianaurichzm

Copy link
Copy Markdown
Contributor

With force: false and preserveTimestamps: true, cpSync() updates the timestamps of an existing destination file even though the file itself is skipped.

This only affects the C++ recursive copy path used by cpSync() without a filter. The JS paths used with a filter, fs.cp(), and fs.promises.cp() already leave skipped files untouched.

const fs = require('node:fs');

fs.mkdirSync('src');
fs.mkdirSync('dest');

fs.writeFileSync('src/f', 'new');
fs.utimesSync('src/f', 1000, 1000);

fs.writeFileSync('dest/f', 'old');
fs.utimesSync('dest/f', 2e9, 2e9);

fs.cpSync('src', 'dest', {
  recursive: true,
  force: false,
  preserveTimestamps: true,
});

console.log(
  fs.readFileSync('dest/f', 'utf8'),
  fs.statSync('dest/f').mtimeMs,
);

// old 1000000
// expected: old 2000000000000

In CopyDirRecursive(), copy_file() returns false when skip_existing causes the copy to be skipped. The mode restoration already checks that return value, but CopyUtimes() did not.

Only copy timestamps when the file was actually copied.

The test covers both the C++ path and the filter path. It fails on main and passes with this change.

make lint and test-fs-* pass on x64 Linux. v22.x is affected as well.

#65547 touches the same loop, but skipped files still leave copied as false, so this change applies cleanly on top of it.

With `force: false` and `preserveTimestamps: true`, the native
directory copy used by `cpSync()` left existing destination files in
place but still copied the source timestamps onto them. The JavaScript
walk, used by `cpSync()` with a `filter` and by `fs.cp()`, leaves
those files untouched.

Only copy the timestamps of files that were actually copied, as is
already done for their mode.

Signed-off-by: Christian Aurich Zanettini Martins <christian.aurichzm@gmail.com>
@nodejs-github-bot nodejs-github-bot added c++ Issues and PRs that require attention from people who are familiar with C++. fs Issues and PRs related to file-system APIs and the fs module. needs-ci PRs that need a full CI run. labels Sep 25, 2026
@codecov

codecov Bot commented Sep 26, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.39%. Comparing base (4bd56b3) to head (b1135e9).
⚠️ Report is 2 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #66305      +/-   ##
==========================================
+ Coverage   90.37%   90.39%   +0.01%     
==========================================
  Files         790      790              
  Lines      274279   274272       -7     
  Branches    52506    52512       +6     
==========================================
+ Hits       247870   247915      +45     
+ Misses      16890    16837      -53     
- Partials     9519     9520       +1     
Files with missing lines Coverage Δ
src/node_file.cc 75.21% <100.00%> (+0.02%) ⬆️

... and 35 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

c++ Issues and PRs that require attention from people who are familiar with C++. fs Issues and PRs related to file-system APIs and the fs module. needs-ci PRs that need a full CI run.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants