Skip to content

Commit e400541

Browse files
authored
Enable user to move from Global to Auto without reloading the window (#2974)
* Enable user to move from Global to Auto without reloading the window * use set python and notify if changed to trigger setup
1 parent 4b8f057 commit e400541

File tree

4 files changed

+62
-28
lines changed

4 files changed

+62
-28
lines changed

extension/src/config.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,17 @@ export class Config extends DeferredDisposable {
4343
return this.pythonBinPath
4444
}
4545

46+
public async setPythonBinPath() {
47+
this.pythonBinPath = await this.getConfigOrExtensionPythonBinPath()
48+
return this.deferred.resolve()
49+
}
50+
51+
public async setPythonAndNotifyIfChanged() {
52+
const oldPath = this.pythonBinPath
53+
this.pythonBinPath = await this.getConfigOrExtensionPythonBinPath()
54+
this.notifyIfChanged(oldPath, this.pythonBinPath)
55+
}
56+
4657
public unsetPythonBinPath() {
4758
this.pythonBinPath = undefined
4859
}
@@ -55,11 +66,6 @@ export class Config extends DeferredDisposable {
5566
return getConfigValue(ConfigKey.PYTHON_PATH) || (await getPythonBinPath())
5667
}
5768

58-
private async setPythonBinPath() {
59-
this.pythonBinPath = await this.getConfigOrExtensionPythonBinPath()
60-
return this.deferred.resolve()
61-
}
62-
6369
private async onDidChangePythonExecutionDetails() {
6470
this.pythonExecutionDetailsListener?.dispose()
6571
const onDidChangePythonExecutionDetails =
@@ -95,12 +101,6 @@ export class Config extends DeferredDisposable {
95101
)
96102
}
97103

98-
private async setPythonAndNotifyIfChanged() {
99-
const oldPath = this.pythonBinPath
100-
this.pythonBinPath = await this.getConfigOrExtensionPythonBinPath()
101-
this.notifyIfChanged(oldPath, this.pythonBinPath)
102-
}
103-
104104
private notifyIfChanged(
105105
oldPath: string | undefined,
106106
newPath: string | undefined

extension/src/extension.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,9 @@ export class Extension extends Disposable implements IExtension {
338338
const previousCliPath = this.config.getCliPath()
339339
const previousPythonPath = this.config.getPythonBinPath()
340340

341-
const completed = await setupWorkspace()
341+
const completed = await setupWorkspace(() =>
342+
this.config.setPythonAndNotifyIfChanged()
343+
)
342344
sendTelemetryEvent(
343345
RegisteredCommands.EXTENSION_SETUP_WORKSPACE,
344346
{ completed },
@@ -507,8 +509,18 @@ export class Extension extends Disposable implements IExtension {
507509
return createFileSystemWatcher(
508510
disposable => this.dispose.track(disposable),
509511
'**/dvc{,.exe}',
510-
path => {
511-
if (path && (!this.cliAccessible || !this.cliCompatible)) {
512+
async path => {
513+
if (!path) {
514+
return
515+
}
516+
517+
const previousPythonBinPath = this.config.getPythonBinPath()
518+
await this.config.setPythonBinPath()
519+
520+
const trySetupWithVenv =
521+
previousPythonBinPath !== this.config.getPythonBinPath()
522+
523+
if (!this.cliAccessible || !this.cliCompatible || trySetupWithVenv) {
512524
setup(this)
513525
}
514526
}

extension/src/setup.test.ts

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ const mockedSetRoots = jest.fn()
7979
const mockedSetupWorkspace = jest.fn()
8080
const mockedUnsetPythonBinPath = jest.fn()
8181

82+
const mockedSetConfigToUsePythonExtension = jest.fn()
83+
8284
const mockedQuickPickYesOrNo = jest.mocked(quickPickYesOrNo)
8385
const mockedQuickPickValue = jest.mocked(quickPickValue)
8486
const mockedSetConfigValue = jest.mocked(setConfigValue)
@@ -105,7 +107,7 @@ describe('setupWorkspace', () => {
105107
it('should present two options if the python extension is installed (Auto & Global)', async () => {
106108
mockedQuickPickValue.mockResolvedValueOnce(undefined)
107109

108-
await setupWorkspace()
110+
await setupWorkspace(mockedSetConfigToUsePythonExtension)
109111

110112
expect(mockedQuickPickValue).toHaveBeenCalledTimes(1)
111113
expect(mockedQuickPickValue).toHaveBeenCalledWith(
@@ -123,7 +125,7 @@ describe('setupWorkspace', () => {
123125
it('should present two options if the python extension is NOT installed (Manual & Global)', async () => {
124126
mockedExtensions.all = []
125127

126-
await setupWorkspace()
128+
await setupWorkspace(mockedSetConfigToUsePythonExtension)
127129

128130
expect(mockedQuickPickValue).toHaveBeenCalledTimes(1)
129131
expect(mockedQuickPickValue).toHaveBeenCalledWith(
@@ -141,9 +143,10 @@ describe('setupWorkspace', () => {
141143
it('should set the dvc path and python path options to undefined if the CLI is being auto detected inside a virtual environment', async () => {
142144
mockedQuickPickValue.mockResolvedValueOnce(2)
143145

144-
await setupWorkspace()
146+
await setupWorkspace(mockedSetConfigToUsePythonExtension)
145147

146148
expect(mockedQuickPickValue).toHaveBeenCalledTimes(1)
149+
expect(mockedSetConfigToUsePythonExtension).toHaveBeenCalledTimes(1)
147150
expect(mockedSetConfigValue).toHaveBeenCalledWith(
148151
ConfigKey.DVC_PATH,
149152
undefined
@@ -158,7 +161,7 @@ describe('setupWorkspace', () => {
158161
mockedQuickPickValue.mockResolvedValueOnce(1)
159162
mockedQuickPickOneOrInput.mockResolvedValueOnce(undefined)
160163

161-
await setupWorkspace()
164+
await setupWorkspace(mockedSetConfigToUsePythonExtension)
162165

163166
expect(mockedQuickPickValue).toHaveBeenCalledTimes(1)
164167
expect(mockedQuickPickOneOrInput).toHaveBeenCalledTimes(1)
@@ -173,8 +176,9 @@ describe('setupWorkspace', () => {
173176
mockedQuickPickOneOrInput.mockResolvedValueOnce('pick')
174177
mockedPickFile.mockResolvedValueOnce(mockedPythonPath)
175178

176-
await setupWorkspace()
179+
await setupWorkspace(mockedSetConfigToUsePythonExtension)
177180

181+
expect(mockedSetConfigToUsePythonExtension).not.toHaveBeenCalled()
178182
expect(mockedQuickPickValue).toHaveBeenCalledTimes(1)
179183
expect(mockedQuickPickYesOrNo).toHaveBeenCalledTimes(2)
180184
expect(mockedQuickPickOneOrInput).toHaveBeenCalledTimes(1)
@@ -190,7 +194,7 @@ describe('setupWorkspace', () => {
190194
it('should return without setting any options if the dialog is cancelled at the virtual environment step', async () => {
191195
mockedQuickPickValue.mockResolvedValueOnce(undefined)
192196

193-
await setupWorkspace()
197+
await setupWorkspace(mockedSetConfigToUsePythonExtension)
194198

195199
expect(mockedQuickPickValue).toHaveBeenCalledTimes(1)
196200
expect(mockedSetConfigValue).not.toHaveBeenCalled()
@@ -208,7 +212,7 @@ describe('setupWorkspace', () => {
208212
.mockResolvedValueOnce(mockedPythonPath)
209213
.mockResolvedValueOnce(mockedDvcPath)
210214

211-
await setupWorkspace()
215+
await setupWorkspace(mockedSetConfigToUsePythonExtension)
212216

213217
expect(mockedQuickPickValue).toHaveBeenCalledTimes(1)
214218
expect(mockedQuickPickYesOrNo).toHaveBeenCalledTimes(2)
@@ -229,7 +233,7 @@ describe('setupWorkspace', () => {
229233
mockedQuickPickValue.mockResolvedValueOnce(1)
230234
mockedQuickPickOneOrInput.mockResolvedValueOnce(undefined)
231235

232-
await setupWorkspace()
236+
await setupWorkspace(mockedSetConfigToUsePythonExtension)
233237

234238
expect(mockedQuickPickValue).toHaveBeenCalledTimes(1)
235239
expect(mockedQuickPickYesOrNo).not.toHaveBeenCalled()
@@ -247,7 +251,7 @@ describe('setupWorkspace', () => {
247251
mockedQuickPickOneOrInput.mockResolvedValueOnce('pick')
248252
mockedPickFile.mockResolvedValueOnce(mockedDvcPath)
249253

250-
await setupWorkspace()
254+
await setupWorkspace(mockedSetConfigToUsePythonExtension)
251255

252256
expect(mockedQuickPickValue).toHaveBeenCalledTimes(1)
253257
expect(mockedQuickPickYesOrNo).toHaveBeenCalledTimes(2)

extension/src/setup.ts

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -129,29 +129,47 @@ const quickPickVenvOption = () => {
129129
})
130130
}
131131

132-
const selectInterpreter = async (usesVenv: number) => {
132+
const manuallySelectInterpreter = async () => {
133+
const interpreterSet = await enterPathOrPickFile(
134+
ConfigKey.PYTHON_PATH,
135+
'Python Interpreter'
136+
)
137+
if (!interpreterSet) {
138+
return false
139+
}
140+
return pickVenvOptions()
141+
}
142+
143+
const selectInterpreter = async (
144+
usesVenv: number,
145+
setConfigToUsePythonExtension: () => Promise<void>
146+
) => {
133147
if (usesVenv === 1) {
134-
return enterPathOrPickFile(ConfigKey.PYTHON_PATH, 'Python Interpreter')
148+
return manuallySelectInterpreter()
135149
}
136150

137151
await Promise.all([
138152
setConfigPath(ConfigKey.PYTHON_PATH, undefined),
139153
setConfigPath(ConfigKey.DVC_PATH, undefined)
140154
])
141155

156+
await setConfigToUsePythonExtension()
157+
142158
selectPythonInterpreter()
143-
return false
159+
return true
144160
}
145161

146-
export const setupWorkspace = async (): Promise<boolean> => {
162+
export const setupWorkspace = async (
163+
setConfigToUsePythonExtension: () => Promise<void>
164+
): Promise<boolean> => {
147165
const usesVenv = await quickPickVenvOption()
148166

149167
if (usesVenv === undefined) {
150168
return false
151169
}
152170

153171
if (usesVenv) {
154-
return (await selectInterpreter(usesVenv)) ? pickVenvOptions() : false
172+
return selectInterpreter(usesVenv, setConfigToUsePythonExtension)
155173
}
156174

157175
return pickCliPath()

0 commit comments

Comments
 (0)