Skip to content

Commit f59f3fa

Browse files
authored
Merge pull request #657 from simschla/bugfix/create-server-resources-on-function-creation
Bugfix/create server resources on function creation
2 parents aefa78e + 86e0b34 commit f59f3fa

File tree

7 files changed

+81
-2
lines changed

7 files changed

+81
-2
lines changed

Diff for: CHANGES.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ This document is intended for Spotless developers.
1010
We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `1.27.0`).
1111

1212
## [Unreleased]
13-
* Bump default ktfmt from 0.15 to 0.16, and remove duplicated logic for the --dropbox-style option ([#642](https://github.com/diffplug/spotless/pull/648))
13+
### Fixed
14+
* `FormatterFunc.Closeable` had a "use after free" bug which caused errors in the npm-based formatters (e.g. prettier) if `spotlessCheck` was called on dirty files. ([#651](https://github.com/diffplug/spotless/issues/651))
15+
### Changed
16+
* Bump default ktfmt from `0.15` to `0.16`, and remove duplicated logic for the `--dropbox-style` option ([#642](https://github.com/diffplug/spotless/pull/648))
1417

1518
## [2.2.0] - 2020-07-13
1619
### Added

Diff for: lib/src/main/java/com/diffplug/spotless/FormatterStepImpl.java

+1
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ protected String format(State state, String rawUnix, File file) throws Exception
8181
void cleanupFormatterFunc() {
8282
if (formatter instanceof FormatterFunc.Closeable) {
8383
((FormatterFunc.Closeable) formatter).close();
84+
formatter = null;
8485
}
8586
}
8687
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright 2020 DiffPlug
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.diffplug.spotless.npm;
17+
18+
import static java.util.Objects.requireNonNull;
19+
20+
import java.io.PrintStream;
21+
import java.time.LocalDateTime;
22+
23+
enum FormattedPrinter {
24+
SYSOUT(System.out);
25+
26+
private static final boolean enabled = false;
27+
28+
private final PrintStream printStream;
29+
30+
FormattedPrinter(PrintStream printStream) {
31+
this.printStream = requireNonNull(printStream);
32+
}
33+
34+
public void print(String msg, Object... paramsForStringFormat) {
35+
if (!enabled) {
36+
return;
37+
}
38+
String formatted = String.format(msg, paramsForStringFormat);
39+
String prefixed = String.format("[%s] %s", LocalDateTime.now().toString(), formatted);
40+
this.printStream.println(prefixed);
41+
}
42+
}

Diff for: lib/src/main/java/com/diffplug/spotless/npm/NpmFormatterStepStateBase.java

+2
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,9 @@ private NodeServerLayout prepareNodeServer(File buildDir) throws IOException {
7474
this.npmConfig.getPackageJsonContent());
7575
NpmResourceHelper
7676
.writeUtf8StringToFile(layout.serveJsFile(), this.npmConfig.getServeScriptContent());
77+
FormattedPrinter.SYSOUT.print("running npm install");
7778
runNpmInstall(layout.nodeModulesDir());
79+
FormattedPrinter.SYSOUT.print("npm install finished");
7880
return layout;
7981
}
8082

Diff for: lib/src/main/java/com/diffplug/spotless/npm/PrettierFormatterStep.java

+4
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ private static class State extends NpmFormatterStepStateBase implements Serializ
8080
@Nonnull
8181
public FormatterFunc createFormatterFunc() {
8282
try {
83+
FormattedPrinter.SYSOUT.print("creating formatter function (starting server)");
8384
ServerProcessInfo prettierRestServer = npmRunServer();
8485
PrettierRestService restService = new PrettierRestService(prettierRestServer.getBaseUrl());
8586
String prettierConfigOptions = restService.resolveConfig(this.prettierConfig.getPrettierConfigPath(), this.prettierConfig.getOptions());
@@ -90,6 +91,7 @@ public FormatterFunc createFormatterFunc() {
9091
}
9192

9293
private void endServer(PrettierRestService restService, ServerProcessInfo restServer) throws Exception {
94+
FormattedPrinter.SYSOUT.print("Closing formatting function (ending server).");
9395
try {
9496
restService.shutdown();
9597
} catch (Throwable t) {
@@ -111,6 +113,8 @@ public PrettierFilePathPassingFormatterFunc(String prettierConfigOptions, Pretti
111113

112114
@Override
113115
public String applyWithFile(String unix, File file) throws Exception {
116+
FormattedPrinter.SYSOUT.print("formatting String '" + unix.substring(0, 50) + "[...]' in file '" + file + "'");
117+
114118
final String prettierConfigOptionsWithFilepath = assertFilepathInConfigOptions(file);
115119
return restService.format(unix, prettierConfigOptionsWithFilepath);
116120
}

Diff for: plugin-gradle/CHANGES.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@
33
We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `3.27.0`).
44

55
## [Unreleased]
6+
### Fixed
67
* Depending on the file system, executing `gradle spotlessApply` might change permission on the changed files from `644` to `755`; fixes ([#656](https://github.com/diffplug/spotless/pull/656))
7-
* Bump default ktfmt from 0.15 to 0.16, and remove duplicated logic for the --dropbox-style option ([#642](https://github.com/diffplug/spotless/pull/648))
8+
* When using the `prettier` or `tsfmt` steps, if any files were dirty then `spotlessCheck` would fail with `java.net.ConnectException: Connection refused` rather than the proper error message ([#651](https://github.com/diffplug/spotless/issues/651)).
9+
### Changed
10+
* Bump default ktfmt from `0.15` to `0.16`, and remove duplicated logic for the `--dropbox-style` option ([#642](https://github.com/diffplug/spotless/pull/648))
811

912
## [5.1.0] - 2020-07-13
1013
### Added

Diff for: plugin-gradle/src/test/java/com/diffplug/gradle/spotless/PrettierIntegrationTest.java

+24
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,30 @@ public void useInlineConfig() throws IOException {
4848
assertFile("test.ts").sameAsResource("npm/prettier/config/typescript.configfile.clean");
4949
}
5050

51+
@Test
52+
public void verifyCleanSpotlessCheckWorks() throws IOException {
53+
setFile("build.gradle").toLines(
54+
"buildscript { repositories { mavenCentral() } }",
55+
"plugins {",
56+
" id 'com.diffplug.spotless'",
57+
"}",
58+
"def prettierConfig = [:]",
59+
"prettierConfig['printWidth'] = 50",
60+
"prettierConfig['parser'] = 'typescript'",
61+
"spotless {",
62+
" format 'mytypescript', {",
63+
" target 'test.ts'",
64+
" prettier().config(prettierConfig)",
65+
" }",
66+
"}");
67+
setFile("test.ts").toResource("npm/prettier/config/typescript.dirty");
68+
BuildResult spotlessCheckFailsGracefully = gradleRunner().withArguments("--stacktrace", "clean", "spotlessCheck").buildAndFail();
69+
Assertions.assertThat(spotlessCheckFailsGracefully.getOutput()).contains("> The following files had format violations:");
70+
71+
gradleRunner().withArguments("--stacktrace", "clean", "spotlessApply").build();
72+
gradleRunner().withArguments("--stacktrace", "clean", "spotlessCheck").build();
73+
}
74+
5175
@Test
5276
public void useFileConfig() throws IOException {
5377
setFile(".prettierrc.yml").toResource("npm/prettier/config/.prettierrc.yml");

0 commit comments

Comments
 (0)