From f5edf0f5d3d1f3946e80816b401e12b5fffdb8c2 Mon Sep 17 00:00:00 2001 From: caalador Date: Mon, 3 Jan 2022 15:46:50 +0200 Subject: [PATCH] fix: better exception for failing removal (#12659) Update exception for failing to remove node_modules folder. Closes #12655 --- .../server/frontend/TaskRunNpmInstall.java | 32 ++++++++++++------- 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/flow-server/src/main/java/com/vaadin/flow/server/frontend/TaskRunNpmInstall.java b/flow-server/src/main/java/com/vaadin/flow/server/frontend/TaskRunNpmInstall.java index 0dc21dfc116..624d9444360 100644 --- a/flow-server/src/main/java/com/vaadin/flow/server/frontend/TaskRunNpmInstall.java +++ b/flow-server/src/main/java/com/vaadin/flow/server/frontend/TaskRunNpmInstall.java @@ -30,6 +30,7 @@ import java.util.stream.Collectors; import org.apache.commons.io.FileUtils; +import org.slf4j.Logger; import com.vaadin.flow.server.Constants; import com.vaadin.flow.server.ExecutionFailedException; @@ -97,8 +98,6 @@ public class TaskRunNpmInstall implements FallibleCommand { /** * Create an instance of the command. * - * @param classFinder - * a reusable class finder * @param packageUpdater * package-updater instance used for checking if previous * execution modified the package.json file @@ -298,13 +297,7 @@ private boolean isVaadinHashUpdated() { */ private void runNpmInstall() throws ExecutionFailedException { // Do possible cleaning before generating any new files. - try { - cleanUp(); - } catch (IOException exception) { - throw new ExecutionFailedException("Couldn't remove " - + packageUpdater.nodeModulesFolder + " directory", - exception); - } + cleanUp(); if (enablePnpm) { try { @@ -459,7 +452,7 @@ private List modifyPnpmFile(File generatedFile, String versionsPath) return lines; } - private void cleanUp() throws IOException { + private void cleanUp() throws ExecutionFailedException { if (!packageUpdater.nodeModulesFolder.exists()) { return; } @@ -467,7 +460,7 @@ private void cleanUp() throws IOException { MODULES_YAML); boolean hasModulesYaml = modulesYaml.exists() && modulesYaml.isFile(); if (!enablePnpm && hasModulesYaml) { - FileUtils.forceDelete(packageUpdater.nodeModulesFolder); + deleteNodeModules(packageUpdater.nodeModulesFolder); } else if (enablePnpm && !hasModulesYaml) { // presence of .staging dir with a "pnpm-*" folder means that pnpm // download is in progress, don't remove anything in this case @@ -475,11 +468,26 @@ private void cleanUp() throws IOException { ".staging"); if (!staging.isDirectory() || staging.listFiles( (dir, name) -> name.startsWith("pnpm-")).length == 0) { - FileUtils.forceDelete(packageUpdater.nodeModulesFolder); + deleteNodeModules(packageUpdater.nodeModulesFolder); } } } + private void deleteNodeModules(File nodeModulesFolder) + throws ExecutionFailedException { + try { + FileUtils.forceDelete(nodeModulesFolder); + } catch (IOException exception) { + Logger log = packageUpdater.log(); + log.debug("Exception removing node_modules", exception); + log.error("Failed to remove '" + + packageUpdater.nodeModulesFolder.getAbsolutePath() + + "'. Please remove it manually."); + throw new ExecutionFailedException( + "Exception removing node_modules. Please remove it manually."); + } + } + private void validateInstalledNpm(FrontendTools tools) throws IllegalStateException { File npmCacheDir = null;