Skip to content

Commit de7942d

Browse files
vaadin-botcaalador
andauthored
fix: Package json hash difference between linux and windows (#24321) (CP: 25.1) (#24328)
This PR cherry-picks changes from the original PR #24321 to branch 25.1. --- #### Original PR description > Windows and Linux generated a different > hash for the package json content > as jackson default indenter > used system line separator. > > Fixes #24305 > Co-authored-by: caalador <mikael.grankvist@vaadin.com>
1 parent 0b1ceb4 commit de7942d

3 files changed

Lines changed: 73 additions & 1 deletion

File tree

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

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131

3232
import net.jcip.annotations.NotThreadSafe;
3333
import org.apache.commons.io.FileUtils;
34+
import org.junit.Assert;
3435
import org.junit.jupiter.api.BeforeEach;
3536
import org.junit.jupiter.api.Test;
3637
import org.junit.jupiter.api.io.TempDir;
@@ -1589,6 +1590,72 @@ void pnpmIsInUse_pwaOfflineDisabledAfterEnabled_flattenedOverridesRemoved()
15891590
}
15901591
}
15911592

1593+
/**
1594+
* This tests that other systems generate the same hash as we get for a
1595+
* Windows machine. There was an issue in the jackson indenter that used
1596+
* different line separators on windows (\r\n) and linux (\n).
1597+
*/
1598+
@Test
1599+
void windowsHashedPackageJson_otherSystemsGetSameHash() {
1600+
var json = """
1601+
{
1602+
"name": "no-name",
1603+
"license": "UNLICENSED",
1604+
"type": "module",
1605+
"dependencies": {
1606+
"@vaadin/common-frontend": "0.0.22",
1607+
"@vaadin/react-components": "25.1.2",
1608+
"@vaadin/vaadin-development-mode-detector": "2.0.7",
1609+
"adaptivecards": "1.2.6",
1610+
"brace": "0.11.1",
1611+
"date-fns": "4.1.0",
1612+
"lit": "3.3.2",
1613+
"react": "19.2.4",
1614+
"react-dom": "19.2.4",
1615+
"react-router": "7.13.1"
1616+
},
1617+
"devDependencies": {
1618+
"@babel/plugin-proposal-object-rest-spread": "7.20.7",
1619+
"@types/node": "25.5.0",
1620+
"@types/react": "19.2.14",
1621+
"@types/react-dom": "19.2.3",
1622+
"typescript": "5.9.3",
1623+
"vite": "7.3.2",
1624+
"vite-plugin-checker": "0.12.0"
1625+
},
1626+
"vaadin": {
1627+
"dependencies": {
1628+
"@vaadin/common-frontend": "0.0.22",
1629+
"@vaadin/react-components": "25.1.2",
1630+
"@vaadin/vaadin-development-mode-detector": "2.0.7",
1631+
"adaptivecards": "1.2.6",
1632+
"brace": "0.11.1",
1633+
"date-fns": "4.1.0",
1634+
"lit": "3.3.2",
1635+
"react": "19.2.4",
1636+
"react-dom": "19.2.4",
1637+
"react-router": "7.13.1"
1638+
},
1639+
"devDependencies": {
1640+
"@babel/plugin-proposal-object-rest-spread": "7.20.7",
1641+
"@types/node": "25.5.0",
1642+
"@types/react": "19.2.14",
1643+
"@types/react-dom": "19.2.3",
1644+
"typescript": "5.9.3",
1645+
"vite": "7.3.2",
1646+
"vite-plugin-checker": "0.12.0"
1647+
},
1648+
"hash": "a4b492aecb32fe13902befbcc4ad0efbe6417273e8ca60346c4839973ae8242c"
1649+
}
1650+
}
1651+
""";
1652+
1653+
var packageJson = JacksonUtils.readTree(json);
1654+
var hash = TaskUpdatePackages.generatePackageJsonHash(packageJson);
1655+
Assert.assertEquals(packageJson.get("vaadin").get("hash").asString(),
1656+
hash);
1657+
}
1658+
15921659
@Test
15931660
void generatePackageJsonHash_pnpmOverrides_includedInHash()
15941661
throws IOException {

‎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;
@@ -574,6 +575,10 @@ public static String toFileJson(JsonNode node) throws JacksonException {
574575
DefaultPrettyPrinter filePrinter = new DefaultPrettyPrinter(
575576
Separators.createDefaultInstance()
576577
.withObjectNameValueSpacing(Spacing.AFTER));
578+
// Force LF line endings so output (and any hash derived from it) is
579+
// identical across platforms; Jackson's default object indenter uses
580+
// the system line separator (\r\n on Windows, \n elsewhere).
581+
filePrinter.indentObjectsWith(new DefaultIndenter(" ", "\n"));
577582
return objectMapper.writer().with(filePrinter).writeValueAsString(node);
578583
}
579584

‎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
@@ -497,7 +497,7 @@ public void toFileJson() throws JacksonException {
497497
"childValue": "child"
498498
},
499499
"parentValue": "parent"
500-
}""", JacksonUtils.toFileJson(json).replace("\r\n", "\n"));
500+
}""", JacksonUtils.toFileJson(json));
501501

502502
}
503503

0 commit comments

Comments
 (0)