From d03d4fe9c646cb6cb5862540c0f97ca90e01a6f4 Mon Sep 17 00:00:00 2001 From: shaanmugapriya Date: Mon, 12 Feb 2024 14:10:45 +0530 Subject: [PATCH 1/3] Updated code to fix 501 --- dist/setup/index.js | 16 +++++++++++++--- setup-dotnet.sln | 30 ++++++++++++++++++++++++++++++ src/setup-dotnet.ts | 14 +++++++++++++- 3 files changed, 56 insertions(+), 4 deletions(-) create mode 100644 setup-dotnet.sln diff --git a/dist/setup/index.js b/dist/setup/index.js index 6070fed..5ec6189 100644 --- a/dist/setup/index.js +++ b/dist/setup/index.js @@ -93335,6 +93335,10 @@ const qualityOptions = [ 'preview', 'ga' ]; +let cancelled = false; +process.on('SIGINT', () => { + cancelled = true; +}); function run() { return __awaiter(this, void 0, void 0, function* () { try { @@ -93358,8 +93362,6 @@ function run() { versions.push(getVersionFromGlobalJson(globalJsonPath)); } if (!versions.length) { - // Try to fall back to global.json - core.debug('No version found, trying to find version from global.json'); const globalJsonPath = path_1.default.join(process.cwd(), 'global.json'); if (fs.existsSync(globalJsonPath)) { versions.push(getVersionFromGlobalJson(globalJsonPath)); @@ -93376,6 +93378,9 @@ function run() { let dotnetInstaller; const uniqueVersions = new Set(versions); for (const version of uniqueVersions) { + if (cancelled) { + throw new Error('Cancelled'); + } dotnetInstaller = new installer_1.DotnetCoreInstaller(version, quality); const installedVersion = yield dotnetInstaller.installDotnet(); installedDotnetVersions.push(installedVersion); @@ -93396,7 +93401,12 @@ function run() { core.info(`##[add-matcher]${path_1.default.join(matchersPath, 'csc.json')}`); } catch (error) { - core.setFailed(error.message); + if (error.message === 'Cancelled') { + console.log('Cleaning up...'); + } + else { + core.setFailed(error.message); + } } }); } diff --git a/setup-dotnet.sln b/setup-dotnet.sln new file mode 100644 index 0000000..a8cae1d --- /dev/null +++ b/setup-dotnet.sln @@ -0,0 +1,30 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.5.002.0 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "__tests__", "__tests__", "{4EBCF652-9B47-4590-A4F4-9AFC5FB9AB16}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "test", "__tests__\e2e-test-csproj\test.csproj", "{BF96E0E1-5384-4445-92C8-B9B999FD812E}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {BF96E0E1-5384-4445-92C8-B9B999FD812E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {BF96E0E1-5384-4445-92C8-B9B999FD812E}.Debug|Any CPU.Build.0 = Debug|Any CPU + {BF96E0E1-5384-4445-92C8-B9B999FD812E}.Release|Any CPU.ActiveCfg = Release|Any CPU + {BF96E0E1-5384-4445-92C8-B9B999FD812E}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(NestedProjects) = preSolution + {BF96E0E1-5384-4445-92C8-B9B999FD812E} = {4EBCF652-9B47-4590-A4F4-9AFC5FB9AB16} + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {7249446B-27CA-4F79-B35C-1575C52AFB5D} + EndGlobalSection +EndGlobal diff --git a/src/setup-dotnet.ts b/src/setup-dotnet.ts index 2a628a5..ab184bf 100644 --- a/src/setup-dotnet.ts +++ b/src/setup-dotnet.ts @@ -19,6 +19,11 @@ const qualityOptions = [ export type QualityOptions = (typeof qualityOptions)[number]; +let cancelled = false; +process.on('SIGINT', () => { + cancelled = true; +}); + export async function run() { try { // @@ -69,6 +74,9 @@ export async function run() { let dotnetInstaller: DotnetCoreInstaller; const uniqueVersions = new Set(versions); for (const version of uniqueVersions) { + if (cancelled) { + throw new Error('Cancelled'); + } dotnetInstaller = new DotnetCoreInstaller(version, quality); const installedVersion = await dotnetInstaller.installDotnet(); installedDotnetVersions.push(installedVersion); @@ -92,7 +100,11 @@ export async function run() { const matchersPath = path.join(__dirname, '..', '..', '.github'); core.info(`##[add-matcher]${path.join(matchersPath, 'csc.json')}`); } catch (error) { - core.setFailed(error.message); + if (error.message === 'Cancelled') { + 'Cleaning up...'; + } else { + core.setFailed(error.message); + } } } From 66808a5f19ef0198d6258bacb46c4d581c913245 Mon Sep 17 00:00:00 2001 From: shaanmugapriya Date: Mon, 12 Feb 2024 18:28:15 +0530 Subject: [PATCH 2/3] Applied code to delete the file if cancelled the workflow --- dist/setup/index.js | 29 +++++++++++++++++++++++++---- src/setup-dotnet.ts | 31 +++++++++++++++++++++++++------ 2 files changed, 50 insertions(+), 10 deletions(-) diff --git a/dist/setup/index.js b/dist/setup/index.js index 5ec6189..070967d 100644 --- a/dist/setup/index.js +++ b/dist/setup/index.js @@ -93328,6 +93328,7 @@ const cache_utils_1 = __nccwpck_require__(1678); const cache_restore_1 = __nccwpck_require__(9517); const constants_1 = __nccwpck_require__(9042); const json5_1 = __importDefault(__nccwpck_require__(6904)); +const os = __importStar(__nccwpck_require__(2037)); const qualityOptions = [ 'daily', 'signed', @@ -93336,6 +93337,7 @@ const qualityOptions = [ 'ga' ]; let cancelled = false; +let errorOccurred = false; process.on('SIGINT', () => { cancelled = true; }); @@ -93362,6 +93364,8 @@ function run() { versions.push(getVersionFromGlobalJson(globalJsonPath)); } if (!versions.length) { + // Try to fall back to global.json + core.debug('No version found, trying to find version from global.json'); const globalJsonPath = path_1.default.join(process.cwd(), 'global.json'); if (fs.existsSync(globalJsonPath)) { versions.push(getVersionFromGlobalJson(globalJsonPath)); @@ -93401,11 +93405,28 @@ function run() { core.info(`##[add-matcher]${path_1.default.join(matchersPath, 'csc.json')}`); } catch (error) { - if (error.message === 'Cancelled') { + core.setFailed(error.message); + errorOccurred = true; + } + finally { + if (errorOccurred || cancelled) { console.log('Cleaning up...'); - } - else { - core.setFailed(error.message); + let directoryPath; + switch (os.platform()) { + case 'win32': + directoryPath = 'C:\\Program Files\\dotnet'; + break; + case 'darwin': + directoryPath = '/usr/local/share/dotnet'; + break; + case 'linux': + directoryPath = '/usr/share/dotnet'; + break; + default: + throw new Error(`Unsupported platform: ${os.platform()}`); + } + fs.rmdirSync(directoryPath, { recursive: true }); + console.log(`Directory ${directoryPath} has been deleted.`); } } }); diff --git a/src/setup-dotnet.ts b/src/setup-dotnet.ts index ab184bf..edd9f96 100644 --- a/src/setup-dotnet.ts +++ b/src/setup-dotnet.ts @@ -8,6 +8,7 @@ import {isCacheFeatureAvailable} from './cache-utils'; import {restoreCache} from './cache-restore'; import {Outputs} from './constants'; import JSON5 from 'json5'; +import * as os from 'os'; const qualityOptions = [ 'daily', @@ -20,6 +21,8 @@ const qualityOptions = [ export type QualityOptions = (typeof qualityOptions)[number]; let cancelled = false; +let errorOccurred = false; +let unsupportedPlatform = false; process.on('SIGINT', () => { cancelled = true; }); @@ -97,13 +100,29 @@ export async function run() { await restoreCache(cacheDependencyPath); } - const matchersPath = path.join(__dirname, '..', '..', '.github'); - core.info(`##[add-matcher]${path.join(matchersPath, 'csc.json')}`); } catch (error) { - if (error.message === 'Cancelled') { - 'Cleaning up...'; - } else { - core.setFailed(error.message); + core.setFailed(error.message); + errorOccurred = true; + } finally { + if (errorOccurred || cancelled) { + ('Cleaning up...'); + let directoryPath: string; + switch (os.platform()) { + case 'win32': + directoryPath = 'C:\\Program Files\\dotnet'; + break; + case 'darwin': + directoryPath = '/usr/local/share/dotnet'; + break; + case 'linux': + directoryPath = '/usr/share/dotnet'; + break; + default: + directoryPath = 'Unsupported platform'; + } + if (!unsupportedPlatform && fs.existsSync(directoryPath)) { + fs.rmdirSync(directoryPath, { recursive: true }); + core.info(`Directory ${directoryPath} has been deleted.`); } } } From e9d3438387c7a5888148c370932258d476b618b0 Mon Sep 17 00:00:00 2001 From: shaanmugapriya Date: Mon, 12 Feb 2024 18:28:58 +0530 Subject: [PATCH 3/3] updated code --- src/setup-dotnet.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/setup-dotnet.ts b/src/setup-dotnet.ts index edd9f96..51f521a 100644 --- a/src/setup-dotnet.ts +++ b/src/setup-dotnet.ts @@ -22,7 +22,7 @@ export type QualityOptions = (typeof qualityOptions)[number]; let cancelled = false; let errorOccurred = false; -let unsupportedPlatform = false; +const unsupportedPlatform = false; process.on('SIGINT', () => { cancelled = true; }); @@ -99,7 +99,6 @@ export async function run() { const cacheDependencyPath = core.getInput('cache-dependency-path'); await restoreCache(cacheDependencyPath); } - } catch (error) { core.setFailed(error.message); errorOccurred = true; @@ -121,8 +120,9 @@ export async function run() { directoryPath = 'Unsupported platform'; } if (!unsupportedPlatform && fs.existsSync(directoryPath)) { - fs.rmdirSync(directoryPath, { recursive: true }); + fs.rmdirSync(directoryPath, {recursive: true}); core.info(`Directory ${directoryPath} has been deleted.`); + } } } }