Skip to content

Commit 2491287

Browse files
Explicitly clear terminal environment variables before injecting new ones (#996)
## Changes * Environment variables are not getting refreshed by just issuing the `environmentVariableCollection.replace` command. We now explicitly clear the terminal env (`environmentVariableCollection.clear`) before injecting new variables. * Also synchronised writing env file and exporting to terminal. Env file is written first and we export to terminal only on successful writes. fixes #980 ## Tests * manual
1 parent 0cd590e commit 2491287

File tree

2 files changed

+48
-42
lines changed

2 files changed

+48
-42
lines changed

packages/databricks-vscode/src/extension.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,10 @@ export async function activate(
8888

8989
// Add the databricks binary to the PATH environment variable in terminals
9090
context.environmentVariableCollection.clear();
91-
context.environmentVariableCollection.append(
91+
context.environmentVariableCollection.persistent = false;
92+
context.environmentVariableCollection.prepend(
9293
"PATH",
93-
`${path.delimiter}${context.asAbsolutePath("./bin")}`
94+
`${context.asAbsolutePath("./bin")}${path.delimiter}`
9495
);
9596

9697
const loggerManager = new LoggerManager(context);

packages/databricks-vscode/src/file-managers/DatabricksEnvFileManager.ts

Lines changed: 45 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {Context, context} from "@databricks/databricks-sdk/dist/context";
1818
import {DbConnectStatusBarButton} from "../language/DbConnectStatusBarButton";
1919
import {EnvVarGenerators, FileUtils} from "../utils";
2020
import {NotebookInitScriptManager} from "../language/notebooks/NotebookInitScriptManager";
21+
import {Mutex} from "../locking/Mutex";
2122

2223
function isValidUserEnvPath(
2324
path: string | undefined,
@@ -27,7 +28,7 @@ function isValidUserEnvPath(
2728
}
2829
export class DatabricksEnvFileManager implements Disposable {
2930
private disposables: Disposable[] = [];
30-
31+
private mutex = new Mutex();
3132
private readonly unresolvedDatabricksEnvFile: string;
3233
private readonly databricksEnvPath: Uri;
3334
private readonly unresolvedUserEnvFile: string;
@@ -97,38 +98,31 @@ export class DatabricksEnvFileManager implements Disposable {
9798
this.disposables.push(
9899
userEnvFileWatcher,
99100
userEnvFileWatcher.onDidChange(async () => {
100-
await this.emitToTerminal();
101101
await this.writeFile();
102102
}, this),
103103
userEnvFileWatcher.onDidDelete(async () => {
104-
await this.emitToTerminal();
105104
await this.writeFile();
106105
}, this),
107106
userEnvFileWatcher.onDidCreate(async () => {
108-
await this.emitToTerminal();
109107
await this.writeFile();
110108
}, this),
111109
this.featureManager.onDidChangeState(
112110
"notebooks.dbconnect",
113111
async () => {
114-
await this.emitToTerminal();
115112
await this.writeFile();
116113
}
117114
),
118115
this.featureManager.onDidChangeState(
119116
"debugging.dbconnect",
120117
() => {
121-
this.emitToTerminal();
122118
this.writeFile();
123119
},
124120
this
125121
),
126122
this.connectionManager.onDidChangeCluster(async () => {
127-
this.emitToTerminal();
128123
this.writeFile();
129124
}, this),
130125
this.connectionManager.onDidChangeState(async () => {
131-
this.emitToTerminal();
132126
this.writeFile();
133127
}, this)
134128
);
@@ -153,44 +147,51 @@ export class DatabricksEnvFileManager implements Disposable {
153147
async writeFile(@context ctx?: Context) {
154148
await this.connectionManager.waitForConnect();
155149

156-
const data = Object.entries({
157-
...(this.getDatabrickseEnvVars() || {}),
158-
...((await EnvVarGenerators.getDbConnectEnvVars(
159-
this.connectionManager,
160-
this.workspacePath
161-
)) || {}),
162-
...this.getIdeEnvVars(),
163-
...((await this.getUserEnvVars()) || {}),
164-
})
165-
.filter(([, value]) => value !== undefined)
166-
.map(([key, value]) => `${key}=${value}`);
167-
data.sort();
150+
await this.mutex.wait();
168151
try {
169-
const oldData = await readFile(
170-
this.databricksEnvPath.fsPath,
171-
"utf-8"
172-
);
173-
if (oldData !== data.join(os.EOL)) {
174-
this.onDidChangeEnvironmentVariablesEmitter.fire();
152+
const data = Object.entries({
153+
...(this.getDatabrickseEnvVars() || {}),
154+
...((await EnvVarGenerators.getDbConnectEnvVars(
155+
this.connectionManager,
156+
this.workspacePath
157+
)) || {}),
158+
...this.getIdeEnvVars(),
159+
...((await this.getUserEnvVars()) || {}),
160+
})
161+
.filter(([, value]) => value !== undefined)
162+
.map(([key, value]) => `${key}=${value}`);
163+
data.sort();
164+
try {
165+
const oldData = await readFile(
166+
this.databricksEnvPath.fsPath,
167+
"utf-8"
168+
);
169+
if (oldData === data.join(os.EOL)) {
170+
return;
171+
}
172+
} catch (e) {
173+
ctx?.logger?.info("Error reading old databricks.env file", e);
175174
}
176-
} catch (e) {
177-
ctx?.logger?.info("Error reading old databricks.env file", e);
178-
}
179-
try {
180-
await writeFile(
181-
this.databricksEnvPath.fsPath,
182-
data.join(os.EOL),
183-
"utf-8"
184-
);
185-
this.dbConnectStatusBarButton.update();
186-
} catch (e) {
187-
ctx?.logger?.info("Error writing databricks.env file", e);
175+
this.onDidChangeEnvironmentVariablesEmitter.fire();
176+
try {
177+
await writeFile(
178+
this.databricksEnvPath.fsPath,
179+
data.join(os.EOL),
180+
"utf-8"
181+
);
182+
this.dbConnectStatusBarButton.update();
183+
await this.emitToTerminal();
184+
} catch (e) {
185+
ctx?.logger?.info("Error writing databricks.env file", e);
186+
}
187+
} finally {
188+
this.mutex.signal();
188189
}
189190
}
190191

191192
async emitToTerminal() {
192-
await this.connectionManager.waitForConnect();
193-
193+
this.clearTerminalEnv();
194+
this.extensionContext.environmentVariableCollection.persistent = false;
194195
Object.entries({
195196
...(this.getDatabrickseEnvVars() || {}),
196197
...this.getIdeEnvVars(),
@@ -207,6 +208,10 @@ export class DatabricksEnvFileManager implements Disposable {
207208
value
208209
);
209210
});
211+
this.extensionContext.environmentVariableCollection.prepend(
212+
"PATH",
213+
`${this.extensionContext.asAbsolutePath("./bin")}${path.delimiter}`
214+
);
210215
}
211216

212217
async clearTerminalEnv() {

0 commit comments

Comments
 (0)