You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@netbeans.apache.org by GitBox <gi...@apache.org> on 2021/09/21 11:22:38 UTC

[GitHub] [netbeans] sdedic commented on a change in pull request #3180: Support of setting the current working directory and environment variables.

sdedic commented on a change in pull request #3180:
URL: https://github.com/apache/netbeans/pull/3180#discussion_r712932022



##########
File path: java/java.lsp.server/src/org/netbeans/modules/java/lsp/server/debugging/launch/NbLaunchDelegate.java
##########
@@ -344,19 +338,54 @@ public void propertyChange(PropertyChangeEvent evt) {
                     notifyFinished(context, exitCode != null && exitCode == 0);
                 });
                 Lookups.executeWith(execLookup, () -> {
-                    execNative(nativeImageFile, args, context, ed, launchFuture);
+                    execNative(nativeImageFile, args, context, ed, params, launchFuture);
                 });
             }
         }
         return launchFuture;
     }
 
-    private static void execNative(File nativeImageFile, List<String> args, DebugAdapterContext context, ExecutionDescriptor executionDescriptor, CompletableFuture<Void> launchFuture) {
+    private static ExplicitProcessParameters createExplicitProcessParameters(Map<String, Object> launchArguments) {
+        List<String> args = argsToStringList(launchArguments.get("args"));
+        List<String> vmArgs = argsToStringList(launchArguments.get("vmArgs"));
+        String cwd = Objects.toString(launchArguments.get("cwd"), System.getProperty("user.dir"));

Review comment:
       Q: is home dir sensible default -- shouldn't we use e.g. project base dir or something like that ?

##########
File path: cpplite/cpplite.debugger/src/org/netbeans/modules/cpplite/debugger/CPPLiteDebugger.java
##########
@@ -878,6 +882,18 @@ public void destroy() {
         };
     }
 
+    private static void setParameters(ProcessBuilder processBuilder, CPPLiteDebuggerConfig configuration) {
+        ExplicitProcessParameters.buildExplicitParameters(Lookup.getDefault());

Review comment:
       The return value is trashed ?

##########
File path: java/java.lsp.server/src/org/netbeans/modules/java/lsp/server/debugging/launch/NbLaunchDelegate.java
##########
@@ -344,19 +338,54 @@ public void propertyChange(PropertyChangeEvent evt) {
                     notifyFinished(context, exitCode != null && exitCode == 0);
                 });
                 Lookups.executeWith(execLookup, () -> {
-                    execNative(nativeImageFile, args, context, ed, launchFuture);
+                    execNative(nativeImageFile, args, context, ed, params, launchFuture);
                 });
             }
         }
         return launchFuture;
     }
 
