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

Commit

Permalink
Make temporary build filename user specific to prevent conflict on sy…
Browse files Browse the repository at this point in the history
…stems with shared temp folders (#1835)

* fixes #1829

* Added a function to generate a hash based on the current users username

* forgot to lint
  • Loading branch information
rikkuness authored and ramya-rao-a committed Aug 13, 2018
1 parent a1d1266 commit 1513adf
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/goBuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { outputChannel } from './goStatus';
import os = require('os');
import { getNonVendorPackages } from './goPackages';
import { getTestFlags } from './testUtils';
import { getUsernameHash } from './util';
import { getCurrentGoWorkspaceFromGOPATH } from './goPath';
import { diagnosticsStatusBarItem } from './goStatus';
/**
Expand Down Expand Up @@ -56,7 +57,7 @@ export function goBuild(fileUri: vscode.Uri, goConfig: vscode.WorkspaceConfigura
}

const buildEnv = Object.assign({}, getToolsEnvVars());
const tmpPath = path.normalize(path.join(os.tmpdir(), 'go-code-check'));
const tmpPath = path.normalize(path.join(os.tmpdir(), 'go-code-check.' + getUsernameHash()));
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'];
Expand Down
19 changes: 19 additions & 0 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,25 @@ export function canonicalizeGOPATHPrefix(filename: string): string {
return currentWorkspace + filename.slice(currentWorkspace.length);
}

/**
* Gets a numeric hash based on the current users username.
* Returns a number between 0 and 4294967295.
*/
export function getUsernameHash(): number {
const user: string = os.userInfo().username;
let hash = 5381,
i = user.length;

while (i) {
hash = (hash * 33) ^ user.charCodeAt(--i);
}

/* JavaScript does bitwise operations (like XOR, above) on 32-bit signed
* integers. Since we want the results to be always positive, convert the
* signed int to an unsigned by doing an unsigned bitshift. */
return hash >>> 0;
}

/**
* Gets version of Go based on the output of the command `go version`.
* Returns null if go is being used from source/tip in which case `go version` will not return release tag like go1.6.3
Expand Down

0 comments on commit 1513adf

Please # to comment.