This is an automated email from the ASF dual-hosted git repository. olamy pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/maven-build-cache-extension.git
The following commit(s) were added to refs/heads/master by this push: new 6680780 [MBUILDCACHE-99] Project checksum fails to identify moved files in certain cases (#161) 6680780 is described below commit 6680780096a5bea91f0d62b2ca92c0d09eaa35df Author: amirhadadi <amirhad...@hotmail.com> AuthorDate: Mon Jul 15 12:49:32 2024 +0300 [MBUILDCACHE-99] Project checksum fails to identify moved files in certain cases (#161) * [MBUILDCACHE-99] Project checksum fails to identify moved files in certain cases. Input files checksum is calculated by sorting all files and using their content (but not their path) to update a checksum. This means that if a file is moved / renamed in a way that doesn't change its location in the list of sorted files, the input files checksum will not change. This change adds the file's path to the checksum. --- .../maven/buildcache/checksum/DigestUtils.java | 1 + .../buildcache/checksum/MavenProjectInput.java | 4 +- .../apache/maven/buildcache/its/Issue99Test.java | 71 ++++++++++++++++++++++ .../projects/mbuildcache-99/.mvn/extensions.xml | 25 ++++++++ src/test/projects/mbuildcache-99/pom.xml | 36 +++++++++++ .../projects/mbuildcache-99/test-module/pom.xml | 33 ++++++++++ .../test-module/src/main/resources/test.properties | 1 + 7 files changed, 169 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/apache/maven/buildcache/checksum/DigestUtils.java b/src/main/java/org/apache/maven/buildcache/checksum/DigestUtils.java index a8e48b5..ee78733 100644 --- a/src/main/java/org/apache/maven/buildcache/checksum/DigestUtils.java +++ b/src/main/java/org/apache/maven/buildcache/checksum/DigestUtils.java @@ -56,6 +56,7 @@ public class DigestUtils { public static DigestItem file(HashChecksum checksum, Path basedir, Path file) throws IOException { byte[] content = Files.readAllBytes(file); String normalized = normalize(basedir, file); + checksum.update(normalized.getBytes(UTF_8)); DigestItem item = item("file", normalized, checksum.update(content)); try { populateContentDetails(file, content, item); diff --git a/src/main/java/org/apache/maven/buildcache/checksum/MavenProjectInput.java b/src/main/java/org/apache/maven/buildcache/checksum/MavenProjectInput.java index 7efc80b..4f8e926 100644 --- a/src/main/java/org/apache/maven/buildcache/checksum/MavenProjectInput.java +++ b/src/main/java/org/apache/maven/buildcache/checksum/MavenProjectInput.java @@ -183,10 +183,10 @@ public class MavenProjectInput { final long t1 = System.currentTimeMillis(); - // hash items: effective pom + version + input files + dependencies + // hash items: effective pom + version + input files paths + input files contents + dependencies final int count = 1 + (config.calculateProjectVersionChecksum() ? 1 : 0) - + inputFiles.size() + + 2 * inputFiles.size() + dependenciesChecksum.size() + pluginDependenciesChecksum.size(); diff --git a/src/test/java/org/apache/maven/buildcache/its/Issue99Test.java b/src/test/java/org/apache/maven/buildcache/its/Issue99Test.java new file mode 100644 index 0000000..4cfc125 --- /dev/null +++ b/src/test/java/org/apache/maven/buildcache/its/Issue99Test.java @@ -0,0 +1,71 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.maven.buildcache.its; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.List; + +import org.apache.maven.buildcache.its.junit.IntegrationTest; +import org.apache.maven.it.VerificationException; +import org.apache.maven.it.Verifier; +import org.junit.jupiter.api.Test; + +import static java.util.Arrays.asList; + +@IntegrationTest("src/test/projects/mbuildcache-99") +public class Issue99Test { + + @Test + void renamedFileInvalidatesCache(Verifier verifier) throws VerificationException, IOException { + verifier.setAutoclean(false); + + verifier.setLogFileName("../log-0.txt"); + verifier.executeGoals(asList("package")); + verifier.verifyErrorFreeLog(); + verifier.verifyTextInLog("Local build was not found"); + verifyTextNotInLog(verifier, "Found cached build"); + + verifier.setLogFileName("../log-1.txt"); + verifier.executeGoals(asList("package")); + verifier.verifyErrorFreeLog(); + verifier.verifyTextInLog("Found cached build"); + verifyTextNotInLog(verifier, "Local build was not found"); + + Files.move( + Paths.get(verifier.getBasedir(), "test-module/src/main/resources/test.properties"), + Paths.get(verifier.getBasedir(), "test-module/src/main/resources/test2.properties")); + + verifier.setLogFileName("../log-2.txt"); + verifier.executeGoals(asList("package")); + verifier.verifyErrorFreeLog(); + verifier.verifyTextInLog("Local build was not found"); + verifyTextNotInLog(verifier, "Found cached build"); + } + + private static void verifyTextNotInLog(Verifier verifier, String text) throws VerificationException { + List<String> lines = verifier.loadFile(verifier.getBasedir(), verifier.getLogFileName(), false); + for (String line : lines) { + if (Verifier.stripAnsi(line).contains(text)) { + throw new VerificationException("Text found in log: " + text); + } + } + } +} diff --git a/src/test/projects/mbuildcache-99/.mvn/extensions.xml b/src/test/projects/mbuildcache-99/.mvn/extensions.xml new file mode 100644 index 0000000..8df78f8 --- /dev/null +++ b/src/test/projects/mbuildcache-99/.mvn/extensions.xml @@ -0,0 +1,25 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + + Copyright 2021 the original author or authors. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + +--> +<extensions> + <extension> + <groupId>org.apache.maven.extensions</groupId> + <artifactId>maven-build-cache-extension</artifactId> + <version>${projectVersion}</version> + </extension> +</extensions> diff --git a/src/test/projects/mbuildcache-99/pom.xml b/src/test/projects/mbuildcache-99/pom.xml new file mode 100644 index 0000000..f814459 --- /dev/null +++ b/src/test/projects/mbuildcache-99/pom.xml @@ -0,0 +1,36 @@ +<!-- + + Copyright 2021 the original author or authors. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + +--> +<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> + + <modelVersion>4.0.0</modelVersion> + <groupId>org.apache.maven.caching.test.mbuildcache-99</groupId> + <artifactId>test</artifactId> + <version>0.0.1-SNAPSHOT</version> + <packaging>pom</packaging> + + <properties> + <maven.compiler.source>1.8</maven.compiler.source> + <maven.compiler.target>1.8</maven.compiler.target> + </properties> + + <modules> + <module>test-module</module> + </modules> + +</project> diff --git a/src/test/projects/mbuildcache-99/test-module/pom.xml b/src/test/projects/mbuildcache-99/test-module/pom.xml new file mode 100644 index 0000000..ffaff30 --- /dev/null +++ b/src/test/projects/mbuildcache-99/test-module/pom.xml @@ -0,0 +1,33 @@ +<!-- + + Copyright 2021 the original author or authors. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + +--> +<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> + + <modelVersion>4.0.0</modelVersion> + <artifactId>test-module</artifactId> + <packaging>jar</packaging> + + <parent> + <groupId>org.apache.maven.caching.test.mbuildcache-99</groupId> + <artifactId>test</artifactId> + <version>0.0.1-SNAPSHOT</version> + <relativePath>../pom.xml</relativePath> + </parent> + + +</project> diff --git a/src/test/projects/mbuildcache-99/test-module/src/main/resources/test.properties b/src/test/projects/mbuildcache-99/test-module/src/main/resources/test.properties new file mode 100644 index 0000000..1c6a1a9 --- /dev/null +++ b/src/test/projects/mbuildcache-99/test-module/src/main/resources/test.properties @@ -0,0 +1 @@ +a=b \ No newline at end of file