Skip to content
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

fix: better exception for failing removal (#12659) (CP: 2.7) #12668

Merged
merged 1 commit into from
Jan 3, 2022
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
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -459,27 +452,42 @@ private List<String> modifyPnpmFile(File generatedFile, String versionsPath)
return lines;
}

private void cleanUp() throws IOException {
private void cleanUp() throws ExecutionFailedException {
if (!packageUpdater.nodeModulesFolder.exists()) {
return;
}
File modulesYaml = new File(packageUpdater.nodeModulesFolder,
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
File staging = new File(packageUpdater.nodeModulesFolder,
".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;
Expand Down