Skip to content
This repository was archived by the owner on Jul 15, 2023. It is now read-only.

Commit ec11985

Browse files
committed
persist temp dir
1 parent bf3174b commit ec11985

7 files changed

Lines changed: 37 additions & 24 deletions

File tree

src/debugAdapter/goDebug.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import { basename, dirname, extname } from 'path';
1212
import { spawn, ChildProcess, execSync, spawnSync, execFile } from 'child_process';
1313
import { Client, RPCConnection } from 'json-rpc2';
1414
import { parseEnvFile, getBinPathWithPreferredGopath, resolveHomeDir, getInferredGopath, getCurrentGoWorkspaceFromGOPATH, envPath, fixDriveCasingInWindows } from '../goPath';
15-
import { getTempFile } from '../util';
1615
import * as logger from 'vscode-debug-logger';
1716

1817
require('console-stamp')(console);
@@ -532,7 +531,7 @@ class GoDebugSession extends DebugSession {
532531
this.delve = null;
533532
this.breakpoints = new Map<string, DebugBreakpoint[]>();
534533

535-
const logPath = getTempFile('vscode-go-debug.txt');
534+
const logPath = path.join(os.tmpdir(), 'vscode-go-debug.txt');
536535
logger.init(e => this.sendEvent(e), logPath, isServer);
537536
}
538537

src/goBuild.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import path = require('path');
22
import vscode = require('vscode');
3-
import { getToolsEnvVars, runTool, ICheckResult, handleDiagnosticErrors, getWorkspaceFolderPath, getCurrentGoPath, getUserNameHash, getTempFile } from './util';
3+
import { getToolsEnvVars, runTool, ICheckResult, handleDiagnosticErrors, getWorkspaceFolderPath, getCurrentGoPath, TempFileProvider } from './util';
44
import { outputChannel } from './goStatus';
55
import os = require('os');
66
import { getNonVendorPackages } from './goPackages';
@@ -70,7 +70,7 @@ export function goBuild(fileUri: vscode.Uri, goConfig: vscode.WorkspaceConfigura
7070
}
7171

7272
const buildEnv = Object.assign({}, getToolsEnvVars());
73-
const tmpPath = getTempFile('go-code-check.' + getUserNameHash());
73+
const tmpPath = TempFileProvider.getFilePath('go-code-check');
7474
const isTestFile = fileUri && fileUri.fsPath.endsWith('_test.go');
7575
const buildFlags: string[] = isTestFile ? getTestFlags(goConfig, null) : (Array.isArray(goConfig['buildFlags']) ? [...goConfig['buildFlags']] : []);
7676
const buildArgs: string[] = isTestFile ? ['test', '-c'] : ['build'];

