Skip to content

Commit f488c63

Browse files
committed
Don't allow --limit-modules with ALL-MODULE-PATH
1 parent 4135dcb commit f488c63

File tree

3 files changed

+22
-41
lines changed

3 files changed

+22
-41
lines changed

src/jdk.jlink/share/classes/jdk/tools/jlink/internal/JlinkTask.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,10 @@ private JlinkConfiguration initJlinkConfig() throws BadArgs {
417417
Set<String> roots = new HashSet<>();
418418
for (String mod : options.addMods) {
419419
if (mod.equals(ALL_MODULE_PATH)) {
420+
// Using --limit-modules with ALL-MODULE-PATH is an error
421+
if (!options.limitMods.isEmpty()) {
422+
throw taskHelper.newBadArgs("err.limit.modules");
423+
}
420424
// all observable modules in the app module path are roots
421425
Set<String> initialRoots = appModuleFinder.findAll()
422426
.stream()
@@ -436,11 +440,8 @@ private JlinkConfiguration initJlinkConfig() throws BadArgs {
436440
// by initialRoots, to find the observable modules from the
437441
// application module path (--module-path option) only. We must
438442
// not include JDK modules from the default module path or the
439-
// run-time image. Only do this if no --limit-modules has been
440-
// specified to begin with.
441-
ModuleFinder mf = limitFinder(finder,
442-
options.limitMods.isEmpty() ? initialRoots : options.limitMods,
443-
Set.of());
443+
// run-time image.
444+
ModuleFinder mf = limitFinder(finder, initialRoots, Set.of());
444445
mf.findAll()
445446
.stream()
446447
.map(ModuleReference::descriptor)

src/jdk.jlink/share/classes/jdk/tools/jlink/resources/jlink.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ err.runtime.link.patched.module=jlink does not support linking from the run-time
129129
\ when running on a patched runtime with --patch-module
130130
err.no.module.path=--module-path option must be specified with --add-modules ALL-MODULE-PATH
131131
err.empty.module.path=No module found in module path ''{0}'' with --add-modules ALL-MODULE-PATH
132+
err.limit.modules=--limit-modules not allowed with --add-modules ALL-MODULE-PATH
132133
err.jlink.version.mismatch=jlink version {0}.{1} does not match target java.base version {2}.{3}
133134
err.automatic.module:automatic module cannot be used with jlink: {0} from {1}
134135
err.unknown.byte.order:unknown byte order {0}

test/jdk/tools/jlink/basic/AllModulePath.java

Lines changed: 15 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@
4141
import org.testng.annotations.Test;
4242

4343
import jdk.test.lib.compiler.CompilerUtils;
44-
import jdk.test.lib.process.OutputAnalyzer;
4544
import jdk.test.lib.process.ProcessTools;
4645
import jdk.tools.jlink.internal.LinkableRuntimeImage;
4746
import tests.Helper;
@@ -126,30 +125,29 @@ public void testAllModulePath() throws Throwable {
126125
}
127126

128127
/*
129-
* --add-modules ALL-MODULE-PATH with an existing module path and module
130-
* limits applied. Module limit on module from module path.
128+
* Since ALL-MODULE-PATH does not allow --limit-modules. Add a test that
129+
* includes just a single module from the module path. This is just for
130+
* completeness and shows the intended replacement.
131131
*/
132132
@Test
133-
public void testLimitModules() throws Throwable {
133+
public void testSubsetModules() throws Throwable {
134134
if (isExplodedJDKImage()) {
135135
return;
136136
}
137137

138138
// create custom image
139139
Path image = HELPER.createNewImageDir("image1");
140+
// Instead of --add-modules ALL-MODULE-PATH [...] --limit-modules m1 do:
140141
List<String> opts = List.of("--module-path", MODS.toString(),
141142
"--output", image.toString(),
142-
"--add-modules", "ALL-MODULE-PATH",
143-
"--limit-modules", "m1");
143+
"--add-modules", "m1");
144144
createImage(image, opts, true /* success */);
145145

146146
checkModules(image, Set.of("m1", "java.base"));
147147
}
148148

149149
/*
150150
* --add-modules *includes* ALL-MODULE-PATH with an existing module path
151-
* and module limits applied. Module limit on a dependency, but custom
152-
* modules explicitly listed (therefore, expect inclusion of them).
153151
*/
154152
@Test
155153
public void testAddModules() throws Throwable {
@@ -162,8 +160,7 @@ public void testAddModules() throws Throwable {
162160
List<String> opts = List.of("--module-path", MODS.toString(),
163161
"--output", image.toString(),
164162
"--add-modules", "m1,test",
165-
"--add-modules", "ALL-MODULE-PATH",
166-
"--limit-modules", "java.base");
163+
"--add-modules", "ALL-MODULE-PATH");
167164
createImage(image, opts, true /* success */);
168165

169166
checkModules(image, Set.of("m1", "test", "java.base"));
@@ -197,7 +194,7 @@ public void modulePathEmpty() throws IOException {
197194
String strNotExists = "not-exist";
198195
Path notExists = Path.of(strNotExists);
199196
if (Files.exists(notExists)) {
200-
throw new RuntimeException("Test setup error, path must not exist!");
197+
throw new AssertionError("Test setup error, path must not exist!");
201198
}
202199
List<String> allArgs = List.of("--add-modules", "ALL-MODULE-PATH",
203200
"--module-path", notExists.toString(),
@@ -210,13 +207,10 @@ public void modulePathEmpty() throws IOException {
210207
}
211208

212209
/*
213-
* --add-modules ALL-MODULE-PATH with an existing module path and module
214-
* limits applied. This case tests a module limit on a dependency, jdk.jfr,
215-
* and *doesn't* list the module explicitly in --add-modules. Therefore,
216-
* expects for the module - on the module path - to not be present.
210+
* --add-modules ALL-MODULE-PATH with --limit-modules is an error
217211
*/
218212
@Test
219-
public void modulePathWithLimitMods() throws Exception {
213+
public void testLimitModules() throws Exception {
220214
if (isExplodedJDKImage()) {
221215
return;
222216
}
@@ -225,14 +219,13 @@ public void modulePathWithLimitMods() throws Exception {
225219
Result result = HELPER.generateDefaultJModule(moduleName, "jdk.jfr");
226220
Path customModulePath = result.getFile().getParent();
227221
List<String> allArgs = List.of("--add-modules", "ALL-MODULE-PATH",
228-
"--limit-modules", "jdk.jfr", // A dependency of com.baz.runtime
222+
"--limit-modules", "jdk.jfr",
229223
"--module-path", customModulePath.toString(),
230224
"--output", targetPath.toString());
231-
JlinkOutput allOut = createImage(targetPath, allArgs, true /* success */);
232-
assertTrue(allOut.stdout.isEmpty());
233-
assertTrue(allOut.stderr.isEmpty());
234-
Set<String> expected = Set.of("java.base", "jdk.jfr");
235-
verifyListModules(targetPath, expected);
225+
JlinkOutput allOut = createImage(targetPath, allArgs, false /* success */);
226+
String actual = allOut.stdout.trim();
227+
String expected = "Error: --limit-modules not allowed with --add-modules ALL-MODULE-PATH";
228+
assertEquals(actual, expected);
236229
}
237230

238231
/*
@@ -252,20 +245,6 @@ private void checkModules(Path image, Set<String> modules) throws Throwable {
252245
.shouldHaveExitValue(0);
253246
}
254247

255-
/*
256-
* Verify linked modules using java --list-modules
257-
*/
258-
private void verifyListModules(Path targetPath, Set<String> expected) throws Exception {
259-
Path java = findTool(targetPath, "java");
260-
List<String> listMods = List.of(java.toString(), "--list-modules");
261-
OutputAnalyzer out = ProcessTools.executeCommand(listMods.toArray(new String[] {}))
262-
.shouldHaveExitValue(0);
263-
Set<String> actual = out.asLines().stream()
264-
.map(s -> { return s.split("@")[0]; })
265-
.collect(Collectors.toSet());
266-
assertEquals(actual, expected);
267-
}
268-
269248
private Path findTool(Path image, String tool) {
270249
String suffix = System.getProperty("os.name").startsWith("Windows")
271250
? ".exe" : "";

0 commit comments

Comments
 (0)