Skip to content

Commit 3d0e382

Browse files
prefer globally installed bin
1 parent 9c31f59 commit 3d0e382

File tree

3 files changed

+46
-33
lines changed

3 files changed

+46
-33
lines changed

dist/index.js

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1279,6 +1279,8 @@ const core = __importStar(__webpack_require__(470));
12791279
const tc = __importStar(__webpack_require__(533));
12801280
const installer = __importStar(__webpack_require__(749));
12811281
const path = __importStar(__webpack_require__(622));
1282+
const cp = __importStar(__webpack_require__(129));
1283+
const fs = __importStar(__webpack_require__(747));
12821284
function run() {
12831285
return __awaiter(this, void 0, void 0, function* () {
12841286
try {
@@ -1291,6 +1293,8 @@ function run() {
12911293
// since getting unstable versions should be explicit
12921294
let stable = (core.getInput('stable') || 'true').toUpperCase() === 'TRUE';
12931295
console.log(`Setup go ${stable ? 'stable' : ''} version spec ${versionSpec}`);
1296+
// if there's a globally install go and bin path, prefer that
1297+
let addedBin = addBinToPath();
12941298
if (versionSpec) {
12951299
let installDir = tc.find('go', versionSpec);
12961300
if (!installDir) {
@@ -1302,6 +1306,11 @@ function run() {
13021306
core.exportVariable('GOROOT', installDir);
13031307
core.addPath(path.join(installDir, 'bin'));
13041308
console.log('Added go to the path');
1309+
// if the global installed bin wasn't added,
1310+
// we can add the bin just installed
1311+
if (!addBinToPath) {
1312+
addBinToPath();
1313+
}
13051314
}
13061315
else {
13071316
throw new Error(`Could not find a version that satisfied version spec: ${versionSpec}`);
@@ -1317,6 +1326,19 @@ function run() {
13171326
});
13181327
}
13191328
exports.run = run;
1329+
function addBinToPath() {
1330+
let added = false;
1331+
let buf = cp.execSync('go env GOPATH');
1332+
if (buf) {
1333+
let d = buf.toString().trim();
1334+
let bp = path.join(d, 'bin');
1335+
if (fs.existsSync(bp)) {
1336+
core.addPath(bp);
1337+
added = true;
1338+
}
1339+
}
1340+
return added;
1341+
}
13201342

13211343

13221344
/***/ }),
@@ -4576,14 +4598,11 @@ var __importStar = (this && this.__importStar) || function (mod) {
45764598
};
45774599
Object.defineProperty(exports, "__esModule", { value: true });
45784600
const tc = __importStar(__webpack_require__(533));
4579-
const cm = __importStar(__webpack_require__(470));
45804601
const path = __importStar(__webpack_require__(622));
45814602
const semver = __importStar(__webpack_require__(280));
45824603
const httpm = __importStar(__webpack_require__(539));
45834604
const sys = __importStar(__webpack_require__(737));
45844605
const core_1 = __webpack_require__(470);
4585-
const cp = __importStar(__webpack_require__(129));
4586-
const fs = __importStar(__webpack_require__(747));
45874606
function downloadGo(versionSpec, stable) {
45884607
return __awaiter(this, void 0, void 0, function* () {
45894608
let toolPath;
@@ -4605,7 +4624,6 @@ function downloadGo(versionSpec, stable) {
46054624
// extracts with a root folder that matches the fileName downloaded
46064625
const toolRoot = path.join(extPath, 'go');
46074626
toolPath = yield tc.cacheDir(toolRoot, 'go', makeSemver(match.version));
4608-
addBinToPath();
46094627
}
46104628
}
46114629
catch (error) {
@@ -4615,19 +4633,6 @@ function downloadGo(versionSpec, stable) {
46154633
});
46164634
}
46174635
exports.downloadGo = downloadGo;
4618-
function addBinToPath() {
4619-
return __awaiter(this, void 0, void 0, function* () {
4620-
let buf = cp.execSync('go env GOPATH');
4621-
if (buf) {
4622-
let d = buf.toString().trim();
4623-
let bp = path.join(d, 'bin');
4624-
if (fs.existsSync(bp)) {
4625-
cm.addPath(bp);
4626-
}
4627-
}
4628-
});
4629-
}
4630-
exports.addBinToPath = addBinToPath;
46314636
function findMatch(versionSpec, stable) {
46324637
return __awaiter(this, void 0, void 0, function* () {
46334638
let archFilter = sys.getArch();

src/installer.ts

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
import * as tc from '@actions/tool-cache';
2-
import * as cm from '@actions/core';
32
import * as path from 'path';
43
import * as semver from 'semver';
54
import * as httpm from '@actions/http-client';
65
import * as sys from './system';
76
import {debug} from '@actions/core';
8-
import * as cp from 'child_process';
9-
import * as fs from 'fs';
107

118
export async function downloadGo(
129
versionSpec: string,
@@ -37,8 +34,6 @@ export async function downloadGo(
3734
// extracts with a root folder that matches the fileName downloaded
3835
const toolRoot = path.join(extPath, 'go');
3936
toolPath = await tc.cacheDir(toolRoot, 'go', makeSemver(match.version));
40-
41-
addBinToPath();
4237
}
4338
} catch (error) {
4439
throw new Error(`Failed to download version ${versionSpec}: ${error}`);
@@ -60,17 +55,6 @@ export interface IGoVersion {
6055
files: IGoVersionFile[];
6156
}
6257

63-
export async function addBinToPath() {
64-
let buf = cp.execSync('go env GOPATH');
65-
if (buf) {
66-
let d = buf.toString().trim();
67-
let bp = path.join(d, 'bin');
68-
if (fs.existsSync(bp)) {
69-
cm.addPath(bp);
70-
}
71-
}
72-
}
73-
7458
export async function findMatch(
7559
versionSpec: string,
7660
stable: boolean

src/main.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import * as core from '@actions/core';
22
import * as tc from '@actions/tool-cache';
33
import * as installer from './installer';
44
import * as path from 'path';
5+
import * as cp from 'child_process';
6+
import * as fs from 'fs';
57

68
export async function run() {
79
try {
@@ -19,6 +21,8 @@ export async function run() {
1921
`Setup go ${stable ? 'stable' : ''} version spec ${versionSpec}`
2022
);
2123

24+
// if there's a globally install go and bin path, prefer that
25+
let addedBin = addBinToPath();
2226
if (versionSpec) {
2327
let installDir: string | undefined = tc.find('go', versionSpec);
2428

@@ -34,6 +38,12 @@ export async function run() {
3438
core.exportVariable('GOROOT', installDir);
3539
core.addPath(path.join(installDir, 'bin'));
3640
console.log('Added go to the path');
41+
42+
// if the global installed bin wasn't added,
43+
// we can add the bin just installed
44+
if (!addBinToPath) {
45+
addBinToPath();
46+
}
3747
} else {
3848
throw new Error(
3949
`Could not find a version that satisfied version spec: ${versionSpec}`
@@ -48,3 +58,17 @@ export async function run() {
4858
core.setFailed(error.message);
4959
}
5060
}
61+
62+
function addBinToPath(): boolean {
63+
let added = false;
64+
let buf = cp.execSync('go env GOPATH');
65+
if (buf) {
66+
let d = buf.toString().trim();
67+
let bp = path.join(d, 'bin');
68+
if (fs.existsSync(bp)) {
69+
core.addPath(bp);
70+
added = true;
71+
}
72+
}
73+
return added;
74+
}

0 commit comments

Comments
 (0)