Skip to content

Commit 08ae17b

Browse files
authored
Merge pull request #508 from Achal1607/add-cwd-notebook
Set user.dir for JShell remote VM to notebook's parent directory
2 parents 71d899a + dd287f7 commit 08ae17b

File tree

1 file changed

+22
-3
lines changed

1 file changed

+22
-3
lines changed

nbcode/notebooks/src/org/netbeans/modules/nbcode/java/notebook/NotebookSessionManager.java

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
import com.google.gson.JsonElement;
1919
import com.google.gson.JsonObject;
2020
import java.net.URI;
21+
import java.nio.file.FileSystemNotFoundException;
22+
import java.nio.file.Files;
2123
import java.nio.file.Path;
2224
import java.nio.file.Paths;
2325
import java.util.ArrayList;
@@ -56,6 +58,7 @@ public class NotebookSessionManager {
5658
private static final String CLASS_PATH = "--class-path";
5759
private static final String MODULE_PATH = "--module-path";
5860
private static final String ADD_MODULES = "--add-modules";
61+
private static final String USER_DIR_PROP = "-Duser.dir=";
5962

6063
private final Map<String, CompletableFuture<JShell>> sessions = new ConcurrentHashMap<>();
6164
private final Map<String, JshellStreamsHandler> jshellStreamsMap = new ConcurrentHashMap<>();
@@ -80,16 +83,17 @@ private CompletableFuture<JShell> jshellBuilder(String notebookUri, JshellStream
8083
if (prj != null) {
8184
notebookPrjMap.put(notebookUri, new ProjectContextInfo(prj));
8285
}
83-
return jshellBuildWithProject(prj, streamsHandler);
86+
return jshellBuildWithProject(notebookUri, prj, streamsHandler);
8487
})).exceptionally(throwable -> {
8588
LOG.log(Level.WARNING, "Failed to get project context, using default JShell configuration", throwable);
86-
return jshellBuildWithProject(null, streamsHandler);
89+
return jshellBuildWithProject(notebookUri, null, streamsHandler);
8790
});
8891
}
8992

90-
private JShell jshellBuildWithProject(Project prj, JshellStreamsHandler streamsHandler) {
93+
private JShell jshellBuildWithProject(String notebookUri, Project prj, JshellStreamsHandler streamsHandler) {
9194
List<String> compilerOptions = getCompilerOptions(prj);
9295
List<String> remoteOptions = getRemoteVmOptions(prj);
96+
setSystemPropertiesForRemoteVm(remoteOptions, notebookUri);
9397

9498
JShell.Builder builder = JShell.builder()
9599
.out(streamsHandler.getPrintOutStream())
@@ -99,11 +103,26 @@ private JShell jshellBuildWithProject(Project prj, JshellStreamsHandler streamsH
99103
if (!compilerOptions.isEmpty()) {
100104
builder.compilerOptions(compilerOptions.toArray(new String[0]))
101105
.remoteVMOptions(remoteOptions.toArray(new String[0]));
106+
} else if (!remoteOptions.isEmpty()) {
107+
builder.remoteVMOptions(remoteOptions.toArray(new String[0]));
102108
}
103109

104110
return builder.build();
105111
}
106112

113+
private void setSystemPropertiesForRemoteVm(List<String> remoteOptions, String notebookUri) {
114+
try {
115+
URI uri = URI.create(notebookUri);
116+
Path parentPath = Path.of(uri).getParent();
117+
if (parentPath != null && Files.isDirectory(parentPath)) {
118+
remoteOptions.add(USER_DIR_PROP + parentPath.toString());
119+
LOG.log(Level.FINE, "Setting user.dir for JShell: {0}", parentPath);
120+
}
121+
} catch (IllegalArgumentException | FileSystemNotFoundException e) {
122+
LOG.log(Level.WARNING, "Could not parse notebook URI to set user.dir: " + notebookUri, e);
123+
}
124+
}
125+
107126
public CompletableFuture<JShell> createSession(NotebookDocument notebookDoc) {
108127
String notebookId = notebookDoc.getUri();
109128
return createSession(notebookId);

0 commit comments

Comments
 (0)