Skip to content

Commit 2c3dd9e

Browse files
authored
Add OS info to the error message (#559)
1 parent 76bbdfa commit 2c3dd9e

File tree

5 files changed

+123
-40
lines changed

5 files changed

+123
-40
lines changed

__tests__/cache-restore.test.ts

+4-9
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ virtualenvs.path = "{cache-dir}/virtualenvs" # /Users/patrick/Library/Caches/py
3030
let saveSatetSpy: jest.SpyInstance;
3131
let getStateSpy: jest.SpyInstance;
3232
let setOutputSpy: jest.SpyInstance;
33-
let getLinuxOSReleaseInfoSpy: jest.SpyInstance;
3433

3534
// cache spy
3635
let restoreCacheSpy: jest.SpyInstance;
@@ -67,6 +66,9 @@ virtualenvs.path = "{cache-dir}/virtualenvs" # /Users/patrick/Library/Caches/py
6766
if (input.includes('poetry')) {
6867
return {stdout: poetryConfigOutput, stderr: '', exitCode: 0};
6968
}
69+
if (input.includes('lsb_release')) {
70+
return {stdout: 'Ubuntu\n20.04', stderr: '', exitCode: 0};
71+
}
7072

7173
return {stdout: '', stderr: 'Error occured', exitCode: 2};
7274
});
@@ -83,7 +85,6 @@ virtualenvs.path = "{cache-dir}/virtualenvs" # /Users/patrick/Library/Caches/py
8385

8486
whichSpy = jest.spyOn(io, 'which');
8587
whichSpy.mockImplementation(() => '/path/to/python');
86-
getLinuxOSReleaseInfoSpy = jest.spyOn(utils, 'getLinuxOSReleaseInfo');
8788
});
8889

