diff --git a/packages/safe-chain/src/shell-integration/supported-shells/bash.js b/packages/safe-chain/src/shell-integration/supported-shells/bash.js index 3c4b1f9..5f9c54e 100644 --- a/packages/safe-chain/src/shell-integration/supported-shells/bash.js +++ b/packages/safe-chain/src/shell-integration/supported-shells/bash.js @@ -3,7 +3,8 @@ import { doesExecutableExistOnSystem, removeLinesMatchingPattern, } from "../helpers.js"; -import { execSync } from "child_process"; +import { execSync, spawnSync } from "child_process"; +import * as os from "os"; const shellName = "Bash"; const executableName = "bash"; @@ -43,10 +44,12 @@ function setup() { function getStartupFile() { try { - return execSync(startupFileCommand, { + var path = execSync(startupFileCommand, { encoding: "utf8", shell: executableName, }).trim(); + + return windowsFixPath(path); } catch (error) { throw new Error( `Command failed: ${startupFileCommand}. Error: ${error.message}` @@ -54,6 +57,50 @@ function getStartupFile() { } } +function windowsFixPath(path) { + try { + if (os.platform() !== "win32") { + return path; + } + + // On windows cygwin bash, paths are returned in format /c/user/..., but we need it in format C:\user\... + // To convert, the cygpath -w command can be used to convert to the desired format. + // Cygpath only exists on Cygwin, so we first check if the command is available. + // If it is, we use it to convert the path. + if (hasCygpath()) { + return cygpathw(path); + } + + return path; + } catch { + return path; + } +} + +function hasCygpath() { + try { + var result = spawnSync("where", ["cygpath"], { shell: executableName }); + return result.status === 0; + } catch { + return false; + } +} + +function cygpathw(path) { + try { + var result = spawnSync("cygpath", ["-w", path], { + encoding: "utf8", + shell: executableName, + }); + if (result.status === 0) { + return result.stdout.trim(); + } + return path; + } catch { + return path; + } +} + export default { name: shellName, isInstalled,