Skip to content

Commit efa5c22

Browse files
authored
Rework Database Storage Path Logic (#11309)
* Fix typo (#11275) * rework database logic * resolve PR * fix lint issues * resolve PR * resolve PR * resolve PR * resolve PR * update names * resolve PR * resolve PR * fix lint error * resolve PR * resolve PR
1 parent 4199155 commit efa5c22

File tree

2 files changed

+47
-17
lines changed

2 files changed

+47
-17
lines changed

Extension/src/LanguageServer/client.ts

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ import { localizedStringCount, lookupString } from '../nativeStrings';
4242
import * as telemetry from '../telemetry';
4343
import { TestHook, getTestHook } from '../testHook';
4444
import {
45-
CodeAnalysisDiagnosticIdentifiersAndUri, RegisterCodeAnalysisNotifications,
45+
CodeAnalysisDiagnosticIdentifiersAndUri,
46+
RegisterCodeAnalysisNotifications,
4647
RemoveCodeAnalysisProblemsParams,
4748
removeAllCodeAnalysisProblems,
4849
removeCodeAnalysisProblems
@@ -97,6 +98,7 @@ interface ConfigStateReceived {
9798
let displayedSelectCompiler: boolean = false;
9899
let secondPromptCounter: number = 0;
99100
let scanForCompilersDone: boolean = false;
101+
let workspaceHash: string = "";
100102

101103
let workspaceDisposables: vscode.Disposable[] = [];
102104
export let workspaceReferences: refs.ReferencesManager;
@@ -539,7 +541,9 @@ export interface TextDocumentWillSaveParams {
539541
interface InitializationOptions {
540542
packageVersion: string;
541543
extensionPath: string;
542-
storagePath: string;
544+
cacheStoragePath: string;
545+
workspaceStoragePath: string;
546+
databaseStoragePath: string;
543547
freeMemory: number;
544548
vcpkgRoot: string;
545549
intelliSenseCacheDisabled: boolean;
@@ -821,7 +825,7 @@ export class DefaultClient implements Client {
821825
private rootPathFileWatcher?: vscode.FileSystemWatcher;
822826
private rootFolder?: vscode.WorkspaceFolder;
823827
private rootRealPath: string;
824-
private storagePath: string;
828+
private workspaceStoragePath: string;
825829
private trackedDocuments = new Set<vscode.TextDocument>();
826830
private isSupported: boolean = true;
827831
private inactiveRegionsDecorations = new Map<string, DecorationRangesPair>();
@@ -916,7 +920,7 @@ export class DefaultClient implements Client {
916920
public get AdditionalEnvironment(): { [key: string]: string | string[] } {
917921
return {
918922
workspaceFolderBasename: this.Name,
919-
workspaceStorage: this.storagePath,
923+
workspaceStorage: this.workspaceStoragePath,
920924
execPath: process.execPath,
921925
pathSeparator: (os.platform() === 'win32') ? "\\" : "/"
922926
};
@@ -1285,21 +1289,17 @@ export class DefaultClient implements Client {
12851289
this.rootFolder = workspaceFolder;
12861290
this.rootRealPath = this.RootPath ? fs.existsSync(this.RootPath) ? fs.realpathSync(this.RootPath) : this.RootPath : "";
12871291

1288-
let storagePath: string | undefined;
1289-
if (util.extensionContext) {
1290-
const path: string | undefined = util.extensionContext.storageUri?.fsPath;
1291-
if (path) {
1292-
storagePath = path;
1293-
}
1292+
this.workspaceStoragePath = util.extensionContext?.storageUri?.fsPath ?? "";
1293+
if (this.workspaceStoragePath.length > 0) {
1294+
workspaceHash = path.basename(path.dirname(this.workspaceStoragePath));
1295+
} else {
1296+
this.workspaceStoragePath = this.RootPath ? path.join(this.RootPath, ".vscode") : "";
12941297
}
12951298

1296-
if (!storagePath) {
1297-
storagePath = this.RootPath ? path.join(this.RootPath, "/.vscode") : "";
1298-
}
12991299
if (workspaceFolder && vscode.workspace.workspaceFolders && vscode.workspace.workspaceFolders.length > 1) {
1300-
storagePath = path.join(storagePath, util.getUniqueWorkspaceStorageName(workspaceFolder));
1300+
this.workspaceStoragePath = path.join(this.workspaceStoragePath, util.getUniqueWorkspaceStorageName(workspaceFolder));
13011301
}
1302-
this.storagePath = storagePath;
1302+
13031303
const rootUri: vscode.Uri | undefined = this.RootUri;
13041304
this.settingsTracker = new SettingsTracker(rootUri);
13051305

@@ -1622,10 +1622,16 @@ export class DefaultClient implements Client {
16221622
currentCaseSensitiveFileSupport.Value = workspaceSettings.caseSensitiveFileSupport;
16231623
}
16241624

1625+
const cacheStoragePath: string = util.getCacheStoragePath();
1626+
const databaseStoragePath: string = (cacheStoragePath.length > 0) && (workspaceHash.length > 0) ?
1627+
path.join(cacheStoragePath, workspaceHash) : "";
1628+
16251629
const initializationOptions: InitializationOptions = {
16261630
packageVersion: util.packageJson.version,
16271631
extensionPath: util.extensionPath,
1628-
storagePath: this.storagePath,
1632+
databaseStoragePath: databaseStoragePath,
1633+
workspaceStoragePath: this.workspaceStoragePath,
1634+
cacheStoragePath: cacheStoragePath,
16291635
freeMemory: Math.floor(os.freemem() / 1048576),
16301636
vcpkgRoot: util.getVcpkgRoot(),
16311637
intelliSenseCacheDisabled: intelliSenseCacheDisabled,
@@ -2029,7 +2035,7 @@ export class DefaultClient implements Client {
20292035

20302036
const response: QueryTranslationUnitSourceResult = await this.languageClient.sendRequest(QueryTranslationUnitSourceRequest, params);
20312037
if (!response.candidates || response.candidates.length === 0) {
2032-
// If we didn't receive any candidates, no configuration is needed.
2038+
// If we didn't receive any candidates, no configuration is needed.
20332039
onFinished();
20342040
DefaultClient.isStarted.resolve();
20352041
return;

Extension/src/common.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1303,6 +1303,30 @@ export function isValidIdentifier(candidate: string): boolean {
13031303
return true;
13041304
}
13051305

1306+
export function getCacheStoragePath(): string {
1307+
let defaultCachePath: string = "";
1308+
let pathEnvironmentVariable: string | undefined;
1309+
switch (os.platform()) {
1310+
case 'win32':
1311+
defaultCachePath = "Microsoft\\vscode-cpptools\\";
1312+
pathEnvironmentVariable = process.env["LOCALAPPDATA"];
1313+
break;
1314+
case 'darwin':
1315+
defaultCachePath = "Library/Caches/vscode-cpptools/";
1316+
pathEnvironmentVariable = os.homedir();
1317+
break;
1318+
default: // Linux
1319+
defaultCachePath = "vscode-cpptools/";
1320+
pathEnvironmentVariable = process.env["XDG_CACHE_HOME"];
1321+
if (!pathEnvironmentVariable) {
1322+
pathEnvironmentVariable = os.homedir();
1323+
}
1324+
break;
1325+
}
1326+
1327+
return pathEnvironmentVariable ? path.join(pathEnvironmentVariable, defaultCachePath) : "";
1328+
}
1329+
13061330
function getUniqueWorkspaceNameHelper(workspaceFolder: vscode.WorkspaceFolder, addSubfolder: boolean): string {
13071331
const workspaceFolderName: string = workspaceFolder ? workspaceFolder.name : "untitled";
13081332
if (!workspaceFolder || workspaceFolder.index < 1) {

0 commit comments

Comments
 (0)