@@ -9,6 +9,7 @@ import { ConfigurationChangeEvent, ConfigurationTarget, Event, EventEmitter, Uri
9
9
import { IWorkspaceService } from './application/types' ;
10
10
import { PythonSettings } from './configSettings' ;
11
11
import { isTestExecution } from './constants' ;
12
+ import { FileSystemPaths } from './platform/fs-paths' ;
12
13
import {
13
14
IDisposable ,
14
15
IDisposableRegistry ,
@@ -22,6 +23,8 @@ import {
22
23
} from './types' ;
23
24
24
25
export const workspaceKeysForWhichTheCopyIsDone_Key = 'workspaceKeysForWhichTheCopyIsDone_Key' ;
26
+ export const workspaceFolderKeysForWhichTheCopyIsDone_Key = 'workspaceFolderKeysForWhichTheCopyIsDone_Key' ;
27
+ export const isGlobalSettingCopiedKey = 'isGlobalSettingCopiedKey' ;
25
28
export const defaultInterpreterPathSetting : keyof IPythonSettings = 'defaultInterpreterPath' ;
26
29
const CI_PYTHON_PATH = getCIPythonPath ( ) ;
27
30
@@ -33,13 +36,18 @@ export function getCIPythonPath(): string {
33
36
}
34
37
@injectable ( )
35
38
export class InterpreterPathService implements IInterpreterPathService {
39
+ public get onDidChange ( ) : Event < InterpreterConfigurationScope > {
40
+ return this . _didChangeInterpreterEmitter . event ;
41
+ }
36
42
public _didChangeInterpreterEmitter = new EventEmitter < InterpreterConfigurationScope > ( ) ;
43
+ private fileSystemPaths : FileSystemPaths ;
37
44
constructor (
38
45
@inject ( IPersistentStateFactory ) private readonly persistentStateFactory : IPersistentStateFactory ,
39
46
@inject ( IWorkspaceService ) private readonly workspaceService : IWorkspaceService ,
40
47
@inject ( IDisposableRegistry ) disposables : IDisposable [ ]
41
48
) {
42
49
disposables . push ( this . workspaceService . onDidChangeConfiguration ( this . onDidChangeConfiguration . bind ( this ) ) ) ;
50
+ this . fileSystemPaths = FileSystemPaths . withDefaults ( ) ;
43
51
}
44
52
45
53
public async onDidChangeConfiguration ( event : ConfigurationChangeEvent ) {
@@ -110,10 +118,6 @@ export class InterpreterPathService implements IInterpreterPathService {
110
118
}
111
119
}
112
120
113
- public get onDidChange ( ) : Event < InterpreterConfigurationScope > {
114
- return this . _didChangeInterpreterEmitter . event ;
115
- }
116
-
117
121
public getSettingKey (
118
122
resource : Uri ,
119
123
configTarget : ConfigurationTarget . Workspace | ConfigurationTarget . WorkspaceFolder
@@ -124,7 +128,9 @@ export class InterpreterPathService implements IInterpreterPathService {
124
128
settingKey = `WORKSPACE_FOLDER_INTERPRETER_PATH_${ folderKey } ` ;
125
129
} else {
126
130
settingKey = this . workspaceService . workspaceFile
127
- ? `WORKSPACE_INTERPRETER_PATH_${ this . workspaceService . workspaceFile . fsPath } `
131
+ ? `WORKSPACE_INTERPRETER_PATH_${ this . fileSystemPaths . normCase (
132
+ this . workspaceService . workspaceFile . fsPath
133
+ ) } `
128
134
: // Only a single folder is opened, use fsPath of the folder as key
129
135
`WORKSPACE_FOLDER_INTERPRETER_PATH_${ folderKey } ` ;
130
136
}
@@ -133,23 +139,48 @@ export class InterpreterPathService implements IInterpreterPathService {
133
139
134
140
public async copyOldInterpreterStorageValuesToNew ( resource : Resource ) : Promise < void > {
135
141
resource = PythonSettings . getSettingsUriAndTarget ( resource , this . workspaceService ) . uri ;
136
- const workspaceKey = this . workspaceService . getWorkspaceFolderIdentifier ( resource ) ;
142
+ const workspaceConfig = this . workspaceService . getConfiguration ( 'python' , resource ) ;
143
+ const oldSettings = workspaceConfig . inspect < string > ( 'pythonPath' ) ! ;
144
+
145
+ // Copy workspace folder setting into the new storage if it hasn't been copied already
146
+ const workspaceFolderKey = this . workspaceService . getWorkspaceFolderIdentifier ( resource ) ;
147
+ const flaggedWorkspaceFolderKeysStorage = this . persistentStateFactory . createGlobalPersistentState < string [ ] > (
148
+ workspaceFolderKeysForWhichTheCopyIsDone_Key ,
149
+ [ ]
150
+ ) ;
151
+ const flaggedWorkspaceFolderKeys = flaggedWorkspaceFolderKeysStorage . value ;
152
+ const shouldUpdateWorkspaceFolderSetting = ! flaggedWorkspaceFolderKeys . includes ( workspaceFolderKey ) ;
153
+ if ( shouldUpdateWorkspaceFolderSetting ) {
154
+ await this . update ( resource , ConfigurationTarget . WorkspaceFolder , oldSettings . workspaceFolderValue ) ;
155
+ await flaggedWorkspaceFolderKeysStorage . updateValue ( [ workspaceFolderKey , ...flaggedWorkspaceFolderKeys ] ) ;
156
+ }
157
+
158
+ // Copy workspace setting into the new storage if it hasn't been copied already
159
+ const workspaceKey = this . workspaceService . workspaceFile
160
+ ? this . fileSystemPaths . normCase ( this . workspaceService . workspaceFile . fsPath )
161
+ : undefined ;
137
162
const flaggedWorkspaceKeysStorage = this . persistentStateFactory . createGlobalPersistentState < string [ ] > (
138
163
workspaceKeysForWhichTheCopyIsDone_Key ,
139
164
[ ]
140
165
) ;
141
166
const flaggedWorkspaceKeys = flaggedWorkspaceKeysStorage . value ;
142
- if ( flaggedWorkspaceKeys . includes ( workspaceKey ) ) {
143
- // Only do a one-off import for each workspace
144
- return ;
145
- } else {
167
+ const shouldUpdateWorkspaceSetting = workspaceKey && ! flaggedWorkspaceKeys . includes ( workspaceKey ) ;
168
+ if ( workspaceKey && shouldUpdateWorkspaceSetting ) {
169
+ await this . update ( resource , ConfigurationTarget . Workspace , oldSettings . workspaceValue ) ;
146
170
await flaggedWorkspaceKeysStorage . updateValue ( [ workspaceKey , ...flaggedWorkspaceKeys ] ) ;
147
171
}
148
- const oldSettings = this . workspaceService . getConfiguration ( 'python' , resource ) ! . inspect < string > ( 'pythonPath' ) ! ;
149
- await Promise . all ( [
150
- this . update ( resource , ConfigurationTarget . WorkspaceFolder , oldSettings . workspaceFolderValue ) ,
151
- this . update ( resource , ConfigurationTarget . Workspace , oldSettings . workspaceValue ) ,
152
- this . update ( undefined , ConfigurationTarget . Global , oldSettings . globalValue )
153
- ] ) ;
172
+
173
+ // Move global setting into the new storage if it hasn't been moved already
174
+ const isGlobalSettingCopiedStorage = this . persistentStateFactory . createGlobalPersistentState < boolean > (
175
+ isGlobalSettingCopiedKey ,
176
+ false
177
+ ) ;
178
+ const shouldUpdateGlobalSetting = ! isGlobalSettingCopiedStorage . value ;
179
+ if ( shouldUpdateGlobalSetting ) {
180
+ await this . update ( undefined , ConfigurationTarget . Global , oldSettings . globalValue ) ;
181
+ // Make sure to delete the original setting after copying it
182
+ await workspaceConfig . update ( 'pythonPath' , undefined , ConfigurationTarget . Global ) ;
183
+ await isGlobalSettingCopiedStorage . updateValue ( true ) ;
184
+ }
154
185
}
155
186
}
0 commit comments