Skip to content

Commit

Permalink
Fix compareLastModifiedTime for Node.js 14.17.0 (#83) (#84)
Browse files Browse the repository at this point in the history
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
  • Loading branch information
gavinaiken and sindresorhus authored Jun 15, 2021
1 parent 66192cc commit e5c66cd
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 2 deletions.
5 changes: 4 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ async function compareLastModifiedTime(stream, sourceFile, targetPath) {
// TODO: Use the `stat` `bigint` option when targeting Node.js 10 and Gulp supports it
const targetStat = await stat(targetPath);

if (sourceFile.stat && Math.floor(sourceFile.stat.mtimeMs) > Math.floor(targetStat.mtimeMs)) {
/*
Precision is lost in the `mtime` when Gulp copies the file from source to target so we cannot compare the modified times directly. This has been the case since Gulp 4. Now, due to an issue in libuv affecting Node.js 14.17.0 and above (including 16.x: https://github.com/nodejs/node/issues/38981) when Gulp copies the file to the target, its `mtime` may be behind the source file by up to 1ms. For example, if the source file has a `mtime` like `1623259049896.314`, the target file `mtime` can end up as `1623259049895.999`. So to compare safely we use floor on the source and ceil on the target, which would give us `1623259049896` for both source and target in that example case.
*/
if (sourceFile.stat && Math.floor(sourceFile.stat.mtimeMs) > Math.ceil(targetStat.mtimeMs)) {
stream.push(sourceFile);
}
}
Expand Down
2 changes: 1 addition & 1 deletion test.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const macro = async (t, options) => {

stream.on('data', file => {
files.push(file);
fs.writeFileSync(path.join(dest, file.relative), file);
fs.writeFileSync(path.join(dest, file.relative), file.contents);
});

stream.on('end', () => {
Expand Down

0 comments on commit e5c66cd

Please sign in to comment.