Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Write tmp files to the OS tmp directory #9580

Merged
merged 3 commits into from
Mar 14, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 35 additions & 1 deletion packages/core/fs/src/NodeFS.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import type {
import fs from 'graceful-fs';
import nativeFS from 'fs';
import ncp from 'ncp';
import path from 'path';
import {tmpdir} from 'os';
import {promisify} from 'util';
import {registerSerializableClass} from '@parcel/core';
import {hashFile} from '@parcel/utils';
Expand Down Expand Up @@ -222,11 +224,43 @@ try {
//
}

let useOsTmpDir;
function shouldUseOsTmpDir(filePath) {
if (useOsTmpDir != null) {
return useOsTmpDir;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess there's a tiny risk the /tmp directory becomes un-usable between the first check and the first write, but that seems pretty unlikely?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't even imagine how that would happen. I'm comfortable with a single check.

}
try {
const tmpDir = tmpdir();
nativeFS.accessSync(
tmpDir,
nativeFS.constants.R_OK | nativeFS.constants.W_OK,
);
const tmpDirStats = nativeFS.statSync(tmpDir);
const filePathStats = nativeFS.statSync(filePath);
// Check the tmpdir is on the same partition as the target directory.
// This is required to ensure renaming is an atomic operation.
useOsTmpDir = tmpDirStats.dev === filePathStats.dev;
} catch (e) {
// We don't have read/write access to the OS tmp directory
useOsTmpDir = false;
}
return useOsTmpDir;
}

// Generate a temporary file path used for atomic writing of files.
function getTempFilePath(filePath: FilePath) {
writeStreamCalls = writeStreamCalls % Number.MAX_SAFE_INTEGER;

let tmpFilePath = filePath;

// If possible, write the tmp file to the OS tmp directory
// This reduces the amount of FS events the watcher needs to process during the build
if (shouldUseOsTmpDir(filePath)) {
tmpFilePath = path.join(tmpdir(), path.basename(filePath));
}

return (
filePath +
tmpFilePath +
'.' +
process.pid +
(threadId != null ? '.' + threadId : '') +
Expand Down
Loading