Skip to content

Commit fd9c90d

Browse files
committed
Set current working directory even for individual files
1 parent e1d381e commit fd9c90d

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

src/nreplController.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import 'process';
22
import * as os from 'os';
3+
import * as path from 'path';
34
import * as vscode from 'vscode';
45
import * as spawn from 'cross-spawn';
56
import { ChildProcess, exec } from 'child_process';
@@ -44,7 +45,7 @@ const start = (): Promise<CljConnectionInformation> => {
4445
return new Promise((resolve, reject) => {
4546

4647
nreplProcess = spawn('lein', LEIN_ARGS, {
47-
cwd: vscode.workspace.rootPath,
48+
cwd: getCwd(), // see the `getCwd` function documentation!
4849
detached: !(os.platform() === 'win32')
4950
});
5051

@@ -102,6 +103,27 @@ const stop = () => {
102103

103104
const dispose = stop;
104105

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+
105127
export const nreplController = {
106128
start,
107129
stop,

0 commit comments

Comments
 (0)