Skip to content

Commit 78d3500

Browse files
Python: Isolate openrewrite package by version and add recipeInstallDir to Builder (#6824)
When multiple CLI versions share the same home directory, they conflict over the openrewrite Python package. This change resolves that by: 1. Resolving a version-specific subdirectory under pipPackagesPath (e.g., `<basePath>/0.5.3/`) so different openrewrite versions don't overwrite each other. Two CLI versions shipping the same openrewrite package version share the directory. 2. Adding a `recipeInstallDir` builder method for a separate shared directory containing user-installed recipe packages (added to PYTHONPATH alongside the version-specific openrewrite directory).
1 parent a72ba76 commit 78d3500

1 file changed

Lines changed: 26 additions & 3 deletions

File tree

rewrite-python/src/main/java/org/openrewrite/python/rpc/PythonRewriteRpc.java

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,7 @@ public static class Builder implements Supplier<PythonRewriteRpc> {
379379
private Path pythonPath = findDefaultPythonPath();
380380
private @Nullable Path log;
381381
private @Nullable Path pipPackagesPath;
382+
private @Nullable Path recipeInstallDir;
382383

383384
private static Path findDefaultPythonPath() {
384385
// Try to find a venv in the project structure
@@ -501,6 +502,19 @@ public Builder pipPackagesPath(@Nullable Path pipPackagesPath) {
501502
return this;
502503
}
503504

505+
/**
506+
* Set the directory where user-installed recipe packages live.
507+
* This directory is added to PYTHONPATH so the RPC server can
508+
* find recipe packages installed via pip.
509+
*
510+
* @param recipeInstallDir The directory containing recipe pip packages
511+
* @return This builder
512+
*/
513+
public Builder recipeInstallDir(@Nullable Path recipeInstallDir) {
514+
this.recipeInstallDir = recipeInstallDir;
515+
return this;
516+
}
517+
504518
/**
505519
* Set the Python language version to parse.
506520
* <p>
@@ -531,8 +545,12 @@ public PythonRewriteRpc get() {
531545
boolean interpreterHasRewrite = isDevBuild && pipPackagesPath != null && canImportRewrite(pythonPath);
532546
boolean usePipPackagesPath = pipPackagesPath != null && !interpreterHasRewrite;
533547

548+
// Resolve version-specific subdirectory under pipPackagesPath
549+
Path resolvedPipPackagesPath = null;
534550
if (usePipPackagesPath) {
535-
bootstrapOpenrewrite(pipPackagesPath);
551+
String versionDir = isDevBuild ? "dev" : version;
552+
resolvedPipPackagesPath = pipPackagesPath.resolve(versionDir);
553+
bootstrapOpenrewrite(resolvedPipPackagesPath);
536554
}
537555

538556
Stream<@Nullable String> cmd;
@@ -562,8 +580,13 @@ public PythonRewriteRpc get() {
562580
List<String> pythonPathParts = new ArrayList<>();
563581

564582
// Add pip packages path if the interpreter doesn't already have rewrite
565-
if (usePipPackagesPath) {
566-
pythonPathParts.add(pipPackagesPath.toAbsolutePath().normalize().toString());
583+
if (resolvedPipPackagesPath != null) {
584+
pythonPathParts.add(resolvedPipPackagesPath.toAbsolutePath().normalize().toString());
585+
}
586+
587+
// Add recipe install directory to PYTHONPATH
588+
if (recipeInstallDir != null) {
589+
pythonPathParts.add(recipeInstallDir.toAbsolutePath().normalize().toString());
567590
}
568591

569592
// If debug source path is set, use it

0 commit comments

Comments
 (0)