From f39b49d756110e24ac75c81413cb13e8a0a423b0 Mon Sep 17 00:00:00 2001 From: Andrew Casey Date: Thu, 17 Oct 2019 11:36:45 -0700 Subject: [PATCH] Update another writeFile call-site --- src/compiler/program.ts | 2 +- src/compiler/sys.ts | 2 +- src/compiler/watch.ts | 15 +++++++++++++-- 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/src/compiler/program.ts b/src/compiler/program.ts index 44cf374ea75..6a697b9c180 100644 --- a/src/compiler/program.ts +++ b/src/compiler/program.ts @@ -124,7 +124,7 @@ namespace ts { try { writeFileWorker(fileName, data, writeByteOrderMark); } - catch (_) { + catch { ensureDirectoriesExist(getDirectoryPath(normalizePath(fileName))); writeFileWorker(fileName, data, writeByteOrderMark); } diff --git a/src/compiler/sys.ts b/src/compiler/sys.ts index c85bb48bb9e..c4a5012b265 100644 --- a/src/compiler/sys.ts +++ b/src/compiler/sys.ts @@ -547,7 +547,7 @@ namespace ts { try { originalWriteFile.call(sys, path, data, writeBom); } - catch (_) { + catch { const directoryPath = getDirectoryPath(normalizeSlashes(path)); if (directoryPath && !sys.directoryExists(directoryPath)) { recursiveCreateDirectory(directoryPath, sys); diff --git a/src/compiler/watch.ts b/src/compiler/watch.ts index 06871fc9cac..f6e334aa47a 100644 --- a/src/compiler/watch.ts +++ b/src/compiler/watch.ts @@ -307,9 +307,20 @@ namespace ts { function writeFile(fileName: string, text: string, writeByteOrderMark: boolean, onError: (message: string) => void) { try { performance.mark("beforeIOWrite"); - ensureDirectoriesExist(getDirectoryPath(normalizePath(fileName))); - host.writeFile!(fileName, text, writeByteOrderMark); + // PERF: Checking for directory existence is expensive. + // Instead, assume the directory exists and fall back + // to creating it if the file write fails. + // NOTE: If patchWriteFileEnsuringDirectory has been called, + // the file write will do its own directory creation and + // the ensureDirectoriesExist call will always be redundant. + try { + host.writeFile!(fileName, text, writeByteOrderMark); + } + catch { + ensureDirectoriesExist(getDirectoryPath(normalizePath(fileName))); + host.writeFile!(fileName, text, writeByteOrderMark); + } performance.mark("afterIOWrite"); performance.measure("I/O Write", "beforeIOWrite", "afterIOWrite");