Repository: eclipse-corrosion/corrosion (language: rust)

Issue:
Cannot use Rust if installed in directories containing spaces
I have found that should rust be installed in a directory containing spaces, then corrosion will not be able to use the rust installation.

To give my specific example. I am using Windows 10 X86_64. The user directory is "C:\users\Michael Whapples\". I had installed rust using rustup, letting it install in the default location under the user home directory. When I tried to use corrosion, when I went to preferences to configure the rust paths, I found even when the rust installation directories had been explicitly set there was still an error message about not being able to use rust (cannot remember the exact error message, either not being able to find the executable or obtain version information) and the apply button was not available.

I realised that the space in the directory path may have been causing problems, so I uninstalled rust, set RUSTUP_HOME and CARGO_HOME to a path without spaces (eg. "C:\rust\") and reinstalled rust via rustup. Using corrosion with rust installed in a location without spaces in the path worked fine.

At the moment I have only been able to test this issue on Windows, it may or may not exist on other platforms.


Discussion / hints:
I think it is easy to fix this bug.

The _**getProcessForCommand**_ function uses **_ProcessBuilder_** and it already can deal with spaces.  But this functionality breaks with function call like:
```
return matchPattern.matcher(getOutputFromCommand(commandPath + " --version")).matches(); 
```
Instead, it should be something like:
```
return matchPattern.matcher(getOutputFromCommand(new String[] { commandPath, "--version" })).matches();
```

Some modification of _**getProcessForCommand**_ function is also needed.
@justdak Interested.in submitting a PR?
@mickaelistria ok, I will try to create patch for this, if there are no other people who want to fix it. :)
@LucasBullen ?

Fix patch:
diff --git a/org.eclipse.corrosion/src/org/eclipse/corrosion/CorrosionPlugin.java b/org.eclipse.corrosion/src/org/eclipse/corrosion/CorrosionPlugin.java
index 2cf9c04..35e43da 100644
--- a/org.eclipse.corrosion/src/org/eclipse/corrosion/CorrosionPlugin.java
+++ b/org.eclipse.corrosion/src/org/eclipse/corrosion/CorrosionPlugin.java
@@ -20,7 +20,6 @@
 
 import org.eclipse.core.runtime.ICoreRunnable;
 import org.eclipse.core.runtime.IStatus;
-import org.eclipse.core.runtime.Platform;
 import org.eclipse.core.runtime.Status;
 import org.eclipse.core.runtime.jobs.Job;
 import org.eclipse.corrosion.cargo.core.CargoTools;
@@ -83,21 +82,21 @@ public static void showError(String title, String message) {
 		});
 	}
 
+	public static boolean validateCommandVersion(String[] commandStrings, Pattern matchPattern) {
+		String[] command = new String[1 + commandStrings.length];
+		
+		System.arraycopy(commandStrings, 0, command, 0, commandStrings.length);
+		command[commandStrings.length] = "--version"; //$NON-NLS-1$
+		
+		return matchPattern.matcher(getOutputFromCommand(command)).matches();
+	}
+	
 	public static boolean validateCommandVersion(String commandPath, Pattern matchPattern) {
-		return matchPattern.matcher(getOutputFromCommand(commandPath + " --version")).matches(); //$NON-NLS-1$
+		return matchPattern.matcher(getOutputFromCommand(commandPath, "--version")).matches(); //$NON-NLS-1$
 	}
 
 	public static Process getProcessForCommand(String... commandStrings) throws IOException {
-		String[] command = new String[3];
-		if (Platform.getOS().equals(Platform.OS_WIN32)) {
-			command[0] = "cmd"; //$NON-NLS-1$
-			command[1] = "/c"; //$NON-NLS-1$
-		} else {
-			command[0] = "/bin/bash"; //$NON-NLS-1$
-			command[1] = "-c"; //$NON-NLS-1$
-		}
-		command[2] = String.join(" ", commandStrings); //$NON-NLS-1$
-		ProcessBuilder builder = new ProcessBuilder(command);
+		ProcessBuilder builder = new ProcessBuilder(commandStrings);
 		builder.directory(getWorkingDirectoryFromPreferences());
 		return builder.start();
 	}
diff --git a/org.eclipse.corrosion/src/org/eclipse/corrosion/CorrosionPreferenceInitializer.java b/org.eclipse.corrosion/src/org/eclipse/corrosion/CorrosionPreferenceInitializer.java
index ab4caee..dbf9187 100644
--- a/org.eclipse.corrosion/src/org/eclipse/corrosion/CorrosionPreferenceInitializer.java
+++ b/org.eclipse.corrosion/src/org/eclipse/corrosion/CorrosionPreferenceInitializer.java
@@ -15,6 +15,7 @@
 package org.eclipse.corrosion;
 
 import java.io.File;
+import java.nio.file.Paths;
 
 import org.eclipse.core.runtime.Platform;
 import org.eclipse.core.runtime.preferences.AbstractPreferenceInitializer;
