Skip to content

Commit c52b23a

Browse files
1 parent d1fe73c commit c52b23a

File tree

5 files changed

+46
-9
lines changed

5 files changed

+46
-9
lines changed

packages/databricks-vscode/package.json

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,13 @@
394394
"type": "string"
395395
},
396396
"description": "Command line arguments."
397+
},
398+
"env": {
399+
"type": "object",
400+
"items": {
401+
"type": "string"
402+
},
403+
"description": "Environment variables."
397404
}
398405
}
399406
}
@@ -404,7 +411,8 @@
404411
"request": "launch",
405412
"name": "Run on Databricks",
406413
"program": "${file}",
407-
"args": []
414+
"args": [],
415+
"env": {}
408416
}
409417
],
410418
"configurationSnippets": [

packages/databricks-vscode/src/run/DatabricksDebugAdapter.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ interface ILaunchRequestArguments extends DebugProtocol.LaunchRequestArguments {
3838

3939
/** Command line arguments */
4040
args?: string[];
41+
42+
/** Env variables */
43+
env?: Record<string, string>;
4144
}
4245

4346
export class DatabricksDebugAdapterFactory
@@ -246,7 +249,7 @@ export class DatabricksDebugSession extends LoggingDebugSession {
246249
await commands.executeCommand("workbench.panel.repl.view.focus");
247250

248251
// start the program in the runtime
249-
await this.runtime.start(args.program, args.args || []);
252+
await this.runtime.start(args.program, args.args || [], args.env || {});
250253
this.sendResponse(response);
251254
}
252255

packages/databricks-vscode/src/run/DatabricksRuntime.test.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ describe(__filename, () => {
9999
})
100100
);
101101

102-
await runtime.start("/Desktop/workspaces/hello.py", []);
102+
await runtime.start("/Desktop/workspaces/hello.py", [], {});
103103

104104
verify(connectionManagerMock.waitForConnect()).called();
105105

@@ -115,14 +115,15 @@ describe(__filename, () => {
115115
});
116116

117117
it("should have the right code", async () => {
118-
await runtime.start("/Desktop/workspaces/hello.py", []);
118+
await runtime.start("/Desktop/workspaces/hello.py", [], {TEST: "TEST"});
119119

120120
const code = capture(executionContextMock.execute).first()[0];
121121
assert.equal(
122122
code,
123123
`import os; os.chdir("/Workspace/Repos/fabian@databricks.com/test");
124124
import sys; sys.path.append("/Workspace/Repos/fabian@databricks.com/test")
125125
import sys; sys.argv = ['/Workspace/Repos/fabian@databricks.com/test/hello.py'];
126+
import os; os.environ["TEST"]='TEST';
126127
import logging; logger = spark._jvm.org.apache.log4j; logging.getLogger("py4j.java_gateway").setLevel(logging.ERROR)
127128
print('43')`
128129
);
@@ -155,7 +156,7 @@ print('43')`
155156
})
156157
);
157158

158-
await runtime.start("/Desktop/workspaces/hello.py", []);
159+
await runtime.start("/Desktop/workspaces/hello.py", [], {});
159160

160161
assert.equal(outputs.length, 7);
161162
assert.equal(outputs[4].text, "something went wrong");

packages/databricks-vscode/src/run/DatabricksRuntime.ts

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ export interface FileAccessor {
3030
readFile(path: string): Promise<string>;
3131
}
3232

33+
type EnvVars = Record<string, string>;
34+
3335
export class DatabricksRuntime implements Disposable {
3436
private _onDidEndEmitter: EventEmitter<void> = new EventEmitter<void>();
3537
readonly onDidEnd: Event<void> = this._onDidEndEmitter.event;
@@ -60,7 +62,11 @@ export class DatabricksRuntime implements Disposable {
6062
/**
6163
* Start executing the given program.
6264
*/
63-
public async start(program: string, args: Array<string>): Promise<void> {
65+
public async start(
66+
program: string,
67+
args: Array<string>,
68+
envVars: EnvVars
69+
): Promise<void> {
6470
const start = Date.now();
6571

6672
const log = (message: string, line: number) => {
@@ -168,7 +174,8 @@ export class DatabricksRuntime implements Disposable {
168174
program,
169175
lines,
170176
args,
171-
syncDestination
177+
syncDestination,
178+
envVars
172179
),
173180
undefined,
174181
this.token
@@ -231,13 +238,28 @@ export class DatabricksRuntime implements Disposable {
231238
program: string,
232239
programLines: Array<string>,
233240
args: Array<string>,
234-
syncDestination: SyncDestination
241+
syncDestination: SyncDestination,
242+
envVars: EnvVars
235243
): string {
236244
const argv = [
237245
syncDestination.localToRemote(Uri.file(program)),
238246
...args,
239247
];
240248

249+
const envVarSetCmds = [];
250+
for (const key in envVars) {
251+
if (!/^[a-zA-Z_]{1,}[a-zA-Z0-9_]*$/.test(key)) {
252+
this._onErrorEmitter.fire(
253+
`Invalid environment variable ${key}: Only lower and upper case letters, digits and '_'(underscore) are allowed.`
254+
);
255+
return "";
256+
}
257+
const cmd = `os.environ["${key}"]='${this.escapePythonString(
258+
envVars[key]
259+
)}'`;
260+
envVarSetCmds.push(cmd);
261+
}
262+
241263
return [
242264
// set working directory
243265
`import os; os.chdir("${syncDestination.localToRemoteDir(
@@ -252,14 +274,16 @@ export class DatabricksRuntime implements Disposable {
252274
.map((arg) => this.escapePythonString(arg))
253275
.join("', '")}'];`,
254276

277+
`import os; ${envVarSetCmds.join("; ")};`,
278+
255279
// Set log level to "ERROR". See https://kb.databricks.com/notebooks/cmd-c-on-object-id-p0.html
256280
`import logging; logger = spark._jvm.org.apache.log4j; logging.getLogger("py4j.java_gateway").setLevel(logging.ERROR)`,
257281
...programLines,
258282
].join("\n");
259283
}
260284

261285
private escapePythonString(str: string): string {
262-
return str.replace(/'/g, "\\'").replace(/\\/g, "\\\\");
286+
return str.replace(/\\/g, "\\\\").replace(/'/g, "\\'");
263287
}
264288

265289
public async disconnect(): Promise<void> {

packages/databricks-vscode/src/sync/CodeSynchronizer.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ export class CodeSynchronizer implements Disposable {
5656
}
5757

5858
async start(syncType: "full" | "incremental") {
59+
this._state = "IN_PROGRESS";
5960
const task = new SyncTask(
6061
this.connection,
6162
this.cli,

0 commit comments

Comments
 (0)