Load built-in modules only from the pack's own jar so an Iris update never changes a pack's modules

Iris no longer loads a bundled iris-builtins.jar with every pack. It carries the jar only to copy into new packs (/iris std create), so a pack and a world's copy of it keep the module versions they were made with. Built-in modules report Iris's version instead of "built-in", and /iris std modules lists every module with version, id and jar and takes the pack positionally.
This commit is contained in:
piexel committed 2026-11-11 08:28:12 +01:00
1 parent 620d59ad7a
commit e7d1ed24cb
54 files changed
+213 -341

No files matched your search

@@ -83,11 +83,6 @@ public final class DiscoveredModule {
return origin;
}
/** Whether the module ships with the host, on its classpath or in a jar it bundles, instead of in the pack. */
public boolean isBuiltIn() {
return source == Source.HOST || origin.endsWith(PackModules.BUNDLED_ORIGIN_SUFFIX);
}
/** The origin and the module's class, which tells apart two modules of one jar. */
String locationWithClass() {
return origin + " (" + module.getClass().getName() + ")";
@@ -36,7 +36,7 @@ public final class LoadedPack implements AutoCloseable {
*/
public static LoadedPack load(Path packRoot, ModuleHost host) {
Objects.requireNonNull(host, "host");
var packModules = PackModules.load(packRoot, host.exposedHostPackages(), host.bundledJars(), host.jarCopyRoot(),
var packModules = PackModules.load(packRoot, host.exposedHostPackages(), host.jarCopyRoot(),
host.logger(), host.platformApi());
try {
var loaded = LoadedModules.load(host.withPackModules(packModules.modules()),
@@ -48,9 +48,7 @@ public final class LoadedPack implements AutoCloseable {
}
}
/** The modules of the host's bundled jars, in the order the host lists them, then the pack's own, one per jar, in jar
* file name order.
*/
/** The modules of the pack's jars, in jar file name order; not the host's built-in modules. */
public List<DiscoveredModule> packModules() {
return packModules.modules();
}
@@ -14,7 +14,7 @@ import java.util.Optional;
import java.util.Set;
import java.util.TreeSet;
/** The host's side of every pack load, the same for all packs: its built-in modules, the module jars it bundles, the host
/** The host's side of every pack load, the same for all packs: its built-in modules, the host
* packages pack modules may use besides the Iris API, the JSON fields the host itself uses (which no
* settings key may take), the packages of the classes the host describes itself and the pack folders it
* reads itself (which no resource kind may read). Build it once and pass it to every
@@ -38,7 +38,6 @@ public record ModuleHost(
Map<Level, Set<String>> reservedHostFields,
Set<String> hostTypePackages,
Set<String> reservedFolders,
List<Path> bundledJars,
Path jarCopyRoot,
System.Logger logger,
Optional<PlatformApi> platformApi) {
@@ -50,7 +49,6 @@ public record ModuleHost(
reservedHostFields = copyOf(reservedHostFields);
hostTypePackages = Set.copyOf(hostTypePackages);
reservedFolders = Set.copyOf(reservedFolders);
bundledJars = List.copyOf(bundledJars);
Objects.requireNonNull(jarCopyRoot, "jarCopyRoot");
Objects.requireNonNull(logger, "logger");
Objects.requireNonNull(platformApi, "platformApi");
@@ -66,7 +64,7 @@ public record ModuleHost(
*/
public static ModuleHost of(List<? extends IrisModule> builtInModules) {
var builtIns = builtInModules.stream().map(DiscoveredModule::builtIn).toList();
return new ModuleHost(builtIns, Set.of(), Map.of(), Set.of(), Set.of(), List.of(), systemTemporaryDirectory(),
return new ModuleHost(builtIns, Set.of(), Map.of(), Set.of(), Set.of(), systemTemporaryDirectory(),
PackModules.DEFAULT_LOGGER, Optional.empty());
}
@@ -75,7 +73,7 @@ public record ModuleHost(
*/
public ModuleHost exposing(String... packages) {
return new ModuleHost(builtIns, union(exposedHostPackages, packages), reservedHostFields,
hostTypePackages, reservedFolders, bundledJars, jarCopyRoot, logger, platformApi);
hostTypePackages, reservedFolders,jarCopyRoot, logger, platformApi);
}
/** This configuration with {@code fields} also reserved as JSON fields the host itself uses,
@@ -83,7 +81,7 @@ public record ModuleHost(
*/
public ModuleHost reservingFields(Map<Level, Set<String>> fields) {
return new ModuleHost(builtIns, exposedHostPackages, merge(reservedHostFields, fields),
hostTypePackages, reservedFolders, bundledJars, jarCopyRoot, logger, platformApi);
hostTypePackages, reservedFolders,jarCopyRoot, logger, platformApi);
}
/** This configuration with the classes in {@code packages}, each with its subpackages, described by
@@ -97,7 +95,7 @@ public record ModuleHost(
@Deprecated(forRemoval = true)
public ModuleHost describingHostTypesIn(String... packages) {
return new ModuleHost(builtIns, exposedHostPackages, reservedHostFields,
union(hostTypePackages, packages), reservedFolders, bundledJars, jarCopyRoot, logger, platformApi);
union(hostTypePackages, packages), reservedFolders,jarCopyRoot, logger, platformApi);
}
/** This configuration with {@code folders}, folder names directly below a pack's root, also read by the host
@@ -105,18 +103,7 @@ public record ModuleHost(
*/
public ModuleHost reservingFolders(String... folders) {
return new ModuleHost(builtIns, exposedHostPackages, reservedHostFields, hostTypePackages,
union(reservedFolders, folders), bundledJars, jarCopyRoot, logger, platformApi);
}
/** 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. Whether they exist
* is checked when a pack loads.
*/
public ModuleHost bundlingJars(Path... jars) {
var bundled = new ArrayList<>(bundledJars);
bundled.addAll(Arrays.asList(jars));
return new ModuleHost(builtIns, exposedHostPackages, reservedHostFields, hostTypePackages, reservedFolders,
bundled, jarCopyRoot, logger, platformApi);
union(reservedFolders, folders), jarCopyRoot, logger, platformApi);
}
/** This configuration copying module jars into new directories below {@code directory}, such as a
@@ -124,7 +111,7 @@ public record ModuleHost(
*/
public ModuleHost copyingJarsTo(Path directory) {
return new ModuleHost(builtIns, exposedHostPackages, reservedHostFields, hostTypePackages,
reservedFolders, bundledJars, directory, logger, platformApi);
reservedFolders,directory, logger, platformApi);
}
/** This configuration logging each pack load's module summary and missing-classes warning, and every module's own
@@ -135,7 +122,7 @@ public record ModuleHost(
Objects.requireNonNull(logger, "logger");
var loggedBuiltIns = builtIns.stream().map(builtIn -> builtIn.loggingTo(logger)).toList();
return new ModuleHost(loggedBuiltIns, exposedHostPackages, reservedHostFields, hostTypePackages,
reservedFolders, bundledJars, jarCopyRoot, logger, platformApi);
reservedFolders,jarCopyRoot, logger, platformApi);
}
/** This configuration running on {@code platform}, whose api classes {@code platform.classes()} defines, such as the Iris
@@ -144,7 +131,7 @@ public record ModuleHost(
*/
public ModuleHost runningOn(PlatformApi platform) {
return new ModuleHost(builtIns, exposedHostPackages, reservedHostFields, hostTypePackages,
reservedFolders, bundledJars, jarCopyRoot, logger, Optional.of(platform));
reservedFolders,jarCopyRoot, logger, Optional.of(platform));
}
/** The built-in modules followed by a pack's modules, in the order {@link LoadedModules} sets them up. */
@@ -15,7 +15,6 @@ import java.util.Objects;
import java.util.Optional;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/** The module jars of one pack: every {@code <pack>/modules/*.jar}, each with the modules it lists,
* ordered by file name so the result never depends on directory listing order. Hosts normally load packs
@@ -50,7 +49,6 @@ import java.util.stream.Stream;
public final class PackModules implements AutoCloseable {
public static final String MODULES_DIRECTORY = "modules";
static final String JAR_EXTENSION = ".jar";
static final String BUNDLED_ORIGIN_SUFFIX = " (bundled with the host)";
private static final String TEMPORARY_DIRECTORY_PROPERTY = "java.io.tmpdir";
static final System.Logger DEFAULT_LOGGER = System.getLogger(PackModules.class.getName());
@@ -98,28 +96,20 @@ public final class PackModules implements AutoCloseable {
/** Like {@link #load(Path, Collection)}, copying the jars below {@code temporaryRoot} and logging to {@code logger}. */
static PackModules load(Path packRoot, Collection<String> exposedHostPackages, Path temporaryRoot, System.Logger logger) {
return load(packRoot, exposedHostPackages, List.of(), temporaryRoot, logger);
return load(packRoot, exposedHostPackages, temporaryRoot, logger, Optional.empty());
}
/** 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.
*/
static PackModules load(Path packRoot, Collection<String> exposedHostPackages, List<Path> bundledJars, Path temporaryRoot,
System.Logger logger) {
return load(packRoot, exposedHostPackages, bundledJars, temporaryRoot, logger, Optional.empty());
}
/** Like {@link #load(Path, Collection, List, Path, System.Logger)} on a host that runs on {@code runningPlatform}, if
/** Like {@link #load(Path, Collection, Path, System.Logger)} on a host that runs on {@code runningPlatform}, if
* any: the pack's tooling parts of that platform are checked at load and can be opened ({@link #openToolingPart}).
*/
static PackModules load(Path packRoot, Collection<String> exposedHostPackages, List<Path> bundledJars, Path temporaryRoot,
static PackModules load(Path packRoot, Collection<String> exposedHostPackages, Path temporaryRoot,
System.Logger logger, Optional<PlatformApi> runningPlatform) {
Objects.requireNonNull(packRoot, "packRoot");
Objects.requireNonNull(logger, "logger");
Objects.requireNonNull(runningPlatform, "runningPlatform");
var host = new ExposedHostClassLoader(IrisModule.class.getClassLoader(), ExposedPackages.apiAnd(exposedHostPackages));
StaleJarSnapshots.remove(temporaryRoot);
var snapshot = JarSnapshot.take(sourceJars(packRoot, bundledJars, findModuleJars(packRoot)), temporaryRoot);
var snapshot = JarSnapshot.take(sourceJars(packRoot, findModuleJars(packRoot)), temporaryRoot);
try {
var packModules = loadSnapshot(packRoot, snapshot, host, logger, runningPlatform);
logLoad(logger, packName(packRoot), packModules);
@@ -201,19 +191,8 @@ public final class PackModules implements AutoCloseable {
}
}
private static List<JarSnapshot.SourceJar> sourceJars(Path packRoot, List<Path> bundledJars, List<Path> packJars) {
var bundled = bundledJars.stream().map(jar -> new JarSnapshot.SourceJar(bundledOrigin(jar), jar));
var own = packJars.stream().map(jar -> new JarSnapshot.SourceJar(packRoot.relativize(jar).toString(), jar));
return Stream.concat(bundled, own).toList();
}
/** How messages name a bundled jar: its file name, marked as the host's. */
static String bundledOrigin(Path bundledJar) {
return fileName(bundledJar) + BUNDLED_ORIGIN_SUFFIX;
}
private static String fileName(Path jar) {
return jar.getFileName().toString();
private static List<JarSnapshot.SourceJar> sourceJars(Path packRoot, List<Path> packJars) {
return packJars.stream().map(jar -> new JarSnapshot.SourceJar(packRoot.relativize(jar).toString(), jar)).toList();
}
private static boolean isJar(Path path) {
@@ -1,16 +1,10 @@
package com.volmit.host;
import java.util.ArrayList;
import java.util.List;
/** The one log message a pack load writes: which modules the pack uses, grouped by whether the host
* or the pack brings them. The host's modules are named only; the pack's own also carry their
* version and jar.
*/
/** The one log message a pack load writes: the modules of the pack's jars, each with its version and jar. */
final class PackModulesSummary {
private static final String INDENT = " ";
private static final String HOST_LABEL = "Host: ";
private static final String PACK_LABEL = "Pack: ";
private static final String ENTRY_SEPARATOR = ",";
private static final int MAX_LINE_WIDTH = 100;
@@ -20,39 +14,25 @@ final class PackModulesSummary {
if (modules.isEmpty()) {
return "Pack " + packName + " uses no modules: no jars in its " + PackModules.MODULES_DIRECTORY + " directory";
}
List<String> hostModules = new ArrayList<>();
List<String> ownModules = new ArrayList<>();
for (var module : modules) {
if (module.isBuiltIn()) {
hostModules.add(module.info().name());
} else {
ownModules.add(describeOwn(module));
}
}
var text = new StringBuilder("Pack ").append(packName).append(" uses ").append(modules.size())
.append(modules.size() == 1 ? " module:" : " modules:");
appendGroup(text, HOST_LABEL, hostModules);
appendGroup(text, PACK_LABEL, ownModules);
appendWrapped(text, modules.stream().map(PackModulesSummary::describe).toList());
return text.toString();
}
private static String describeOwn(DiscoveredModule discovered) {
private static String describe(DiscoveredModule discovered) {
var info = discovered.info();
return info.name() + " " + info.version() + " (" + discovered.origin() + ")";
}
private static void appendGroup(StringBuilder text, String label, List<String> entries) {
if (entries.isEmpty()) {
return;
}
String continuation = INDENT + " ".repeat(label.length());
var line = new StringBuilder(INDENT).append(label);
private static void appendWrapped(StringBuilder text, List<String> entries) {
var line = new StringBuilder(INDENT);
int entriesOnLine = 0;
for (int index = 0; index < entries.size(); index++) {
String entry = entries.get(index) + (index < entries.size() - 1 ? ENTRY_SEPARATOR : "");
if (entriesOnLine > 0 && line.length() + 1 + entry.length() > MAX_LINE_WIDTH) {
text.append('\n').append(line);
line = new StringBuilder(continuation);
line = new StringBuilder(INDENT);
entriesOnLine = 0;
}
if (entriesOnLine > 0) {
@@ -123,37 +123,6 @@ class LoadedPackTest {
assertEquals(List.of("INFO Pack " + pack.getFileName() + " uses 1 module:"), log.levelsAndFirstLines());
}
@Test
void bundledJarsLoadWithEveryPackBeforeItsOwnJarsInItsClassLoader(@TempDir Path build) {
var bundledJar = module("host.bundled.BundledModule", "bundled:core",
"public static final PlacementStage<Void> MARK = Stage.placement(\"bundled:core/mark\");",
"graph.define(MARK).run(context -> {});")
.writeInto(build, "bundled.jar");
module("pack.user.User", "user:core", NO_MEMBERS,
"graph.define(Stage.placement(\"user:core/after\")).requires(host.bundled.BundledModule.MARK).run(context -> {});")
.compileAgainst(bundledJar)
.writeInto(pack, "user.jar");
try (var loaded = LoadedPack.load(pack, HOST.bundlingJars(bundledJar))) {
assertEquals(List.of("bundled.jar" + PackModules.BUNDLED_ORIGIN_SUFFIX, Path.of("modules", "user.jar").toString()),
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());
}
}
@Test
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");
var error = assertThrows(ModuleLoadException.class, () -> LoadedPack.load(pack, HOST.bundlingJars(bundledJar)));
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. */
private static final class RecordingLogger implements System.Logger {
private final List<String> lines = new java.util.ArrayList<>();
@@ -14,56 +14,27 @@ class PackModulesSummaryTest {
return DiscoveredModule.fromJar(module, module.info(), origin);
}
private static DiscoveredModule bundled(IrisModule module) {
return pack(module, "builtins.jar" + PackModules.BUNDLED_ORIGIN_SUFFIX);
}
@Test
void listsTheVersionAndJarOfEveryModuleThePackBringsInLoadOrder() {
void listsTheVersionAndJarOfEveryModuleInLoadOrder() {
var modules = List.of(
pack(new MossModule(), "modules/moss.jar"),
pack(new SnowModule(), "modules/snow.jar"));
assertEquals("""
Pack overworld uses 2 modules:
Pack: Moss 1.0.0 (modules/moss.jar), Snow test (modules/snow.jar)""",
Moss 1.0.0 (modules/moss.jar), Snow test (modules/snow.jar)""",
PackModulesSummary.describe("overworld", modules));
}
@Test
void namesHostModulesWithoutTheirJar() {
var modules = List.of(
bundled(new SnowModule()),
DiscoveredModule.builtIn(new MossModule()));
assertEquals("""
Pack overworld uses 2 modules:
Host: Snow, Moss""",
PackModulesSummary.describe("overworld", modules));
}
@Test
void groupsHostModulesBeforeThePacksOwn() {
var modules = List.of(
pack(new MossModule(), "modules/moss.jar"),
bundled(new SnowModule()));
assertEquals("""
Pack overworld uses 2 modules:
Host: Snow
Pack: Moss 1.0.0 (modules/moss.jar)""",
PackModulesSummary.describe("overworld", modules));
}
@Test
void wrapsALongGroupUnderItsFirstEntry() {
var modules = Collections.nCopies(20, bundled(new SnowModule()));
void wrapsALongListAtTheSameIndent() {
var modules = Collections.nCopies(20, pack(new SnowModule(), "modules/snow.jar"));
String[] lines = PackModulesSummary.describe("overworld", modules).split("\n");
assertTrue(lines.length > 2);
assertTrue(lines[1].startsWith(" Host: Snow, Snow"));
assertTrue(lines[2].startsWith(" Snow"));
assertTrue(lines[1].startsWith(" Snow test (modules/snow.jar), Snow"));
assertTrue(lines[2].startsWith(" Snow"));
for (String line : lines) {
assertTrue(line.length() <= 100, line);
}
@@ -75,7 +46,7 @@ class PackModulesSummaryTest {
assertEquals("""
Pack overworld uses 1 module:
Pack: Moss 1.0.0 (modules/moss.jar)""",
Moss 1.0.0 (modules/moss.jar)""",
PackModulesSummary.describe("overworld", modules));
}
@@ -54,7 +54,7 @@ class ToolingPartCheckTest {
private PackModules load(TestModuleJar jar, Optional<PlatformApi> runningPlatform) {
jar.writeInto(pack, "user.jar");
return PackModules.load(pack, List.of(), List.of(), work, PackModules.DEFAULT_LOGGER, runningPlatform);
return PackModules.load(pack, List.of(), work, PackModules.DEFAULT_LOGGER, runningPlatform);
}
private String loadFailure(TestModuleJar jar, Optional<PlatformApi> runningPlatform) {
@@ -158,7 +158,7 @@ class ToolingPartCheckTest {
partUsingTheBuiltInModule().writeInto(pack, "user.jar");
var exposed = List.of(CarveModule.class.getPackageName());
try (var modules = PackModules.load(pack, exposed, List.of(), work, PackModules.DEFAULT_LOGGER, Optional.of(platformDenyingExample()))) {
try (var modules = PackModules.load(pack, exposed, work, PackModules.DEFAULT_LOGGER, Optional.of(platformDenyingExample()))) {
assertEquals(1, modules.toolingParts().size());
}
}
@@ -45,7 +45,7 @@ class ToolingPartLoaderTest {
.compileAgainst(platform.jar())
.toolingParts("bukkit=" + PART)
.writeInto(pack, "user.jar");
modules = PackModules.load(pack, List.of(), List.of(), work, PackModules.DEFAULT_LOGGER,
modules = PackModules.load(pack, List.of(), work, PackModules.DEFAULT_LOGGER,
Optional.of(platform.asApiOf(ToolingPlatform.BUKKIT)));
}