@@ -143,6 +144,6 @@ private static File getExectuableFileOfCargoDefaultHome(final String executable)
 	}
 
 	private static String getWorkingDirectoryBestGuess() {
-		return CorrosionPlugin.getOutputFromCommand(IS_WINDOWS ? "cd" : "pwd"); //$NON-NLS-1$ //$NON-NLS-2$
+		return Paths.get("").toAbsolutePath().toString(); //$NON-NLS-1$
 	}
 }
diff --git a/org.eclipse.corrosion/src/org/eclipse/corrosion/CorrosionPreferencePage.java b/org.eclipse.corrosion/src/org/eclipse/corrosion/CorrosionPreferencePage.java
index ed59bd5..47259ad 100644
--- a/org.eclipse.corrosion/src/org/eclipse/corrosion/CorrosionPreferencePage.java
+++ b/org.eclipse.corrosion/src/org/eclipse/corrosion/CorrosionPreferencePage.java
@@ -192,7 +192,8 @@ private boolean isRustupSectionValid() {
 			setErrorMessage(Messages.CorrosionPreferencePage_emptyToolchain);
 			return false;
 		}
-		String rlsPath = varParse(rustupInput.getValue()) + " run " + getToolchainId() + " rls"; //$NON-NLS-1$ //$NON-NLS-2$
+		String[] rlsPath = new String[] { varParse(rustupInput.getValue()), "run", getToolchainId(), "rls" }; //$NON-NLS-1$ //$NON-NLS-2$
+		
 		if (!CorrosionPlugin.validateCommandVersion(rlsPath, RustManager.RLS_VERSION_FORMAT_PATTERN)) {
 			setErrorMessage(Messages.CorrosionPreferencePage_rustupMissingRLS);
 			setInstallRequired(true);
diff --git a/org.eclipse.corrosion/src/org/eclipse/corrosion/RustManager.java b/org.eclipse.corrosion/src/org/eclipse/corrosion/RustManager.java
index 3af630f..6feafa8 100644
--- a/org.eclipse.corrosion/src/org/eclipse/corrosion/RustManager.java
+++ b/org.eclipse.corrosion/src/org/eclipse/corrosion/RustManager.java
@@ -281,7 +281,7 @@ public static String getRLS() {
 		String rlsPath = ""; //$NON-NLS-1$
 		if (rustSourceIndex == 0) {
 			String rustup = preferenceStore.getString(CorrosionPreferenceInitializer.RUSTUP_PATHS_PREFERENCE);
-			rlsPath = CorrosionPlugin.getOutputFromCommand(rustup + " which rls"); //$NON-NLS-1$
+			rlsPath = CorrosionPlugin.getOutputFromCommand(rustup, "which", "rls"); //$NON-NLS-1$ //$NON-NLS-2$
 		} else if (rustSourceIndex == 1) {
 			rlsPath = preferenceStore.getString(CorrosionPreferenceInitializer.RLS_PATH_PREFERENCE);
 		}
diff --git a/org.eclipse.corrosion/src/org/eclipse/corrosion/debug/RustGDBLaunchWrapper.java b/org.eclipse.corrosion/src/org/eclipse/corrosion/debug/RustGDBLaunchWrapper.java
index 31df795..62f5e58 100644
--- a/org.eclipse.corrosion/src/org/eclipse/corrosion/debug/RustGDBLaunchWrapper.java
+++ b/org.eclipse.corrosion/src/org/eclipse/corrosion/debug/RustGDBLaunchWrapper.java
@@ -45,7 +45,7 @@ public String[] getLaunchEnvironment() throws CoreException {
 
 		if (length > 0) {
 			for (int i = 0; i < length; i++) {
-				if (envVariables[i].startsWith("PATH=")) { //$NON-NLS-1$
+				if (envVariables[i].toUpperCase().startsWith("PATH=")) { //$NON-NLS-1$
 					envVariables[i] += File.pathSeparator + getCargoBinLocation() + File.pathSeparator
 							+ getGDBLocation();
 					return envVariables;
@@ -58,7 +58,7 @@ public String[] getLaunchEnvironment() throws CoreException {
 			int i = 0;
 			for (Map.Entry<String, String> entry : env.entrySet()) {
 				envVariables[i] = entry.getKey() + "=" + entry.getValue(); //$NON-NLS-1$
-				if (envVariables[i].startsWith("PATH=")) { //$NON-NLS-1$
+				if (envVariables[i].toUpperCase().startsWith("PATH=")) { //$NON-NLS-1$
 					envVariables[i] += File.pathSeparator + getCargoBinLocation() + File.pathSeparator
 							+ getGDBLocation();
 				}
@@ -78,7 +78,7 @@ private static String getCargoBinLocation() {
 
 	private static String getGDBLocation() {
 		IPath location = Path.fromOSString(CorrosionPlugin
-				.getOutputFromCommand(Platform.getOS().equals(Platform.OS_WIN32) ? "where gdb" : "which gdb"));//$NON-NLS-1$ //$NON-NLS-2$
+				.getOutputFromCommand((Platform.getOS().equals(Platform.OS_WIN32) ? "where" : "which"), "gdb"));//$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
 		String parentDirectory = location.toFile().getParent();
 		return parentDirectory != null ? parentDirectory : ""; //$NON-NLS-1$
 	}


Test patch: