Skip to content

Commit b384472

Browse files
committed
fix(maven): clarify requirements.txt vs packages handling and lock file checks
- Clarify behavior between <packages> and requirements.txt dependency modes - Avoid possible NullPointerExceptions when requirements.txt is not used - Validate lock file usage when no Python dependencies are configured - Make dependency mode selection explicit and deterministic
1 parent c025031 commit b384472

File tree

3 files changed

+40
-23
lines changed

3 files changed

+40
-23
lines changed

graalpy-maven-plugin/src/main/java/org/graalvm/python/maven/plugin/InstallPackagesMojo.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
*/
4141
package org.graalvm.python.maven.plugin;
4242

43+
import java.nio.file.Files;
4344
import org.apache.maven.plugin.MojoExecutionException;
4445
import org.apache.maven.plugins.annotations.LifecyclePhase;
4546
import org.apache.maven.plugins.annotations.Mojo;
@@ -100,6 +101,18 @@ private void manageVenv() throws MojoExecutionException {
100101
MavenDelegateLog log = new MavenDelegateLog(getLog());
101102
Path lockFile = getLockFile();
102103
Path reqFile = resolveReqFile();
104+
105+
boolean emptyPackages = packages == null || packages.isEmpty();
106+
boolean hasReqFile = reqFile != null && Files.exists(reqFile);
107+
boolean hasLockFile = lockFile != null && Files.exists(lockFile);
108+
109+
if (emptyPackages && !hasReqFile) {
110+
if (hasLockFile) {
111+
throw new MojoExecutionException(
112+
"Lock file is present, but no Python packages or requirements.txt are configured.");
113+
}
114+
return;
115+
}
103116
try {
104117
VFSUtils.createVenv(venvDirectory, packages, lockFile, MISSING_LOCK_FILE_WARNING, createLauncher(),
105118
getGraalPyVersion(project), log, reqFile);

graalpy-maven-plugin/src/main/java/org/graalvm/python/maven/plugin/LockPackagesMojo.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public class LockPackagesMojo extends AbstractGraalPyMojo {
6565
based on the packages defined in the plugin configuration and their dependencies.
6666
""";
6767
public static final String MISSING_DEPENDENCY_CONFIGURATION_ERROR = """
68-
In order to run the lock-packages goal you must declare Python dependencies in the graalpy-maven-plugin configuration.
68+
In order to run the lock-packages goal there have to be python packages declared in the graalpy-maven-plugin configuration.
6969
7070
You must configure Python dependencies in one of the following ways:
7171
@@ -140,12 +140,11 @@ private void checkEmptyPackages() throws MojoExecutionException {
140140
boolean requirementsExists = reqFilePath != null;
141141
// Disallow lock-packages when no packages OR when requirementsFile is used
142142
if (emptyPackages || requirementsExists) {
143-
getLog().error("");
144-
getLog().error("Missing Python dependency configuration.");
145143
getLog().error("");
146144
getLog().error(MISSING_DEPENDENCY_CONFIGURATION_ERROR);
147145
getLog().error("");
148-
throw new MojoExecutionException("Missing Python dependency configuration.");
146+
throw new MojoExecutionException("In order to run the lock-packages goal there have to be python packages "
147+
+ "declared in the graalpy-maven-plugin configuration");
149148
}
150149
}
151150
}

org.graalvm.python.embedding.tools/src/main/java/org/graalvm/python/embedding/tools/vfs/VFSUtils.java

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -543,38 +543,43 @@ public static void createVenv(Path venvDirectory, List<String> packages, Path lo
543543
Objects.requireNonNull(graalPyVersion);
544544
Objects.requireNonNull(log);
545545

546-
if (installPackagesFromReqFile(venvDirectory, launcher, graalPyVersion, log, reqFile)) {
546+
if (installPackagesFromReqFile(venvDirectory, launcher, graalPyVersion, log, lockFilePath, reqFile)) {
547547
return;
548548
}
549549

550550
installPackages(venvDirectory, packages, lockFilePath, missingLockFileWarning, launcher, graalPyVersion, log);
551551
}
552552

553553
static boolean installPackagesFromReqFile(Path venvDirectory, Launcher launcher, String graalPyVersion,
554-
BuildToolLog log, Path reqFile) throws IOException {
555-
if (reqFile != null) {
556-
log.info("Using <requirements.txt> dependency mode.");
557-
log.info("Installing Python dependencies from: " + reqFile);
554+
BuildToolLog log, Path lockFilePath, Path reqFile) throws IOException {
555+
if (reqFile == null) {
556+
return false;
557+
}
558+
if (!Files.exists(reqFile)) {
559+
return false;
560+
}
561+
log.info("Using <requirements.txt> dependency mode.");
562+
log.info("Installing Python dependencies from: " + reqFile);
558563

559-
warnIfLockFileExists(venvDirectory, log);
564+
warnIfLockFileExists(lockFilePath, log);
560565

561-
VenvContents vc = ensureVenv(venvDirectory, graalPyVersion, launcher, log);
566+
VenvContents vc = ensureVenv(venvDirectory, graalPyVersion, launcher, log);
562567

563-
runPip(venvDirectory, "install", log, "--compile", "-r", reqFile.toString());
564-
List<String> reqPackages = requirementsPackages(reqFile);
565-
vc.write(reqPackages);
568+
runPip(venvDirectory, "install", log, "--compile", "-r", reqFile.toString());
569+
List<String> reqPackages = requirementsPackages(reqFile);
570+
vc.write(reqPackages);
566571

567-
InstalledPackages installed = InstalledPackages.fromVenv(venvDirectory);
568-
installed.freeze(log);
572+
InstalledPackages installed = InstalledPackages.fromVenv(venvDirectory);
573+
installed.freeze(log);
569574

570-
return true;
571-
}
572-
return false;
575+
return true;
573576
}
574577

575-
private static void warnIfLockFileExists(Path venvDirectory, BuildToolLog log) {
576-
Path lockFile = venvDirectory.resolve("graalpy.lock");
577-
if (Files.exists(lockFile)) {
578+
private static void warnIfLockFileExists(Path lockFilePath, BuildToolLog log) {
579+
if (lockFilePath == null) {
580+
return;
581+
}
582+
if (Files.exists(lockFilePath)) {
578583
log.warning("Lock file is ignored in <requirements.txt> mode.");
579584
}
580585
}
@@ -589,7 +594,7 @@ static void installPackages(Path venvDirectory, List<String> packages, Path lock
589594

590595
List<String> pluginPackages = trim(packages);
591596

592-
LockFile lockFile = null;
597+
VFSUtils.LockFile lockFile = null;
593598
if (lockFilePath != null && Files.exists(lockFilePath)) {
594599
log.info("Lock-file detected: " + lockFilePath);
595600
lockFile = LockFile.fromFile(lockFilePath, log);

0 commit comments

Comments
 (0)