Skip to content

Commit e803400

Browse files
authored
fix: bump npm infrastructure dependencies and make sure changing serve.js content is applied (#2542)
2 parents c96c28f + 3bfd0ae commit e803400

File tree

14 files changed

+177
-27
lines changed

14 files changed

+177
-27
lines changed

CHANGES.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
1414
* Support for `idea` ([#2020](https://github.com/diffplug/spotless/pull/2020), [#2535](https://github.com/diffplug/spotless/pull/2535))
1515
* Add support for removing wildcard imports via `removeWildcardImports` step. ([#2517](https://github.com/diffplug/spotless/pull/2517))
1616

17+
### Fixed
18+
* Make sure npm-based formatters use the correct `node_modules` directory when running in parallel. ([#2542](https://github.com/diffplug/spotless/pull/2542))
19+
20+
### Changed
21+
* Bump internal dependencies for npm-based formatters ([#2542](https://github.com/diffplug/spotless/pull/2542))
22+
1723
## [3.1.2] - 2025-05-27
1824
### Fixed
1925
* Fix `UnsupportedOperationException` in the Gradle plugin when using `targetExcludeContent[Pattern]` ([#2487](https://github.com/diffplug/spotless/pull/2487))

lib/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ dependencies {
7676
testCommonImplementation "org.junit.jupiter:junit-jupiter:$VER_JUNIT"
7777
testCommonImplementation "org.assertj:assertj-core:$VER_ASSERTJ"
7878
testCommonImplementation "com.diffplug.durian:durian-testlib:$VER_DURIAN"
79+
testCommonImplementation projects.testlib
7980
testCommonRuntimeOnly "org.junit.platform:junit-platform-launcher"
8081

8182
// GLUE CODE (alphabetic order please)

lib/src/main/java/com/diffplug/spotless/npm/NodeServerLayout.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2020-2023 DiffPlug
2+
* Copyright 2020-2025 DiffPlug
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -37,16 +37,16 @@ class NodeServerLayout {
3737
private final File serveJsFile;
3838
private final File npmrcFile;
3939

40-
NodeServerLayout(File buildDir, String packageJsonContent) {
41-
this.nodeModulesDir = new File(buildDir, nodeModulesDirName(packageJsonContent));
40+
NodeServerLayout(File buildDir, String packageJsonContent, String serveJsContent) {
41+
this.nodeModulesDir = new File(buildDir, nodeModulesDirName(packageJsonContent, serveJsContent));
4242
this.packageJsonFile = new File(nodeModulesDir, "package.json");
4343
this.packageLockJsonFile = new File(nodeModulesDir, "package-lock.json");
4444
this.serveJsFile = new File(nodeModulesDir, "serve.js");
4545
this.npmrcFile = new File(nodeModulesDir, ".npmrc");
4646
}
4747

48-
private static String nodeModulesDirName(String packageJsonContent) {
49-
String md5Hash = NpmResourceHelper.md5(packageJsonContent);
48+
private static String nodeModulesDirName(String packageJsonContent, String serveJsContent) {
49+
String md5Hash = NpmResourceHelper.md5(packageJsonContent, serveJsContent);
5050
Matcher matcher = PACKAGE_JSON_NAME_PATTERN.matcher(packageJsonContent);
5151
if (!matcher.find()) {
5252
throw new IllegalArgumentException("package.json must contain a name property");

lib/src/main/java/com/diffplug/spotless/npm/NpmFormatterStepStateBase.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public static class Runtime {
7171

7272
Runtime(NpmFormatterStepStateBase parent) {
7373
this.parent = parent;
74-
this.nodeServerLayout = new NodeServerLayout(parent.locations.buildDir(), parent.npmConfig.getPackageJsonContent());
74+
this.nodeServerLayout = new NodeServerLayout(parent.locations.buildDir(), parent.npmConfig.getPackageJsonContent(), parent.npmConfig.getServeScriptContent());
7575
this.nodeServeApp = new NodeServeApp(nodeServerLayout, parent.npmConfig, parent.locations);
7676
}
7777

lib/src/main/java/com/diffplug/spotless/npm/NpmResourceHelper.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2023 DiffPlug
2+
* Copyright 2016-2025 DiffPlug
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -27,10 +27,14 @@
2727
import java.util.Objects;
2828
import java.util.concurrent.TimeoutException;
2929
import java.util.stream.Collectors;
30+
import java.util.stream.Stream;
3031

3132
import com.diffplug.spotless.ThrowingEx;
3233

3334
final class NpmResourceHelper {
35+
36+
public static final String MD5_STRING_DELIMITER = "@@@";
37+
3438
private NpmResourceHelper() {
3539
// no instance required
3640
}
@@ -140,9 +144,15 @@ static String md5(File file) {
140144
return md5(readUtf8StringFromFile(file));
141145
}
142146

143-
static String md5(String fileContent) {
147+
static String md5(String fileContent, String... additionalFileContents) {
148+
Objects.requireNonNull(fileContent, "fileContent must not be null");
149+
Stream<String> additionalFileContentStream = Stream.concat(
150+
Stream.of(fileContent),
151+
Stream.of(additionalFileContents));
144152
MessageDigest md = ThrowingEx.get(() -> MessageDigest.getInstance("MD5"));
145-
md.update(fileContent.getBytes(StandardCharsets.UTF_8));
153+
String stringToHash = additionalFileContentStream.collect(Collectors.joining(MD5_STRING_DELIMITER));
154+
md.update(stringToHash.getBytes(StandardCharsets.UTF_8));
155+
146156
byte[] digest = md.digest();
147157
// convert byte array digest to hex string
148158
StringBuilder sb = new StringBuilder();

lib/src/main/resources/com/diffplug/spotless/npm/common-serve.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// this file will be glued to the top of the specific xy-serve.js file
22
const debug_serve = false; // set to true for debug log output in node process
3-
const GracefulShutdownManager = require("@moebius/http-graceful-shutdown").GracefulShutdownManager;
3+
const shutdownServer = require("http-graceful-shutdown");
44
const express = require("express");
55
const app = express();
66

@@ -48,12 +48,18 @@ var listener = app.listen(0, "127.0.0.1", () => {
4848
}
4949
});
5050
});
51-
const shutdownManager = new GracefulShutdownManager(listener);
51+
const shutdown = shutdownServer(listener, {
52+
forceExit: false, // let the event loop clear
53+
finally: () => debugLog("graceful shutdown finished."),
54+
});
5255

5356
app.post("/shutdown", (req, res) => {
5457
res.status(200).send("Shutting down");
55-
setTimeout(function () {
56-
shutdownManager.terminate(() => debugLog("graceful shutdown finished."));
58+
setTimeout(async () => {
59+
try {
60+
await shutdown();
61+
} catch (err) {
62+
console.error("Error during shutdown:", err);
63+
}
5764
}, 200);
5865
});
59-
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "spotless-eslint",
3-
"version": "2.0.0",
3+
"version": "4.0.0",
44
"description": "Spotless formatter step for running eslint as a rest service.",
55
"repository": "https://github.com/diffplug/spotless",
66
"license": "Apache-2.0",
@@ -9,11 +9,11 @@
99
},
1010
"devDependencies": {
1111
${devDependencies},
12-
"express": "4.18.2",
13-
"@moebius/http-graceful-shutdown": "1.1.0"
12+
"express": "5.1.0",
13+
"http-graceful-shutdown": "3.1.14"
1414
},
1515
"dependencies": {},
1616
"engines": {
17-
"node": ">=6"
17+
"node": ">= 18"
1818
}
1919
}
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "spotless-prettier",
3-
"version": "2.0.0",
3+
"version": "4.0.0",
44
"description": "Spotless formatter step for running prettier as a rest service.",
55
"repository": "https://github.com/diffplug/spotless",
66
"license": "Apache-2.0",
@@ -9,11 +9,11 @@
99
},
1010
"devDependencies": {
1111
${devDependencies},
12-
"express": "4.18.2",
13-
"@moebius/http-graceful-shutdown": "1.1.0"
12+
"express": "5.1.0",
13+
"http-graceful-shutdown": "3.1.14"
1414
},
1515
"dependencies": {},
1616
"engines": {
17-
"node": ">=6"
17+
"node": ">= 18"
1818
}
1919
}
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "spotless-tsfmt",
3-
"version": "2.0.0",
3+
"version": "4.0.0",
44
"description": "Spotless formatter step for running tsfmt as a rest service.",
55
"repository": "https://github.com/diffplug/spotless",
66
"license": "Apache-2.0",
@@ -9,11 +9,11 @@
99
},
1010
"devDependencies": {
1111
${devDependencies},
12-
"express": "4.18.2",
13-
"@moebius/http-graceful-shutdown": "1.1.0"
12+
"express": "5.1.0",
13+
"http-graceful-shutdown": "3.1.14"
1414
},
1515
"dependencies": {},
1616
"engines": {
17-
"node": ">= 4.2.0"
17+
"node": ">= 18"
1818
}
1919
}

lib/src/main/resources/com/diffplug/spotless/npm/tsfmt-serve.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ app.post("/tsfmt/format", (req, res) => {
1919
*/
2020
// result contains 'message' (String), 'error' (boolean), 'dest' (String) => formatted
2121
if (resultMap.error !== undefined && resultMap.error) {
22-
res.status(400).send(resultmap.message);
22+
res.status(400).send(resultMap.message);
2323
return;
2424
}
2525
res.set("Content-Type", "text/plain");

0 commit comments

Comments
 (0)