Skip to content

Commit c42c9ac

Browse files
authored
Merge pull request #9044 from dannyzaken/danny-fixes
in try_read_file_sync - treat ENOTDIR as ENOENT
2 parents 9dd0dfd + ff22da2 commit c42c9ac

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

src/util/fs_utils.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,14 +142,19 @@ function try_read_file_sync(file_name) {
142142
try {
143143
return fs.readFileSync(file_name, 'utf8');
144144
} catch (err) {
145-
if (err.code === 'ENOENT') {
145+
if (is_not_exist_err_code(err)) {
146146
// file does not exist
147147
return;
148148
}
149149
throw err;
150150
}
151151
}
152152

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

154159
// returns the first line in the file that contains the substring
155160
async function find_line_in_file(file_name, line_sub_string) {
@@ -373,3 +378,4 @@ exports.PRIVATE_DIR_PERMISSIONS = PRIVATE_DIR_PERMISSIONS;
373378
exports.file_exists = file_exists;
374379
exports.file_not_exists = file_not_exists;
375380
exports.try_read_file_sync = try_read_file_sync;
381+
exports.is_not_exist_err_code = is_not_exist_err_code;

0 commit comments

Comments
 (0)