From 9949b2958794a9267514a7cbd1e3f02da972d905 Mon Sep 17 00:00:00 2001 From: Snjezana Peco Date: Thu, 28 Oct 2021 20:51:44 +0200 Subject: [PATCH] Creating the formatter file fails silently if parent folder doesn't exist Signed-off-by: Snjezana Peco --- src/extension.ts | 25 ++++++++++++------------- src/utils.ts | 6 ++++-- 2 files changed, 16 insertions(+), 15 deletions(-) diff --git a/src/extension.ts b/src/extension.ts index 1b21371c90..d9d35c6eb8 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -13,7 +13,7 @@ import * as requirements from './requirements'; import { initialize as initializeRecommendation } from './recommendation'; import { Commands } from './commands'; import { ExtensionAPI, ClientStatus } from './extension.api'; -import { getJavaConfiguration, deleteDirectory, getBuildFilePatterns, getInclusionPatternsFromNegatedExclusion, convertToGlob, getExclusionBlob } from './utils'; +import { getJavaConfiguration, deleteDirectory, getBuildFilePatterns, getInclusionPatternsFromNegatedExclusion, convertToGlob, getExclusionBlob, ensureExists } from './utils'; import { onConfigurationChange, getJavaServerMode, ServerMode, ACTIVE_BUILD_TOOL_STATE } from './settings'; import { logger, initializeLogFile } from './log'; import glob = require('glob'); @@ -610,9 +610,7 @@ async function cleanWorkspace(workspacePath) { const doIt = 'Restart and delete'; window.showWarningMessage('Are you sure you want to clean the Java language server workspace?', 'Cancel', doIt).then(selection => { if (selection === doIt) { - if (!fs.existsSync(workspacePath)) { - fs.mkdirSync(workspacePath); - } + ensureExists(workspacePath); const file = path.join(workspacePath, cleanWorkspaceFileName); fs.closeSync(fs.openSync(file, 'w')); commands.executeCommand(Commands.RELOAD_WINDOW); @@ -690,9 +688,7 @@ async function openFormatter(extensionPath) { relativePath = fileName; } else { const root = path.join(extensionPath, '..', 'redhat.java'); - if (!fs.existsSync(root)) { - fs.mkdirSync(root); - } + ensureExists(root); file = path.join(root, fileName); } if (!fs.existsSync(file)) { @@ -765,9 +761,7 @@ async function addFormatter(extensionPath, formatterUrl, defaultFormatter, relat relativePath = fileName; } else { const root = path.join(extensionPath, '..', 'redhat.java'); - if (!fs.existsSync(root)) { - fs.mkdirSync(root); - } + ensureExists(root); f = path.join(root, fileName); } } else { @@ -780,9 +774,14 @@ async function addFormatter(extensionPath, formatterUrl, defaultFormatter, relat const action = 'Yes'; window.showWarningMessage(msg, action, 'No').then((selection) => { if (action === selection) { - fs.createReadStream(defaultFormatter) - .pipe(fs.createWriteStream(f)) - .on('finish', () => openDocument(extensionPath, f, defaultFormatter, relativePath)); + try { + ensureExists(path.dirname(f)); + fs.createReadStream(defaultFormatter) + .pipe(fs.createWriteStream(f)) + .on('finish', () => openDocument(extensionPath, f, defaultFormatter, relativePath)); + } catch (error) { + window.showErrorMessage(`Failed to create ${f}: ${error}`); + } } }); } else { diff --git a/src/utils.ts b/src/utils.ts index 1cbac47ac2..a4b4898bb7 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -41,9 +41,11 @@ export function getTimestamp(file) { } export function ensureExists(folder) { - if (!fs.existsSync(folder)) { - fs.mkdirSync(folder); + if (fs.existsSync(folder)) { + return; } + ensureExists(path.dirname(folder)); + fs.mkdirSync(folder); } export function getBuildFilePatterns(): string[] {