Skip to content

Commit 61bfa84

Browse files
authored
fix: Package json hash difference between linux and windows (#24321) (#24329)
Windows and Linux generated a different hash for the package json content as jackson default indenter used system line separator. Fixes #24305
1 parent bd5befa commit 61bfa84

3 files changed

Lines changed: 72 additions & 1 deletion

File tree

flow-server/src/main/java/com/vaadin/flow/internal/JacksonUtils.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import tools.jackson.core.JsonGenerator;
3737
import tools.jackson.core.json.JsonReadFeature;
3838
import tools.jackson.core.type.TypeReference;
39+
import tools.jackson.core.util.DefaultIndenter;
3940
import tools.jackson.core.util.DefaultPrettyPrinter;
4041
import tools.jackson.core.util.Separators;
4142
import tools.jackson.core.util.Separators.Spacing;
@@ -537,6 +538,10 @@ public static String toFileJson(JsonNode node) throws JacksonException {
537538
DefaultPrettyPrinter filePrinter = new DefaultPrettyPrinter(
538539
Separators.createDefaultInstance()
539540
.withObjectNameValueSpacing(Spacing.AFTER));
541+
// Force LF line endings so output (and any hash derived from it) is
542+
// identical across platforms; Jackson's default object indenter uses
543+
// the system line separator (\r\n on Windows, \n elsewhere).
544+
filePrinter.indentObjectsWith(new DefaultIndenter(" ", "\n"));
540545
return objectMapper.writer().with(filePrinter).writeValueAsString(node);
541546
}
542547

flow-server/src/test/java/com/vaadin/flow/internal/JacksonUtilsTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -487,7 +487,7 @@ public void toFileJson() throws JacksonException {
487487
"childValue": "child"
488488
},
489489
"parentValue": "parent"
490-
}""", JacksonUtils.toFileJson(json).replace("\r\n", "\n"));
490+
}""", JacksonUtils.toFileJson(json));
491491

492492
}
493493

flow-server/src/test/java/com/vaadin/flow/server/frontend/TaskUpdatePackagesNpmTest.java

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1622,6 +1622,72 @@ public void pnpmIsInUse_pwaOfflineDisabledAfterEnabled_flattenedOverridesRemoved
16221622
}
16231623
}
16241624

1625+
/**
1626+
* This tests that other systems generate the same hash as we get for a
1627+
* Windows machine. There was an issue in the jackson indenter that used
1628+
* different line separators on windows (\r\n) and linux (\n).
1629+
*/
1630+
@Test
1631+
public void windowsHashedPackageJson_otherSystemsGetSameHash() {
1632+
var json = """
1633+
{
1634+
"name": "no-name",
1635+
"license": "UNLICENSED",
1636+
"type": "module",
1637+
"dependencies": {
1638+
"@vaadin/common-frontend": "0.0.22",
1639+
"@vaadin/react-components": "25.1.2",
1640+
"@vaadin/vaadin-development-mode-detector": "2.0.7",
1641+
"adaptivecards": "1.2.6",
1642+
"brace": "0.11.1",
1643+
"date-fns": "4.1.0",
1644+
"lit": "3.3.2",
1645+
"react": "19.2.4",
1646+
"react-dom": "19.2.4",
1647+
"react-router": "7.13.1"
1648+
},
1649+
"devDependencies": {
1650+
"@babel/plugin-proposal-object-rest-spread": "7.20.7",
1651+
"@types/node": "25.5.0",
1652+
"@types/react": "19.2.14",
1653+
"@types/react-dom": "19.2.3",
1654+
"typescript": "5.9.3",
1655+
"vite": "7.3.2",
1656+
"vite-plugin-checker": "0.12.0"
1657+
},
1658+
"vaadin": {
1659+
"dependencies": {
1660+
"@vaadin/common-frontend": "0.0.22",
1661+
"@vaadin/react-components": "25.1.2",
1662+
"@vaadin/vaadin-development-mode-detector": "2.0.7",
1663+
"adaptivecards": "1.2.6",
1664+
"brace": "0.11.1",
1665+
"date-fns": "4.1.0",
1666+
"lit": "3.3.2",
1667+
"react": "19.2.4",
1668+
"react-dom": "19.2.4",
1669+
"react-router": "7.13.1"
1670+
},
1671+
"devDependencies": {
1672+
"@babel/plugin-proposal-object-rest-spread": "7.20.7",
1673+
"@types/node": "25.5.0",
1674+
"@types/react": "19.2.14",
1675+
"@types/react-dom": "19.2.3",
1676+
"typescript": "5.9.3",
1677+
"vite": "7.3.2",
1678+
"vite-plugin-checker": "0.12.0"
1679+
},
1680+
"hash": "a4b492aecb32fe13902befbcc4ad0efbe6417273e8ca60346c4839973ae8242c"
1681+
}
1682+
}
1683+
""";
1684+
1685+
var packageJson = JacksonUtils.readTree(json);
1686+
var hash = TaskUpdatePackages.generatePackageJsonHash(packageJson);
1687+
Assert.assertEquals(packageJson.get("vaadin").get("hash").asString(),
1688+
hash);
1689+
}
1690+
16251691
@Test
16261692
public void generatePackageJsonHash_pnpmOverrides_includedInHash()
16271693
throws IOException {

0 commit comments

Comments
 (0)