src/goCheck.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import os = require('os');
1111
import { getCoverage } from './goCover';
1212
import { outputChannel, diagnosticsStatusBarItem } from './goStatus';
1313
import { goTest } from './testUtils';
14-
import { ICheckResult, getBinPath, getTempFile } from './util';
14+
import { ICheckResult, getBinPath, TempFileProvider } from './util';
1515
import { goLint } from './goLint';
1616
import { goVet } from './goVet';
1717
import { goBuild } from './goBuild';
@@ -69,7 +69,7 @@ export function check(fileUri: vscode.Uri, goConfig: vscode.WorkspaceConfigurati
6969

7070
let args = [...buildFlags];
7171
if (goConfig['coverOnSave']) {
72-
tmpCoverPath = getTempFile('go-code-cover');
72+
tmpCoverPath = TempFileProvider.getFilePath('go-code-cover');
7373
args = ['-coverprofile=' + tmpCoverPath, ...buildFlags];
7474
}
7575

src/goCover.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import vscode = require('vscode');
88
import path = require('path');
99
import os = require('os');
1010
import fs = require('fs');
11-
import { getTempFile } from './util';
11+
import { TempFileProvider } from './util';
1212
import { showTestOutput, goTest } from './testUtils';
1313
import rl = require('readline');
1414

@@ -113,7 +113,7 @@ export function toggleCoverageCurrentPackage() {
113113
let cwd = path.dirname(editor.document.uri.fsPath);
114114

115115
let buildFlags = goConfig['testFlags'] || goConfig['buildFlags'] || [];
116-
let tmpCoverPath = getTempFile('go-code-cover');
116+
let tmpCoverPath = TempFileProvider.getFilePath('go-code-cover');
117117
let args = ['-coverprofile=' + tmpCoverPath, ...buildFlags];
118118
return goTest({
119119
goConfig: goConfig,

src/goMain.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import { showTestOutput, cancelRunningTests } from './testUtils';
2929
import * as goGenerateTests from './goGenerateTests';
3030
import { addImport, addImportToWorkspace } from './goImport';
3131
import { installAllTools, checkLanguageServer } from './goInstallTools';
32-
import { isGoPathSet, getBinPath, sendTelemetryEvent, getExtensionCommands, getGoVersion, getCurrentGoPath, getToolsGopath, handleDiagnosticErrors, disposeTelemetryReporter, getToolsEnvVars } from './util';
32+
import { isGoPathSet, getBinPath, sendTelemetryEvent, getExtensionCommands, getGoVersion, getCurrentGoPath, getToolsGopath, handleDiagnosticErrors, disposeTelemetryReporter, getToolsEnvVars, TempFileProvider } from './util';
3333
import { LanguageClient, RevealOutputChannelOn, FormattingOptions, ProvideDocumentFormattingEditsSignature, ProvideCompletionItemsSignature } from 'vscode-languageclient';
3434
import { clearCacheForTools, fixDriveCasingInWindows } from './goPath';
3535
import { addTags, removeTags } from './goModifytags';
@@ -83,6 +83,7 @@ export function activate(ctx: vscode.ExtensionContext): void {
8383
});
8484
}
8585
ctx.globalState.update('goroot', currentGoroot);
86+
TempFileProvider.registerStore(ctx.globalState);
8687

8788
offerToInstallTools();
8889
if (checkLanguageServer()) {

src/goTest.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import path = require('path');
99
import vscode = require('vscode');
1010
import os = require('os');
11-
import { getTempFile } from './util';
11+
import { TempFileProvider } from './util';
1212
import { goTest, TestConfig, getTestFlags, getTestFunctions, getBenchmarkFunctions, extractInstanceTestName, findAllTestSuiteRuns } from './testUtils';
1313
import { getCoverage } from './goCover';
1414

@@ -213,7 +213,7 @@ function makeCoverData(goConfig: vscode.WorkspaceConfiguration, confFlag: string
213213
let tmpCoverPath = '';
214214
let testFlags = getTestFlags(goConfig, args) || [];
215215
if (goConfig[confFlag] === true) {
216-
tmpCoverPath = getTempFile('go-code-cover');
216+
tmpCoverPath = TempFileProvider.getFilePath('go-code-cover');
217217
testFlags.push('-coverprofile=' + tmpCoverPath);
218218
}
219219

src/util.ts

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -844,19 +844,32 @@ export function makeMemoizedByteOffsetConverter(buffer: Buffer): (byteOffset: nu
844844
};
845845
}
846846

847-
export const getTempDir = (() => {
848-
let dir: string | undefined;
849-
return (): string => {
850-
if (!dir) {
851-
dir = fs.mkdtempSync(os.tmpdir() + '/vscode-go');
852-
if (!fs.existsSync(dir)) {
853-
fs.mkdirSync(dir);
854-
}
847+
export class TempFileProvider {
848+
private static globalState: vscode.Memento;
849+
850+
/**
851+
* register store to provider for persistance
852+
*/
853+
static registerStore(globalState: vscode.Memento) {
854+
TempFileProvider.globalState = globalState;
855+
}
856+
857+
/**
858+
* returns path to temp file with name
859+
*/
860+
static getFilePath(name: string): string {
861+
let tempDir = TempFileProvider.globalState.get<string>('tempDir');
862+
console.log(tempDir);
863+
864+
if (!tempDir) {
865+
tempDir = fs.mkdtempSync(os.tmpdir() + path.sep + 'vscode-go');
866+
TempFileProvider.globalState.update('tempDir', tempDir);
855867
}
856-
return dir;
857-
};
858-
})();
859868

860-
export function getTempFile(name: string): string {
861-
return path.normalize(path.join(getTempDir(), name));
869+
if (!fs.existsSync(tempDir)) {
870+
fs.mkdirSync(tempDir);
871+
}
872+
873+
return path.normalize(path.join(tempDir, name));
874+
}
862875
}

0 commit comments

Comments
 (0)