From 030eb69fba3633a7afebab2a5c9f3f704fb9d4d9 Mon Sep 17 00:00:00 2001 From: adityaanikam Date: Mon, 31 Aug 2026 16:37:48 +0530 Subject: [PATCH] Fix NPE in createP2Index for in-place composite-repository edits modifyCompositeRepository resolves the effective output location for an in-place edit (a null outputLocation on the descriptor) inside modifyOutputCompositeRepository, but only as a local variable that is never written back to the descriptor. It then calls createP2Index(repository.outputLocation(), ...) directly afterward, passing the still-null field, which createP2Index dereferences with directory.resolve("p2.index") -- throwing NullPointerException after the composite metadata has already been written. Extract the existing null-fallback into a shared resolveOutputLocation() helper and use it at both call sites, so modifyCompositeRepository passes the same resolved path to createP2Index that modifyOutputCompositeRepository already computes for itself. Fixes https://github.com/eclipse-tycho/tycho/issues/6251 (cherry picked from commit 937fa071048a2a51c087e77f35ba7cd4f0c2d98b) --- .../tools/P2RepositoryDataManipulator.java | 33 +++++++----- tycho-its/projects/issue6251/pom.xml | 50 +++++++++++++++++++ .../tycho/test/issue6251/Issue6251Test.java | 34 +++++++++++++ 3 files changed, 104 insertions(+), 13 deletions(-) create mode 100644 tycho-its/projects/issue6251/pom.xml create mode 100644 tycho-its/src/test/java/org/eclipse/tycho/test/issue6251/Issue6251Test.java diff --git a/p2-maven-plugin/src/main/java/org/eclipse/tycho/p2maven/tools/P2RepositoryDataManipulator.java b/p2-maven-plugin/src/main/java/org/eclipse/tycho/p2maven/tools/P2RepositoryDataManipulator.java index 2a1dbd5145..68d6c7793f 100644 --- a/p2-maven-plugin/src/main/java/org/eclipse/tycho/p2maven/tools/P2RepositoryDataManipulator.java +++ b/p2-maven-plugin/src/main/java/org/eclipse/tycho/p2maven/tools/P2RepositoryDataManipulator.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2025, 2025 Hannes Wellmann and others. + * Copyright (c) 2025, 2026 Hannes Wellmann and others. * This program and the accompanying materials * are made available under the terms of the Eclipse Public License 2.0 * which accompanies this distribution, and is available at @@ -133,7 +133,7 @@ public void modifyRepositoryMetadata(ModifiedRepositoryDescriptor repository, St modifyOutputRepository(manager.getMetadataRepository(location.getURL(), location.getId()), outputLocation, manager::mirrorMetadataRepository, r -> r.executeBatch(m -> modification.accept(r), null)); } - xzCompress(repository, outputLocation); + xzCompress(repository); } @FunctionalInterface @@ -155,8 +155,9 @@ private > void modifyOutputRepository(R repository, } } - private void xzCompress(ModifiedRepositoryDescriptor repository, Path outputLocation) throws IOException { + private void xzCompress(ModifiedRepositoryDescriptor repository) throws IOException { if (repository.xzCompress()) { + Path outputLocation = resolveOutputLocation(repository); XZCompressor xzCompressor = new XZCompressor(); xzCompressor.setPreserveOriginalFile(repository.keepNonXzIndexFiles()); xzCompressor.setRepoFolder(outputLocation.toAbsolutePath().toString()); @@ -210,7 +211,7 @@ public void modifyCompositeRepository(ModifiedRepositoryDescriptor repository, S }, modification, CompositeMetadataRepository.PI_REPOSITORY_TYPE, CompositeMetadataRepositoryFactory.CONTENT_FILENAME); } - createP2Index(repository.outputLocation(), repository.isMetadata(), repository.isArtifact()); + createP2Index(resolveOutputLocation(repository), repository.isMetadata(), repository.isArtifact()); } private , R extends ICompositeRepository> void modifyOutputCompositeRepository( @@ -234,15 +235,7 @@ private , R extends ICompositeRepository> throw e; } } - Path outputLocation = descriptor.outputLocation(); - if (outputLocation == null) { - try { - outputLocation = Path.of(repository.getURL()); - } catch (Exception e) { - throw new IllegalStateException( - "Repository is not modifable and no output location is defined: " + repository.getURL(), e); - } - } + Path outputLocation = resolveOutputLocation(descriptor); modification.accept(state); @@ -254,6 +247,20 @@ private , R extends ICompositeRepository> } } + private static Path resolveOutputLocation(ModifiedRepositoryDescriptor descriptor) { + Path outputLocation = descriptor.outputLocation(); + if (outputLocation == null) { // In-place edit + MavenRepositoryLocation repository = descriptor.repository(); + try { + outputLocation = Path.of(repository.getURL()); + } catch (Exception e) { + throw new IllegalStateException( + "Repository is not modifable and no output location is defined: " + repository.getURL(), e); + } + } + return outputLocation; + } + private static OutputStream createCompositeFileOutputStream(Path directory, String filename, boolean compress) throws IOException { Path file = directory.resolve(filename + CompositeArtifactRepository.XML_EXTENSION); diff --git a/tycho-its/projects/issue6251/pom.xml b/tycho-its/projects/issue6251/pom.xml new file mode 100644 index 0000000000..ebcec94cf9 --- /dev/null +++ b/tycho-its/projects/issue6251/pom.xml @@ -0,0 +1,50 @@ + + + + 4.0.0 + + tycho-its-project + issue6251 + 1.0.0-SNAPSHOT + pom + + + + + org.eclipse.tycho + tycho-p2-repository-plugin + ${tycho-version} + + + modify-in-place + initialize + + modify-composite-repository + + + + t + ${project.baseUri}repo/ + + metadata + + ../child + + false + + + + + + + diff --git a/tycho-its/src/test/java/org/eclipse/tycho/test/issue6251/Issue6251Test.java b/tycho-its/src/test/java/org/eclipse/tycho/test/issue6251/Issue6251Test.java new file mode 100644 index 0000000000..68a6a6aff3 --- /dev/null +++ b/tycho-its/src/test/java/org/eclipse/tycho/test/issue6251/Issue6251Test.java @@ -0,0 +1,34 @@ +/******************************************************************************* + * Copyright (c) 2026 Contributors to the Eclipse Foundation + * This program and the accompanying materials + * are made available under the terms of the Eclipse Public License 2.0 + * which accompanies this distribution, and is available at + * https://www.eclipse.org/legal/epl-2.0/ + * + * SPDX-License-Identifier: EPL-2.0 + *******************************************************************************/ +package org.eclipse.tycho.test.issue6251; + +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.List; + +import org.apache.maven.it.Verifier; +import org.eclipse.tycho.test.AbstractTychoIntegrationTest; +import org.junit.jupiter.api.Test; + +public class Issue6251Test extends AbstractTychoIntegrationTest { + + @Test + public void modifyCompositeRepositoryInPlaceWritesP2Index() throws Exception { + Verifier verifier = getVerifier("issue6251"); + + verifier.executeGoals(List.of("initialize")); + verifier.verifyErrorFreeLog(); + + Path p2Index = Path.of(verifier.getBasedir(), "repo", "p2.index"); + assertTrue(Files.exists(p2Index), "p2.index was not written to the in-place repository"); + } +}