diff --git a/app/src/main/java/io/xpipe/app/util/TerminalLauncherManager.java b/app/src/main/java/io/xpipe/app/util/TerminalLauncherManager.java index aa0d56e3c..404f43c97 100644 --- a/app/src/main/java/io/xpipe/app/util/TerminalLauncherManager.java +++ b/app/src/main/java/io/xpipe/app/util/TerminalLauncherManager.java @@ -3,7 +3,9 @@ package io.xpipe.app.util; import io.xpipe.beacon.ClientException; import io.xpipe.beacon.ServerException; import io.xpipe.core.process.ProcessControl; +import io.xpipe.core.process.ShellControl; import io.xpipe.core.process.TerminalInitScriptConfig; +import io.xpipe.core.util.FailableFunction; import lombok.Setter; import lombok.Value; import lombok.experimental.NonFinal; @@ -20,9 +22,21 @@ public class TerminalLauncherManager { private static void prepare( ProcessControl processControl, TerminalInitScriptConfig config, String directory, Entry entry) { + FailableFunction workingDirectory = sc -> { + if (directory == null) { + return null; + } + + if (!sc.getShellDialect().directoryExists(sc,directory).executeAndCheck()) { + return sc.getOsType().getFallbackWorkingDirectory(); + } + + return directory; + }; + try { var file = ScriptHelper.createLocalExecScript( - processControl.prepareTerminalOpen(config, directory != null ? var1 -> directory : null)); + processControl.prepareTerminalOpen(config, workingDirectory)); entry.setResult(new ResultSuccess(Path.of(file))); } catch (Exception e) { entry.setResult(new ResultFailure(e)); diff --git a/core/src/main/java/io/xpipe/core/process/OsType.java b/core/src/main/java/io/xpipe/core/process/OsType.java index 30992b951..2676b8d4b 100644 --- a/core/src/main/java/io/xpipe/core/process/OsType.java +++ b/core/src/main/java/io/xpipe/core/process/OsType.java @@ -28,6 +28,8 @@ public interface OsType { } } + String getFallbackWorkingDirectory(); + List determineInterestingPaths(ShellControl pc) throws Exception; String getHomeDirectory(ShellControl pc) throws Exception; @@ -54,6 +56,11 @@ public interface OsType { final class Windows implements OsType, Local, Any { + @Override + public String getFallbackWorkingDirectory() { + return "C:\\"; + } + @Override public List determineInterestingPaths(ShellControl pc) throws Exception { var home = getHomeDirectory(pc); @@ -116,6 +123,11 @@ public interface OsType { class Unix implements OsType { + @Override + public String getFallbackWorkingDirectory() { + return "/"; + } + @Override public List determineInterestingPaths(ShellControl pc) throws Exception { var home = getHomeDirectory(pc); @@ -198,6 +210,11 @@ public interface OsType { final class MacOs implements OsType, Local, Any { + @Override + public String getFallbackWorkingDirectory() { + return "/"; + } + @Override public List determineInterestingPaths(ShellControl pc) throws Exception { var home = getHomeDirectory(pc);