-    private static void execNative(File nativeImageFile, List<String> args, DebugAdapterContext context, ExecutionDescriptor executionDescriptor, CompletableFuture<Void> launchFuture) {
+    private static ExplicitProcessParameters createExplicitProcessParameters(Map<String, Object> launchArguments) {
+        List<String> args = argsToStringList(launchArguments.get("args"));
+        List<String> vmArgs = argsToStringList(launchArguments.get("vmArgs"));
+        String cwd = Objects.toString(launchArguments.get("cwd"), System.getProperty("user.dir"));
+        Object envObj = launchArguments.get("env");
+        Map<String, String> env = envObj != null ? (Map<String, String>) envObj : Collections.emptyMap();
+        ExplicitProcessParameters.Builder bld = ExplicitProcessParameters.builder();
+        if (!args.isEmpty()) {
+            bld.launcherArgs(vmArgs);
+        }
+        if (!vmArgs.isEmpty()) {
+            bld.args(args);
+        }
+        bld.replaceArgs(false);
+        bld.workingDirectory(new File(cwd));
+        if (!env.isEmpty()) {
+            bld.environmentVariables(env);
+        }
+        ExplicitProcessParameters params = bld.build();
+        return params;
+    }
+
+    private static void execNative(File nativeImageFile, List<String> args,
+                                   DebugAdapterContext context,
+                                   ExecutionDescriptor executionDescriptor,
+                                   ExplicitProcessParameters params,
+                                   CompletableFuture<Void> launchFuture) {
         ExecutionService.newService(() -> {
             launchFuture.complete(null);
-            List<String> command = args.isEmpty() ? Collections.singletonList(nativeImageFile.getAbsolutePath()) : join(nativeImageFile.getAbsolutePath(), args);
+            List<String> command = join(nativeImageFile.getAbsolutePath(), args);
             try {
-                return new ProcessBuilder(command).start();
+                ProcessBuilder pb = new ProcessBuilder(command);
+                File workingDirectory = params.getWorkingDirectory();
+                if (workingDirectory != null) {
+                    pb.directory(workingDirectory);
+                }
+                Map<String, String> env = params.getEnvironmentVariables();
+                if (!env.isEmpty()) {
+                    pb.environment().putAll(env);

Review comment:
       Value of a map entry (env var value) may be `null`

##########
File path: ide/extexecution.base/src/org/netbeans/api/extexecution/base/ExplicitProcessParameters.java
##########
@@ -416,6 +497,12 @@ public Builder combine(@NullAllowed ExplicitProcessParameters p) {
             if (p.getArguments() != null) {
                 args(p.getArguments());
             }
+            if (p.getWorkingDirectory() != null) {
+                workingDirectory(p.getWorkingDirectory());
+            }
+            if (!p.getEnvironmentVariables().isEmpty()) {
+                environmentVariables(p.getEnvironmentVariables());

Review comment:
       Q: what about `null` values of env variables ? Adding a `null` env var is a way how to remove the setting (from an earlier `ExplicitProcessParameters` instance), but should the `null` value remain if applied on top of an existing list ?
   If so, it is IMHO worth to mention in Javadoc, as the consumer must be prepared to ignore null values.

##########
File path: java/maven/src/org/netbeans/modules/maven/runjar/LaunchArgPrereqsChecker.java
##########
@@ -122,6 +123,15 @@ public boolean checkRunConfig(RunConfig config, ExecutionContext con) {
             config.setProperty(MavenExecuteUtils.RUN_APP_PARAMS, 
                     MavenExecuteUtils.joinParameters(appArgsValue));
         }
+        File workingDirectory = injectParams.getWorkingDirectory();
+        if (workingDirectory != null) {
+            config.setProperty(MavenExecuteUtils.RUN_WORKDIR,
+                    workingDirectory.getAbsolutePath());
+        }
+        Map<String, String> environmentVariables = injectParams.getEnvironmentVariables();
+        for (Map.Entry<String, String> env : environmentVariables.entrySet()) {
+            config.setProperty("Env." + env.getKey(), env.getValue());

Review comment:
       handle null `env.getValue()` ?

##########
File path: java/maven/src/org/netbeans/modules/maven/runjar/LaunchArgPrereqsChecker.java
##########
@@ -122,6 +123,15 @@ public boolean checkRunConfig(RunConfig config, ExecutionContext con) {
             config.setProperty(MavenExecuteUtils.RUN_APP_PARAMS, 
                     MavenExecuteUtils.joinParameters(appArgsValue));
         }
+        File workingDirectory = injectParams.getWorkingDirectory();
+        if (workingDirectory != null) {

Review comment:
       Note - possibly a different behaviour for missing `cwd` than in N-I debugging (unspecified vs. userdir)

##########
File path: cpplite/cpplite.debugger/src/org/netbeans/modules/cpplite/debugger/CPPLiteDebuggerConfig.java
##########
@@ -31,16 +32,30 @@
 public final class CPPLiteDebuggerConfig {
 
     private final List<String> executable;
-    private final File directory;
+    private final ExplicitProcessParameters processParameters;
     @NullAllowed
     private final Long processId;
     private final String debugger;
 
-    public CPPLiteDebuggerConfig(List<String> executable, File directory, @NullAllowed Long processId, String debugger) {
-        this.executable = executable;
-        this.directory = directory;
+    public CPPLiteDebuggerConfig(List<String> executable, ExplicitProcessParameters processParameters, @NullAllowed Long processId, String debugger) {
+        this.processParameters = processParameters;
         this.processId = processId;
         this.debugger = debugger;
+        if (processParameters.isArgReplacement()) {

Review comment:
       I see that `launcherArguments` property is ignored - would it be possible to e.g. log a warning, if someone passes them (since they won't be used) ? Doing so may indicate some bug.
   




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org
For additional commands, e-mail: notifications-help@netbeans.apache.org

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists