|
1 | 1 | import 'process'; |
2 | 2 | import * as os from 'os'; |
| 3 | +import * as path from 'path'; |
3 | 4 | import * as vscode from 'vscode'; |
4 | 5 | import * as spawn from 'cross-spawn'; |
5 | 6 | import { ChildProcess, exec } from 'child_process'; |
@@ -44,7 +45,7 @@ const start = (): Promise<CljConnectionInformation> => { |
44 | 45 | return new Promise((resolve, reject) => { |
45 | 46 |
|
46 | 47 | nreplProcess = spawn('lein', LEIN_ARGS, { |
47 | | - cwd: vscode.workspace.rootPath, |
| 48 | + cwd: getCwd(), // see the `getCwd` function documentation! |
48 | 49 | detached: !(os.platform() === 'win32') |
49 | 50 | }); |
50 | 51 |
|
@@ -102,6 +103,27 @@ const stop = () => { |
102 | 103 |
|
103 | 104 | const dispose = stop; |
104 | 105 |
|
| 106 | +/** |
| 107 | + * It's important to set the current working directory parameter when spawning |
| 108 | + * the nREPL process. Without the parameter been set, it can take quite a long |
| 109 | + * period of time for the nREPL to start accepting commands. This most likely |
| 110 | + * the result of one of the plugins we use doing something with the file |
| 111 | + * system (first time I've seen it, I was surprised why VSCode and Java |
| 112 | + * constantly asking for the permissions to access folders in my home directory). |
| 113 | + */ |
| 114 | +const getCwd = () => { |
| 115 | + let cwd = vscode.workspace.rootPath; |
| 116 | + |
| 117 | + if (cwd) return cwd; |
| 118 | + |
| 119 | + // Try to get folder name from the active editor. |
| 120 | + const document = vscode.window.activeTextEditor?.document; |
| 121 | + |
| 122 | + if (!document) return; |
| 123 | + |
| 124 | + return path.dirname(document.fileName); |
| 125 | +} |
| 126 | + |
105 | 127 | export const nreplController = { |
106 | 128 | start, |
107 | 129 | stop, |
|
0 commit comments