Skip to content

in try_read_file_sync - treat ENOTDIR as ENOENT #9044

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

Merged
merged 1 commit into from
May 28, 2025
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
8 changes: 7 additions & 1 deletion src/util/fs_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,14 +142,19 @@ function try_read_file_sync(file_name) {
try {
return fs.readFileSync(file_name, 'utf8');
} catch (err) {
if (err.code === 'ENOENT') {
if (is_not_exist_err_code(err)) {
// file does not exist
return;
}
throw err;
}
}

// returns true if the error is ENOENT or ENOTDIR
// ENOTDIR is relevant for cases where a directory in the middle of the path is a file and not a directory
function is_not_exist_err_code(err) {
return err && (err.code === 'ENOENT' || err.code === 'ENOTDIR');
}

// returns the first line in the file that contains the substring
async function find_line_in_file(file_name, line_sub_string) {
Expand Down Expand Up @@ -373,3 +378,4 @@ exports.PRIVATE_DIR_PERMISSIONS = PRIVATE_DIR_PERMISSIONS;
exports.file_exists = file_exists;
exports.file_not_exists = file_not_exists;
exports.try_read_file_sync = try_read_file_sync;
exports.is_not_exist_err_code = is_not_exist_err_code;