8990
describe('Validate provided package manager', () => {
@@ -120,17 +121,11 @@ virtualenvs.path = "{cache-dir}/virtualenvs" # /Users/patrick/Library/Caches/py
120121
dependencyFile
121122
);
122123

123-
if (process.platform === 'linux') {
124-
getLinuxOSReleaseInfoSpy.mockImplementation(() =>
125-
Promise.resolve('Ubuntu-20.4')
126-
);
127-
}
128-
129124
await cacheDistributor.restoreCache();
130125

131126
if (process.platform === 'linux' && packageManager === 'pip') {
132127
expect(infoSpy).toHaveBeenCalledWith(
133-
`Cache restored from key: setup-python-${process.env['RUNNER_OS']}-Ubuntu-20.4-python-${pythonVersion}-${packageManager}-${fileHash}`
128+
`Cache restored from key: setup-python-${process.env['RUNNER_OS']}-20.04-Ubuntu-python-${pythonVersion}-${packageManager}-${fileHash}`
134129
);
135130
} else {
136131
expect(infoSpy).toHaveBeenCalledWith(

dist/setup/index.js

+60-15
Original file line numberDiff line numberDiff line change
@@ -65943,9 +65943,9 @@ class PipCache extends cache_distributor_1.default {
6594365943
let primaryKey = '';
6594465944
let restoreKey = '';
6594565945
if (utils_1.IS_LINUX) {
65946-
const osRelease = yield utils_1.getLinuxOSReleaseInfo();
65947-
primaryKey = `${this.CACHE_KEY_PREFIX}-${process.env['RUNNER_OS']}-${osRelease}-python-${this.pythonVersion}-${this.packageManager}-${hash}`;
65948-
restoreKey = `${this.CACHE_KEY_PREFIX}-${process.env['RUNNER_OS']}-${osRelease}-python-${this.pythonVersion}-${this.packageManager}`;
65946+
const osInfo = yield utils_1.getLinuxInfo();
65947+
primaryKey = `${this.CACHE_KEY_PREFIX}-${process.env['RUNNER_OS']}-${osInfo.osVersion}-${osInfo.osName}-python-${this.pythonVersion}-${this.packageManager}-${hash}`;
65948+
restoreKey = `${this.CACHE_KEY_PREFIX}-${process.env['RUNNER_OS']}-${osInfo.osVersion}-${osInfo.osName}-python-${this.pythonVersion}-${this.packageManager}`;
6594965949
}
6595065950
else {
6595165951
primaryKey = `${this.CACHE_KEY_PREFIX}-${process.env['RUNNER_OS']}-python-${this.pythonVersion}-${this.packageManager}-${hash}`;
@@ -66401,8 +66401,11 @@ function useCpythonVersion(version, architecture, updateEnvironment, checkLatest
6640166401
}
6640266402
}
6640366403
if (!installDir) {
66404+
const osInfo = yield utils_1.getOSInfo();
6640466405
throw new Error([
66405-
`Version ${version} with arch ${architecture} not found`,
66406+
`The version '${version}' with architecture '${architecture}' was not found for ${osInfo
66407+
? `${osInfo.osName} ${osInfo.osVersion}`
66408+
: 'this operating system'}.`,
6640666409
`The list of all available versions can be found here: ${installer.MANIFEST_URL}`
6640766410
].join(os.EOL));
6640866411
}
@@ -66975,7 +66978,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
6697566978
return (mod && mod.__esModule) ? mod : { "default": mod };
6697666979
};
6697766980
Object.defineProperty(exports, "__esModule", ({ value: true }));
66978-
exports.logWarning = exports.getLinuxOSReleaseInfo = exports.isCacheFeatureAvailable = exports.isGhes = exports.validatePythonVersionFormatForPyPy = exports.writeExactPyPyVersionFile = exports.readExactPyPyVersionFile = exports.getPyPyVersionFromPath = exports.isNightlyKeyword = exports.validateVersion = exports.createSymlinkInFolder = exports.WINDOWS_PLATFORMS = exports.WINDOWS_ARCHS = exports.IS_MAC = exports.IS_LINUX = exports.IS_WINDOWS = void 0;
66981+
exports.getOSInfo = exports.getLinuxInfo = exports.logWarning = exports.isCacheFeatureAvailable = exports.isGhes = exports.validatePythonVersionFormatForPyPy = exports.writeExactPyPyVersionFile = exports.readExactPyPyVersionFile = exports.getPyPyVersionFromPath = exports.isNightlyKeyword = exports.validateVersion = exports.createSymlinkInFolder = exports.WINDOWS_PLATFORMS = exports.WINDOWS_ARCHS = exports.IS_MAC = exports.IS_LINUX = exports.IS_WINDOWS = void 0;
6697966982
const cache = __importStar(__nccwpck_require__(7799));
6698066983
const core = __importStar(__nccwpck_require__(2186));
6698166984
const fs_1 = __importDefault(__nccwpck_require__(7147));
@@ -67066,22 +67069,64 @@ function isCacheFeatureAvailable() {
6706667069
return true;
6706767070
}
6706867071
exports.isCacheFeatureAvailable = isCacheFeatureAvailable;
67069-
function getLinuxOSReleaseInfo() {
67072+
function logWarning(message) {
67073+
const warningPrefix = '[warning]';
67074+
core.info(`${warningPrefix}${message}`);
67075+
}
67076+
exports.logWarning = logWarning;
67077+
function getWindowsInfo() {
6707067078
return __awaiter(this, void 0, void 0, function* () {
67071-
const { stdout, stderr, exitCode } = yield exec.getExecOutput('lsb_release', ['-i', '-r', '-s'], {
67079+
const { stdout } = yield exec.getExecOutput('powershell -command "(Get-CimInstance -ClassName Win32_OperatingSystem).Caption"', undefined, {
6707267080
silent: true
6707367081
});
67074-
const [osRelease, osVersion] = stdout.trim().split('\n');
67075-
core.debug(`OS Release: ${osRelease}, Version: ${osVersion}`);
67076-
return `${osVersion}-${osRelease}`;
67082+
const windowsVersion = stdout.trim().split(' ')[3];
67083+
return { osName: 'Windows', osVersion: windowsVersion };
6707767084
});
6707867085
}
67079-
exports.getLinuxOSReleaseInfo = getLinuxOSReleaseInfo;
67080-
function logWarning(message) {
67081-
const warningPrefix = '[warning]';
67082-
core.info(`${warningPrefix}${message}`);
67086+
function getMacOSInfo() {
67087+
return __awaiter(this, void 0, void 0, function* () {
67088+
const { stdout } = yield exec.getExecOutput('sw_vers', ['-productVersion'], {
67089+
silent: true
67090+
});
67091+
const macOSVersion = stdout.trim();
67092+
return { osName: 'macOS', osVersion: macOSVersion };
67093+
});
6708367094
}
67084-
exports.logWarning = logWarning;
67095+
function getLinuxInfo() {
67096+
return __awaiter(this, void 0, void 0, function* () {
67097+
const { stdout } = yield exec.getExecOutput('lsb_release', ['-i', '-r', '-s'], {
67098+
silent: true
67099+
});
67100+
const [osName, osVersion] = stdout.trim().split('\n');
67101+
core.debug(`OS Name: ${osName}, Version: ${osVersion}`);
67102+
return { osName: osName, osVersion: osVersion };
67103+
});
67104+
}
67105+
exports.getLinuxInfo = getLinuxInfo;
67106+
function getOSInfo() {
67107+
return __awaiter(this, void 0, void 0, function* () {
67108+
let osInfo;
67109+
try {
67110+
if (exports.IS_WINDOWS) {
67111+
osInfo = yield getWindowsInfo();
67112+
}
67113+
else if (exports.IS_LINUX) {
67114+
osInfo = yield getLinuxInfo();
67115+
}
67116+
else if (exports.IS_MAC) {
67117+
osInfo = yield getMacOSInfo();
67118+
}
67119+
}
67120+
catch (err) {
67121+
const error = err;
67122+
core.debug(error.message);
67123+
}
67124+
finally {
67125+
return osInfo;
67126+
}
67127+
});
67128+
}
67129+
exports.getOSInfo = getOSInfo;
6708567130

6708667131

6708767132
/***/ }),

src/cache-distributions/pip-cache.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import * as path from 'path';
77
import os from 'os';
88

99
import CacheDistributor from './cache-distributor';
10-
import {getLinuxOSReleaseInfo, IS_LINUX, IS_WINDOWS} from '../utils';
10+
import {getLinuxInfo, IS_LINUX, IS_WINDOWS} from '../utils';
1111

1212
class PipCache extends CacheDistributor {
1313
constructor(
@@ -61,9 +61,9 @@ class PipCache extends CacheDistributor {
6161
let restoreKey = '';
6262

6363
if (IS_LINUX) {
64-
const osRelease = await getLinuxOSReleaseInfo();
65-
primaryKey = `${this.CACHE_KEY_PREFIX}-${process.env['RUNNER_OS']}-${osRelease}-python-${this.pythonVersion}-${this.packageManager}-${hash}`;
66-
restoreKey = `${this.CACHE_KEY_PREFIX}-${process.env['RUNNER_OS']}-${osRelease}-python-${this.pythonVersion}-${this.packageManager}`;
64+
const osInfo = await getLinuxInfo();
65+
primaryKey = `${this.CACHE_KEY_PREFIX}-${process.env['RUNNER_OS']}-${osInfo.osVersion}-${osInfo.osName}-python-${this.pythonVersion}-${this.packageManager}-${hash}`;
66+
restoreKey = `${this.CACHE_KEY_PREFIX}-${process.env['RUNNER_OS']}-${osInfo.osVersion}-${osInfo.osName}-python-${this.pythonVersion}-${this.packageManager}`;
6767
} else {
6868
primaryKey = `${this.CACHE_KEY_PREFIX}-${process.env['RUNNER_OS']}-python-${this.pythonVersion}-${this.packageManager}-${hash}`;
6969
restoreKey = `${this.CACHE_KEY_PREFIX}-${process.env['RUNNER_OS']}-python-${this.pythonVersion}-${this.packageManager}`;

src/find-python.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as os from 'os';
22
import * as path from 'path';
3-
import {IS_WINDOWS, IS_LINUX} from './utils';
3+
import {IS_WINDOWS, IS_LINUX, getOSInfo} from './utils';
44

55
import * as semver from 'semver';
66

@@ -85,9 +85,14 @@ export async function useCpythonVersion(
8585
}
8686

8787
if (!installDir) {
88+
const osInfo = await getOSInfo();
8889
throw new Error(
8990
[
90-
`Version ${version} with arch ${architecture} not found`,
91+
`The version '${version}' with architecture '${architecture}' was not found for ${
92+
osInfo
93+
? `${osInfo.osName} ${osInfo.osVersion}`
94+
: 'this operating system'
95+
}.`,
9196
`The list of all available versions can be found here: ${installer.MANIFEST_URL}`
9297
].join(os.EOL)
9398
);

src/utils.ts

+48-10
Original file line numberDiff line numberDiff line change
@@ -122,23 +122,61 @@ export function isCacheFeatureAvailable(): boolean {
122122
return true;
123123
}
124124

125-
export async function getLinuxOSReleaseInfo() {
126-
const {stdout, stderr, exitCode} = await exec.getExecOutput(
127-
'lsb_release',
128-
['-i', '-r', '-s'],
125+
export function logWarning(message: string): void {
126+
const warningPrefix = '[warning]';
127+
core.info(`${warningPrefix}${message}`);
128+
}
129+
130+
async function getWindowsInfo() {
131+
const {stdout} = await exec.getExecOutput(
132+
'powershell -command "(Get-CimInstance -ClassName Win32_OperatingSystem).Caption"',
133+
undefined,
129134
{
130135
silent: true
131136
}
132137
);
133138

134-
const [osRelease, osVersion] = stdout.trim().split('\n');
139+
const windowsVersion = stdout.trim().split(' ')[3];
135140

136-
core.debug(`OS Release: ${osRelease}, Version: ${osVersion}`);
141+
return {osName: 'Windows', osVersion: windowsVersion};
142+
}
143+
144+
async function getMacOSInfo() {
145+
const {stdout} = await exec.getExecOutput('sw_vers', ['-productVersion'], {
146+
silent: true
147+
});
137148

138-
return `${osVersion}-${osRelease}`;
149+
const macOSVersion = stdout.trim();
150+
151+
return {osName: 'macOS', osVersion: macOSVersion};
139152
}
140153

141-
export function logWarning(message: string): void {
142-
const warningPrefix = '[warning]';
143-
core.info(`${warningPrefix}${message}`);
154+
export async function getLinuxInfo() {
155+
const {stdout} = await exec.getExecOutput('lsb_release', ['-i', '-r', '-s'], {
156+
silent: true
157+
});
158+
159+
const [osName, osVersion] = stdout.trim().split('\n');
160+
161+
core.debug(`OS Name: ${osName}, Version: ${osVersion}`);
162+
163+
return {osName: osName, osVersion: osVersion};
164+
}
165+
166+
export async function getOSInfo() {
167+
let osInfo;
168+
try {
169+
if (IS_WINDOWS) {
170+
osInfo = await getWindowsInfo();
171+
} else if (IS_LINUX) {
172+
osInfo = await getLinuxInfo();
173+
} else if (IS_MAC) {
174+
osInfo = await getMacOSInfo();
175+
}
176+
} catch (err) {
177+
const error = err as Error;
178+
core.debug(error.message);
179+
} finally {
180+
return osInfo;
181+
}
144182
}

0 commit comments

Comments
 (0)