From d3c0e4d6cce123f581a5c5cbf6c6c05bedf5e9c5 Mon Sep 17 00:00:00 2001 From: piexel Date: Thu, 22 Oct 2026 09:55:12 +0200 Subject: [PATCH] Bundled jars: no special case for a pack's own copy Nothing is released, so a pack holding an old copy of a bundled jar gets no compatibility handling: it fails like any pack whose jars hold the same class, naming both ("Class ... is in both module jars iris-builtins.jar (bundled with the host) and modules/iris-builtins.jar"). Removes LoadedPack.replacedJars() and the bundled file name checks. Jar copies in a load's snapshot are named by their place in the load, so same-named jars from the host and the pack no longer collide before that check can name them. --- .../java/com/volmit/host/JarSnapshot.java | 12 ++--- src/main/java/com/volmit/host/LoadedPack.java | 5 --- src/main/java/com/volmit/host/ModuleHost.java | 20 +-------- .../java/com/volmit/host/PackModules.java | 44 +++++-------------- .../java/com/volmit/host/LoadedPackTest.java | 24 +++------- .../com/volmit/host/PackJarSnapshotTest.java | 2 +- 6 files changed, 28 insertions(+), 79 deletions(-) diff --git a/src/main/java/com/volmit/host/JarSnapshot.java b/src/main/java/com/volmit/host/JarSnapshot.java index f8a043463..d82f8e713 100644 --- a/src/main/java/com/volmit/host/JarSnapshot.java +++ b/src/main/java/com/volmit/host/JarSnapshot.java @@ -23,6 +23,7 @@ final class JarSnapshot { static final String DIRECTORY_PREFIX = "iris-pack-modules-"; static final String OWNER_FILE = "owner.txt"; static final String OWNER_HEADER = "Iris pack module jar copies"; + private static final String COPY_NAME_SEPARATOR = "-"; /** A module jar to copy: its origin, which every message names, and the file. */ record SourceJar(String origin, Path file) { @@ -48,15 +49,15 @@ final class JarSnapshot { this.jars = List.copyOf(jars); } - /** Copies {@code jars}, whose file names differ, into a new directory below {@code temporaryRoot}. Leaves nothing - * behind if a copy fails. + /** Copies {@code jars} into a new directory below {@code temporaryRoot}, each copy named by its place in the list so + * jars of the same file name from different places do not collide. Leaves nothing behind if a copy fails. */ static JarSnapshot take(List jars, Path temporaryRoot) { var directory = createOwnedDirectory(temporaryRoot); var copies = new ArrayList(jars.size()); try { - for (var jar : jars) { - copies.add(copy(jar, directory)); + for (int index = 0; index < jars.size(); index++) { + copies.add(copy(jars.get(index), directory.resolve(index + COPY_NAME_SEPARATOR + jars.get(index).file().getFileName()))); } return new JarSnapshot(directory, copies); } catch (RuntimeException | Error failure) { @@ -117,8 +118,7 @@ final class JarSnapshot { return OWNER_HEADER + "\n" + pid + "\n"; } - private static CopiedJar copy(SourceJar jar, Path directory) { - var copy = directory.resolve(jar.file().getFileName().toString()); + private static CopiedJar copy(SourceJar jar, Path copy) { try { Files.copy(jar.file(), copy); return new CopiedJar(jar.origin(), copy); diff --git a/src/main/java/com/volmit/host/LoadedPack.java b/src/main/java/com/volmit/host/LoadedPack.java index dc5e5c9c2..9cfd7be3e 100644 --- a/src/main/java/com/volmit/host/LoadedPack.java +++ b/src/main/java/com/volmit/host/LoadedPack.java @@ -53,11 +53,6 @@ public final class LoadedPack implements AutoCloseable { return packModules.modules(); } - /** The pack's own jars that a jar the host bundles replaced; see {@link PackModules#replacedJars()}. */ - public List replacedJars() { - return packModules.replacedJars(); - } - /** Classes the pack's jars use that nobody has, per jar; see {@link PackModules#missingClasses()}. */ public List missingClasses() { return packModules.missingClasses(); diff --git a/src/main/java/com/volmit/host/ModuleHost.java b/src/main/java/com/volmit/host/ModuleHost.java index de7f5882c..728606521 100644 --- a/src/main/java/com/volmit/host/ModuleHost.java +++ b/src/main/java/com/volmit/host/ModuleHost.java @@ -55,7 +55,6 @@ public record ModuleHost( HostTypes.of(hostTypePackages); requireFieldNames(reservedHostFields); requireFolderNames(reservedFolders); - requireJarFileNames(bundledJars); } /** The given built-in modules, exposing only the Iris API and reserving nothing. Jar copies go to @@ -102,10 +101,8 @@ public record ModuleHost( } /** This configuration also loading {@code jars}, module jars the host ships, with every pack, as if they were in its - * {@code modules} folder: before the pack's own jars, in the given order, in the same class loader. A pack jar with the - * same file name, such as an old copy, is left out, so a pack can never load a stale copy; {@link LoadedPack#replacedJars()} - * names it, for the host to tell the pack's owner. Their file names must differ and end in {@code .jar}; whether they - * exist is checked when a pack loads. + * {@code modules} folder: before the pack's own jars, in the given order, in the same class loader. Whether they exist + * is checked when a pack loads. */ public ModuleHost bundlingJars(Path... jars) { var bundled = new ArrayList<>(bundledJars); @@ -164,19 +161,6 @@ public record ModuleHost( } } - private static void requireJarFileNames(List jars) { - var names = new TreeSet(); - for (var jar : jars) { - var name = Objects.requireNonNull(jar, "bundled jar").getFileName(); - if (name == null || !name.toString().endsWith(PackModules.JAR_EXTENSION)) { - throw new IllegalArgumentException("Bundled module jar " + jar + " is not a .jar file"); - } - if (!names.add(name.toString())) { - throw new IllegalArgumentException("Two bundled module jars are named " + name); - } - } - } - private static Map> merge( Map> existing, Map> added) { diff --git a/src/main/java/com/volmit/host/PackModules.java b/src/main/java/com/volmit/host/PackModules.java index 8a7ad63b5..60f85d329 100644 --- a/src/main/java/com/volmit/host/PackModules.java +++ b/src/main/java/com/volmit/host/PackModules.java @@ -53,15 +53,13 @@ public final class PackModules implements AutoCloseable { private final List modules; private final List missingClasses; - private final List replacedJars; private final URLClassLoader loader; private final JarSnapshot snapshot; - private PackModules(List modules, List missingClasses, List replacedJars, - URLClassLoader loader, JarSnapshot snapshot) { + private PackModules( + List modules, List missingClasses, URLClassLoader loader, JarSnapshot snapshot) { this.modules = List.copyOf(modules); this.missingClasses = List.copyOf(missingClasses); - this.replacedJars = List.copyOf(replacedJars); this.loader = loader; this.snapshot = snapshot; } @@ -94,8 +92,7 @@ public final class PackModules implements AutoCloseable { } /** Like {@link #load(Path, Collection, Path, System.Logger)}, also loading the host's {@code bundledJars} as if they were - * in the pack, before its own jars and in the given order. A pack jar with a bundled jar's file name, such as an old copy - * of it, is left out, so the host's copy is the one that loads; {@link #replacedJars()} names it. + * in the pack, before its own jars and in the given order. */ static PackModules load(Path packRoot, Collection exposedHostPackages, List bundledJars, Path temporaryRoot, System.Logger logger) { @@ -103,12 +100,9 @@ public final class PackModules implements AutoCloseable { Objects.requireNonNull(logger, "logger"); var host = new ExposedHostClassLoader(IrisModule.class.getClassLoader(), ExposedPackages.apiAnd(exposedHostPackages)); StaleJarSnapshots.remove(temporaryRoot); - var packJars = findModuleJars(packRoot); - var replacedPackJars = packJarsNamedLike(packJars, bundledJars); - var snapshot = JarSnapshot.take(sourceJars(packRoot, bundledJars, packJars, replacedPackJars), temporaryRoot); - var replacedJars = replacedPackJars.stream().map(jar -> packRoot.relativize(jar).toString()).toList(); + var snapshot = JarSnapshot.take(sourceJars(packRoot, bundledJars, findModuleJars(packRoot)), temporaryRoot); try { - var packModules = loadSnapshot(packRoot, snapshot, replacedJars, host, logger); + var packModules = loadSnapshot(packRoot, snapshot, host, logger); logLoad(logger, packName(packRoot), packModules); return packModules; } catch (RuntimeException | Error failure) { @@ -130,11 +124,6 @@ public final class PackModules implements AutoCloseable { return missingClasses; } - /** The pack's own jars, relative to the pack, that a bundled jar of the same file name replaced, in file name order. */ - public List replacedJars() { - return replacedJars; - } - /** Closes the class loader, then deletes the jar copies, which an open loader would keep locked on Windows. */ @Override public void close() { @@ -152,11 +141,10 @@ public final class PackModules implements AutoCloseable { return Path.of(System.getProperty(TEMPORARY_DIRECTORY_PROPERTY)); } - private static PackModules loadSnapshot(Path packRoot, JarSnapshot snapshot, List replacedJars, - ExposedHostClassLoader host, System.Logger logger) { + private static PackModules loadSnapshot(Path packRoot, JarSnapshot snapshot, ExposedHostClassLoader host, System.Logger logger) { var jars = snapshot.jars().stream().map(ModuleJar::read).toList(); var missingClasses = PackClassCheck.requireLinkable(jars, host); - return defineModules(packRoot, jars, missingClasses, replacedJars, host, snapshot, logger); + return defineModules(packRoot, jars, missingClasses, host, snapshot, logger); } private static List findModuleJars(Path packRoot) { @@ -172,17 +160,9 @@ public final class PackModules implements AutoCloseable { } } - private static List packJarsNamedLike(List packJars, List bundledJars) { - var bundledNames = bundledJars.stream().map(PackModules::fileName).collect(Collectors.toSet()); - return packJars.stream().filter(jar -> bundledNames.contains(fileName(jar))).toList(); - } - - private static List sourceJars(Path packRoot, List bundledJars, List packJars, - List replacedPackJars) { + private static List sourceJars(Path packRoot, List bundledJars, List packJars) { var bundled = bundledJars.stream().map(jar -> new JarSnapshot.SourceJar(bundledOrigin(jar), jar)); - var own = packJars.stream() - .filter(Predicate.not(replacedPackJars::contains)) - .map(jar -> new JarSnapshot.SourceJar(packRoot.relativize(jar).toString(), jar)); + var own = packJars.stream().map(jar -> new JarSnapshot.SourceJar(packRoot.relativize(jar).toString(), jar)); return Stream.concat(bundled, own).toList(); } @@ -200,12 +180,12 @@ public final class PackModules implements AutoCloseable { } private static PackModules defineModules( - Path packRoot, List jars, List missingClasses, List replacedJars, ClassLoader host, - JarSnapshot snapshot, System.Logger logger) { + Path packRoot, List jars, List missingClasses, ClassLoader host, JarSnapshot snapshot, + System.Logger logger) { var loader = newPackClassLoader(packRoot, jars, host); try { var modules = createModules(jars, loader, snapshot, logger); - return new PackModules(modules, withoutDeclaredOptional(missingClasses, modules), replacedJars, loader, snapshot); + return new PackModules(modules, withoutDeclaredOptional(missingClasses, modules), loader, snapshot); } catch (RuntimeException | Error failure) { closeAfterFailure(loader, failure); throw failure; diff --git a/src/test/java/com/volmit/host/LoadedPackTest.java b/src/test/java/com/volmit/host/LoadedPackTest.java index 42588c47c..81c51ae05 100644 --- a/src/test/java/com/volmit/host/LoadedPackTest.java +++ b/src/test/java/com/volmit/host/LoadedPackTest.java @@ -137,29 +137,19 @@ class LoadedPackTest { loaded.packModules().stream().map(DiscoveredModule::origin).toList()); var order = loaded.placement().stages().stream().map(stage -> stage.stage().id().toString()).toList(); assertTrue(order.indexOf("bundled:core/mark") < order.indexOf("user:core/after"), order.toString()); - assertTrue(loaded.replacedJars().isEmpty()); } } @Test - void aPackJarNamedLikeABundledJarIsLeftOutAndNamed(@TempDir Path build) { - var bundledJar = module("host.bundled.BundledModule", "bundled:core", NO_MEMBERS, NO_STAGES).writeInto(build, "bundled.jar"); - module("pack.stale.StaleModule", "bundled:core", NO_MEMBERS, NO_STAGES).writeInto(pack, "bundled.jar"); + void aPackCopyOfABundledJarFailsNamingBothJars(@TempDir Path build) { + var bundled = module("host.bundled.BundledModule", "bundled:core", NO_MEMBERS, NO_STAGES); + var bundledJar = bundled.writeInto(build, "bundled.jar"); + bundled.writeInto(pack, "bundled.jar"); - try (var loaded = LoadedPack.load(pack, HOST.bundlingJars(bundledJar))) { - assertEquals(List.of("host.bundled.BundledModule"), - loaded.packModules().stream().map(found -> found.module().getClass().getName()).toList()); - assertEquals(List.of(Path.of("modules", "bundled.jar").toString()), loaded.replacedJars()); - } - } + var error = assertThrows(IllegalArgumentException.class, () -> LoadedPack.load(pack, HOST.bundlingJars(bundledJar))); - @Test - void bundledJarsNeedDistinctJarFileNames() { - var host = ModuleHost.of(List.of()); - - assertThrows(IllegalArgumentException.class, () -> host.bundlingJars(Path.of("notes.txt"))); - assertThrows(IllegalArgumentException.class, () -> host.bundlingJars(Path.of("one", "same.jar"), Path.of("two", "same.jar"))); - assertThrows(IllegalArgumentException.class, () -> host.bundlingJars(Path.of("one", "same.jar")).bundlingJars(Path.of("two", "same.jar"))); + assertTrue(error.getMessage().contains("Class host.bundled.BundledModule is in both module jars bundled.jar" + + PackModules.BUNDLED_ORIGIN_SUFFIX + " and " + Path.of("modules", "bundled.jar")), error.getMessage()); } /** Keeps the level and first line of every message, which is enough to see where the summary went. */ diff --git a/src/test/java/com/volmit/host/PackJarSnapshotTest.java b/src/test/java/com/volmit/host/PackJarSnapshotTest.java index 960e0c2bd..d8f336be4 100644 --- a/src/test/java/com/volmit/host/PackJarSnapshotTest.java +++ b/src/test/java/com/volmit/host/PackJarSnapshotTest.java @@ -57,7 +57,7 @@ class PackJarSnapshotTest { var module = packModules.modules().getFirst(); var location = Path.of(module.module().getClass().getProtectionDomain().getCodeSource().getLocation().toURI()); assertEquals(1, snapshotDirectories().size()); - assertEquals(snapshotDirectories().getFirst().resolve("gamma.jar"), location); + assertEquals(snapshotDirectories().getFirst().resolve("0-gamma.jar"), location); assertEquals(origin("gamma.jar"), module.origin()); Files.delete(original);