diff --git a/src/debugAdapter/goDebug.ts b/src/debugAdapter/goDebug.ts index 1bbfb52b9..6228da103 100644 --- a/src/debugAdapter/goDebug.ts +++ b/src/debugAdapter/goDebug.ts @@ -12,7 +12,6 @@ import { basename, dirname, extname } from 'path'; import { spawn, ChildProcess, execSync, spawnSync, execFile } from 'child_process'; import { Client, RPCConnection } from 'json-rpc2'; import { parseEnvFile, getBinPathWithPreferredGopath, resolveHomeDir, getInferredGopath, getCurrentGoWorkspaceFromGOPATH, envPath, fixDriveCasingInWindows } from '../goPath'; -import { getTempFile } from '../util'; import * as logger from 'vscode-debug-logger'; require('console-stamp')(console); @@ -532,7 +531,7 @@ class GoDebugSession extends DebugSession { this.delve = null; this.breakpoints = new Map(); - const logPath = getTempFile('vscode-go-debug.txt'); + const logPath = path.join(os.tmpdir(), 'vscode-go-debug.txt'); logger.init(e => this.sendEvent(e), logPath, isServer); } diff --git a/src/goBuild.ts b/src/goBuild.ts index fa5d6a46a..6fb94a02b 100644 --- a/src/goBuild.ts +++ b/src/goBuild.ts @@ -1,6 +1,6 @@ import path = require('path'); import vscode = require('vscode'); -import { getToolsEnvVars, runTool, ICheckResult, handleDiagnosticErrors, getWorkspaceFolderPath, getCurrentGoPath, getUserNameHash, getTempFile } from './util'; +import { getToolsEnvVars, runTool, ICheckResult, handleDiagnosticErrors, getWorkspaceFolderPath, getCurrentGoPath, TempFileProvider } from './util'; import { outputChannel } from './goStatus'; import os = require('os'); import { getNonVendorPackages } from './goPackages'; @@ -70,7 +70,7 @@ export function goBuild(fileUri: vscode.Uri, goConfig: vscode.WorkspaceConfigura } const buildEnv = Object.assign({}, getToolsEnvVars()); - const tmpPath = getTempFile('go-code-check.' + getUserNameHash()); + const tmpPath = TempFileProvider.getFilePath('go-code-check'); const isTestFile = fileUri && fileUri.fsPath.endsWith('_test.go'); const buildFlags: string[] = isTestFile ? getTestFlags(goConfig, null) : (Array.isArray(goConfig['buildFlags']) ? [...goConfig['buildFlags']] : []); const buildArgs: string[] = isTestFile ? ['test', '-c'] : ['build']; diff --git a/src/goCheck.ts b/src/goCheck.ts index 2e099b1e3..d15d9e553 100644 --- a/src/goCheck.ts +++ b/src/goCheck.ts @@ -11,7 +11,7 @@ import os = require('os'); import { getCoverage } from './goCover'; import { outputChannel, diagnosticsStatusBarItem } from './goStatus'; import { goTest } from './testUtils'; -import { ICheckResult, getBinPath, getTempFile } from './util'; +import { ICheckResult, getBinPath, TempFileProvider } from './util'; import { goLint } from './goLint'; import { goVet } from './goVet'; import { goBuild } from './goBuild'; @@ -69,7 +69,7 @@ export function check(fileUri: vscode.Uri, goConfig: vscode.WorkspaceConfigurati let args = [...buildFlags]; if (goConfig['coverOnSave']) { - tmpCoverPath = getTempFile('go-code-cover'); + tmpCoverPath = TempFileProvider.getFilePath('go-code-cover'); args = ['-coverprofile=' + tmpCoverPath, ...buildFlags]; } diff --git a/src/goCover.ts b/src/goCover.ts index 12d45fa3f..8bc0d02d4 100644 --- a/src/goCover.ts +++ b/src/goCover.ts @@ -8,7 +8,7 @@ import vscode = require('vscode'); import path = require('path'); import os = require('os'); import fs = require('fs'); -import { getTempFile } from './util'; +import { TempFileProvider } from './util'; import { showTestOutput, goTest } from './testUtils'; import rl = require('readline'); @@ -113,7 +113,7 @@ export function toggleCoverageCurrentPackage() { let cwd = path.dirname(editor.document.uri.fsPath); let buildFlags = goConfig['testFlags'] || goConfig['buildFlags'] || []; - let tmpCoverPath = getTempFile('go-code-cover'); + let tmpCoverPath = TempFileProvider.getFilePath('go-code-cover'); let args = ['-coverprofile=' + tmpCoverPath, ...buildFlags]; return goTest({ goConfig: goConfig, diff --git a/src/goMain.ts b/src/goMain.ts index 7c411afaa..2d9823ef9 100644 --- a/src/goMain.ts +++ b/src/goMain.ts @@ -29,7 +29,7 @@ import { showTestOutput, cancelRunningTests } from './testUtils'; import * as goGenerateTests from './goGenerateTests'; import { addImport, addImportToWorkspace } from './goImport'; import { installAllTools, checkLanguageServer } from './goInstallTools'; -import { isGoPathSet, getBinPath, sendTelemetryEvent, getExtensionCommands, getGoVersion, getCurrentGoPath, getToolsGopath, handleDiagnosticErrors, disposeTelemetryReporter, getToolsEnvVars } from './util'; +import { isGoPathSet, getBinPath, sendTelemetryEvent, getExtensionCommands, getGoVersion, getCurrentGoPath, getToolsGopath, handleDiagnosticErrors, disposeTelemetryReporter, getToolsEnvVars, TempFileProvider } from './util'; import { LanguageClient, RevealOutputChannelOn, FormattingOptions, ProvideDocumentFormattingEditsSignature, ProvideCompletionItemsSignature } from 'vscode-languageclient'; import { clearCacheForTools, fixDriveCasingInWindows } from './goPath'; import { addTags, removeTags } from './goModifytags'; @@ -83,6 +83,7 @@ export function activate(ctx: vscode.ExtensionContext): void { }); } ctx.globalState.update('goroot', currentGoroot); + TempFileProvider.registerStore(ctx.globalState); offerToInstallTools(); if (checkLanguageServer()) { diff --git a/src/goTest.ts b/src/goTest.ts index e9bd4d785..20dc7db39 100644 --- a/src/goTest.ts +++ b/src/goTest.ts @@ -8,7 +8,7 @@ import path = require('path'); import vscode = require('vscode'); import os = require('os'); -import { getTempFile } from './util'; +import { TempFileProvider } from './util'; import { goTest, TestConfig, getTestFlags, getTestFunctions, getBenchmarkFunctions, extractInstanceTestName, findAllTestSuiteRuns } from './testUtils'; import { getCoverage } from './goCover'; @@ -213,7 +213,7 @@ function makeCoverData(goConfig: vscode.WorkspaceConfiguration, confFlag: string let tmpCoverPath = ''; let testFlags = getTestFlags(goConfig, args) || []; if (goConfig[confFlag] === true) { - tmpCoverPath = getTempFile('go-code-cover'); + tmpCoverPath = TempFileProvider.getFilePath('go-code-cover'); testFlags.push('-coverprofile=' + tmpCoverPath); } diff --git a/src/util.ts b/src/util.ts index 60e4dae4e..3936d95e1 100644 --- a/src/util.ts +++ b/src/util.ts @@ -844,19 +844,32 @@ export function makeMemoizedByteOffsetConverter(buffer: Buffer): (byteOffset: nu }; } -export const getTempDir = (() => { - let dir: string | undefined; - return (): string => { - if (!dir) { - dir = fs.mkdtempSync(os.tmpdir() + '/vscode-go'); - if (!fs.existsSync(dir)) { - fs.mkdirSync(dir); - } +export class TempFileProvider { + private static globalState: vscode.Memento; + + /** + * register store to provider for persistance + */ + static registerStore(globalState: vscode.Memento) { + TempFileProvider.globalState = globalState; + } + + /** + * returns path to temp file with name + */ + static getFilePath(name: string): string { + let tempDir = TempFileProvider.globalState.get('tempDir'); + console.log(tempDir); + + if (!tempDir) { + tempDir = fs.mkdtempSync(os.tmpdir() + path.sep + 'vscode-go'); + TempFileProvider.globalState.update('tempDir', tempDir); } - return dir; - }; -})(); -export function getTempFile(name: string): string { - return path.normalize(path.join(getTempDir(), name)); + if (!fs.existsSync(tempDir)) { + fs.mkdirSync(tempDir); + } + + return path.normalize(path.join(tempDir, name)); + } }