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: reliable deletion of node_modules (#12874) (CP: 2.7) #12877

Merged
merged 1 commit into from
Jan 28, 2022
Merged
Show file tree
Hide file tree
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 @@ -29,6 +29,8 @@
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;

import com.vaadin.flow.server.frontend.FrontendUtils;

import elemental.json.Json;
import elemental.json.JsonObject;
import elemental.json.impl.JsonUtil;
Expand Down Expand Up @@ -134,15 +136,12 @@ boolean isDefaultCompatibility() {
private void removeNodeModules() {
// Remove node_modules folder
File nodeModules = new File(npmFolder, "node_modules");
if (nodeModules.exists()) {
try {
FileUtils.deleteDirectory(nodeModules);
} catch (IOException exception) {
getLog().debug("Exception removing node_modules", exception);
getLog().error(
"Failed to remove '" + nodeModules.getAbsolutePath()
+ "'. Please remove it manually.");
}
try {
FrontendUtils.deleteNodeModules(nodeModules);
} catch (IOException exception) {
getLog().debug("Exception removing node_modules", exception);
getLog().error("Failed to remove '" + nodeModules.getAbsolutePath()
+ "'. Please remove it manually.");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,18 @@
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.time.LocalDateTime;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Scanner;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.apache.commons.io.FileUtils;
Expand Down Expand Up @@ -1064,4 +1067,39 @@ public static void console(String format, Object message) {
System.out.print(String.format(format, message));
}

/**
* Try to remove the {@code node_modules} directory, if it exists inside the
* given base directory. Note that pnpm uses symlinks internally, so delete
* utilities that follow symlinks when deleting and/or modifying permissions
* may not work as intended.
*
* @param nodeModules
* the {@code node_modules} directory
* @throws IOException
* on failure to delete any one file, or if the directory name
* is not {@code node_modules}
*/
public static void deleteNodeModules(File nodeModules) throws IOException {
if (!nodeModules.exists()) {
return;
}

if (!nodeModules.isDirectory()
|| !nodeModules.getName().equals("node_modules")) {
throw new IOException(nodeModules.getAbsolutePath()
+ " does not look like a node_modules directory");
}

Path nodeModulesPath = nodeModules.toPath();
try (Stream<Path> walk = Files.walk(nodeModulesPath)) {
String undeletable = walk.sorted(Comparator.reverseOrder())
.map(Path::toFile).filter(file -> !file.delete())
.map(File::getAbsolutePath)
.collect(Collectors.joining(", "));

if (!undeletable.isEmpty() && nodeModules.exists()) {
throw new IOException("Unable to delete files: " + undeletable);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,7 @@ private void cleanUp() throws ExecutionFailedException {
private void deleteNodeModules(File nodeModulesFolder)
throws ExecutionFailedException {
try {
FileUtils.forceDelete(nodeModulesFolder);
FrontendUtils.deleteNodeModules(nodeModulesFolder);
} catch (IOException exception) {
Logger log = packageUpdater.log();
log.debug("Exception removing node_modules", exception);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.Arrays;
import java.util.List;

Expand Down Expand Up @@ -363,6 +364,57 @@ public void clearCachedStatsContent_clearsCache()
service.getContext().getAttribute(CACHE_KEY));
}

@Test
public void deleteNodeModules_nopIfNotExists() throws IOException {
File nodeModules = new File(tmpDir.getRoot(), "node_modules");
FrontendUtils.deleteNodeModules(nodeModules);
}

@Test(expected = IOException.class)
public void deleteNodeModules_throwsIfNotNamedNodeModules()
throws IOException {
File myModules = new File(tmpDir.getRoot(), "my_modules");
myModules.mkdirs();
FrontendUtils.deleteNodeModules(myModules);
}

@Test
public void deleteNodeModules_canDeleteSymlinksAndNotFollowThem()
throws IOException {
File externalDir = new File(tmpDir.getRoot(), "external");
File externalLicense = new File(externalDir, "LICENSE");

externalLicense.getParentFile().mkdirs();
externalLicense.createNewFile();

File nodeModules = new File(tmpDir.getRoot(), "node_modules");
File containing = new File(nodeModules, ".pnpm/a/node_modules/dep");
containing.mkdirs();
File license = new File(containing, "LICENSE");
license.createNewFile();

File linking = new File(nodeModules, ".pnpm/b/node_modules/dep");
linking.getParentFile().mkdirs();
Files.createSymbolicLink(linking.toPath(),
new File("../../a/node_modules/dep").toPath());

File linkingExternal = new File(nodeModules,
".pnpm/b/node_modules/external");
Files.createSymbolicLink(linkingExternal.toPath(),
new File("../../../../external").toPath());

Assert.assertTrue(nodeModules.exists());
Assert.assertTrue(linking.exists());
Assert.assertTrue(new File(linking, "LICENSE").exists());
Assert.assertTrue(new File(linkingExternal, "LICENSE").exists());

FrontendUtils.deleteNodeModules(nodeModules);

Assert.assertFalse(nodeModules.exists());
Assert.assertTrue(externalLicense.exists());
}


private ResourceProvider mockResourceProvider(VaadinService service,
VaadinContext context) {
DeploymentConfiguration config = Mockito
Expand Down