Skip to content

Commit c96b05f

Browse files
Improve environment checks (#1260)
## Changes Check that python version in the environment matches the selected DBR version ## Tests <!-- How is this tested? --> --------- Co-authored-by: Kartik Gupta <[email protected]>
1 parent 31eeb6b commit c96b05f

File tree

1 file changed

+58
-20
lines changed

1 file changed

+58
-20
lines changed

packages/databricks-vscode/src/language/EnvironmentDependenciesVerifier.ts

Lines changed: 58 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {Loggers} from "../logger";
88
import {Context, context} from "@databricks/databricks-sdk/dist/context";
99
import {EnvironmentDependenciesInstaller} from "./EnvironmentDependenciesInstaller";
1010
import {FeatureStepState} from "../feature-manager/FeatureManager";
11+
import {ResolvedEnvironment} from "./MsPythonExtensionApi";
1112

1213
export class EnvironmentDependenciesVerifier extends MultiStepAccessVerifier {
1314
constructor(
@@ -22,9 +23,10 @@ export class EnvironmentDependenciesVerifier extends MultiStepAccessVerifier {
2223
"checkEnvironmentDependencies",
2324
]);
2425
this.disposables.push(
25-
this.connectionManager.onDidChangeCluster((cluster) => {
26+
this.connectionManager.onDidChangeCluster(async (cluster) => {
2627
this.checkCluster(cluster);
2728
if (cluster) {
29+
await this.checkPythonEnvironment();
2830
this.checkEnvironmentDependencies();
2931
}
3032
}, this),
@@ -140,40 +142,76 @@ export class EnvironmentDependenciesVerifier extends MultiStepAccessVerifier {
140142
return this.acceptStep("checkWorkspaceHasUc");
141143
}
142144

145+
private matchEnvironmentVersion(
146+
env: ResolvedEnvironment | undefined,
147+
major: number,
148+
minor: number
149+
): boolean {
150+
if (!env || !env.version || !env.environment) {
151+
return false;
152+
}
153+
return env.version.major === major && env.version.minor === minor;
154+
}
155+
156+
private printEnvironment(env?: ResolvedEnvironment): string {
157+
return env?.version && env.environment
158+
? `Current version is ${env.version.major}.${env.version.minor}.${env.version.micro}.`
159+
: "No active environments found.";
160+
}
161+
143162
async checkPythonEnvironment(): Promise<FeatureStepState> {
144-
const executable = await this.pythonExtension.getPythonExecutable();
145-
if (!executable) {
163+
const env = await this.pythonExtension.pythonEnvironment;
164+
const dbrVersionParts =
165+
this.connectionManager.cluster?.dbrVersion || [];
166+
// DBR 13 and 14 require python 3.10
167+
if (
168+
(dbrVersionParts[0] === 13 || dbrVersionParts[0] === 14) &&
169+
!this.matchEnvironmentVersion(env, 3, 10)
170+
) {
146171
return this.rejectStep(
147172
"checkPythonEnvironment",
148-
"Select Python Interpreter",
149-
"No python executable found",
173+
"Activate an environment with Python 3.10",
174+
`The python version should match DBR ${
175+
dbrVersionParts[0]
176+
} requirements. ${this.printEnvironment(env)}`,
150177
this.selectPythonInterpreter.bind(this)
151178
);
152179
}
153-
const env = await this.pythonExtension.pythonEnvironment;
180+
// DBR 15 requires python 3.11
154181
if (
155-
env?.version &&
156-
!(
157-
env.version.major > 3 ||
158-
(env.version.major === 3 && env.version.minor >= 10)
159-
)
182+
dbrVersionParts[0] === 15 &&
183+
!this.matchEnvironmentVersion(env, 3, 11)
160184
) {
161185
return this.rejectStep(
162186
"checkPythonEnvironment",
163-
"Select Python >= 3.10.0",
164-
`Databricks Connect requires python >= 3.10.0. Current version is ${[
165-
env.version.major,
166-
env.version.minor,
167-
env.version.micro,
168-
].join(".")}.`,
187+
"Activate an environment with Python 3.11",
188+
`The version should match DBR ${
189+
dbrVersionParts[0]
190+
} requirements. ${this.printEnvironment(env)}`,
191+
this.selectPythonInterpreter.bind(this)
192+
);
193+
}
194+
// If we don't know DBR version (no cluster is connected or new version is released and the extension isn't updated yet),
195+
// we still check that environment is active and has python >= 3.10
196+
const envVersionTooLow =
197+
env?.version && (env.version.major !== 3 || env.version.minor < 10);
198+
const noEnvironment = !env?.environment;
199+
if (noEnvironment || envVersionTooLow) {
200+
return this.rejectStep(
201+
"checkPythonEnvironment",
202+
"Activate an environment with Python >= 3.10",
203+
`Databricks Connect requires python >= 3.10. ${this.printEnvironment(
204+
env
205+
)}`,
169206
this.selectPythonInterpreter.bind(this)
170207
);
171208
}
172-
if (!env?.environment) {
209+
const executable = await this.pythonExtension.getPythonExecutable();
210+
if (!executable) {
173211
return this.rejectStep(
174212
"checkPythonEnvironment",
175-
"Activate virtual environment",
176-
"No active virtual environment",
213+
"Activate an environment with Python >= 3.10",
214+
"No python executable found",
177215
this.selectPythonInterpreter.bind(this)
178216
);
179217
}

0 commit comments

Comments
 (0)