From 46adeca10b0f84c11b0843722b3c6c6ac94e06e3 Mon Sep 17 00:00:00 2001 From: Jason Frey Date: Tue, 5 Apr 2022 11:03:29 -0400 Subject: [PATCH] Fix `bundler:` with a minor version installing the wrong version If passed `bundler: 2.2`, setup-ruby would install bundler 2.3, because it would generate a constraint that looks like `~> 2.2`. This unexpectedly installs the latest of the 2.x series, but the expectation is that it would install the latest of the 2.2.x series. Fixes #304 --- .github/workflows/test.yml | 22 ++++++++++++++++++++++ action.yml | 2 +- bundler.js | 9 ++++++--- dist/index.js | 9 ++++++--- 4 files changed, 35 insertions(+), 7 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 621fc0cb3..cdef4e5ce 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -145,6 +145,28 @@ jobs: rubygems: 3.2.3 - run: gem --version | grep -F "3.3.3" + testMajorBundlerVersion: + name: "Test with a major Bundler version" + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - uses: ./ + with: + ruby-version: 2.6 + bundler: 2 + - run: bundle --version | grep -P "Bundler version 2\.\d+\.\d+" + + testMinorBundlerVersion: + name: "Test with a minor Bundler version" + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - uses: ./ + with: + ruby-version: 2.6 + bundler: 2.2 + - run: bundle --version | grep -P "Bundler version 2\.2\.\d+" + testExactBundlerVersion: name: "Test with an exact Bundler version" runs-on: ubuntu-latest diff --git a/action.yml b/action.yml index 01a1b5831..a0814dffa 100644 --- a/action.yml +++ b/action.yml @@ -18,7 +18,7 @@ inputs: Defaults to 'default'. bundler: description: | - The version of Bundler to install. Either 'none', 'latest', 'Gemfile.lock', or a version number (e.g., 1, 2, 2.1.4). + The version of Bundler to install. Either 'none', 'latest', 'Gemfile.lock', or a version number (e.g., 1, 2, 2.1, 2.1.4). For 'Gemfile.lock', the version is determined based on the BUNDLED WITH section from the file Gemfile.lock, $BUNDLE_GEMFILE.lock or gems.locked. Defaults to 'Gemfile.lock' if the file exists and 'latest' otherwise. required: false diff --git a/bundler.js b/bundler.js index 63d0dd1ee..21e2929e9 100644 --- a/bundler.js +++ b/bundler.js @@ -65,8 +65,8 @@ export async function installBundler(bundlerVersionInput, lockFile, platform, ru bundlerVersion = '2' } - if (/^\d+/.test(bundlerVersion)) { - // OK + if (/^\d+(?:\.\d+){0,2}$/.test(bundlerVersion)) { + // OK - input is a 1, 2, or 3 part version number } else { throw new Error(`Cannot parse bundler input: ${bundlerVersion}`) } @@ -95,9 +95,12 @@ export async function installBundler(bundlerVersionInput, lockFile, platform, ru console.log(`Using Bundler 1 shipped with ${engine}-${rubyVersion}`) } else { const gem = path.join(rubyPrefix, 'bin', 'gem') - const bundlerVersionConstraint = /^\d+\.\d+\.\d+/.test(bundlerVersion) ? bundlerVersion : `~> ${bundlerVersion}` // Workaround for https://github.com/rubygems/rubygems/issues/5245 const force = (platform.startsWith('windows-') && engine === 'ruby' && floatVersion >= 3.1) ? ['--force'] : [] + + const versionParts = [...bundlerVersion.matchAll(/\d+/g)].length + const bundlerVersionConstraint = versionParts === 3 ? bundlerVersion : `~> ${bundlerVersion}.0` + await exec.exec(gem, ['install', 'bundler', ...force, '-v', bundlerVersionConstraint]) } diff --git a/dist/index.js b/dist/index.js index 15b65f279..db679b8b3 100644 --- a/dist/index.js +++ b/dist/index.js @@ -79,8 +79,8 @@ async function installBundler(bundlerVersionInput, lockFile, platform, rubyPrefi bundlerVersion = '2' } - if (/^\d+/.test(bundlerVersion)) { - // OK + if (/^\d+(?:\.\d+){0,2}$/.test(bundlerVersion)) { + // OK - input is a 1, 2, or 3 part version number } else { throw new Error(`Cannot parse bundler input: ${bundlerVersion}`) } @@ -109,9 +109,12 @@ async function installBundler(bundlerVersionInput, lockFile, platform, rubyPrefi console.log(`Using Bundler 1 shipped with ${engine}-${rubyVersion}`) } else { const gem = path.join(rubyPrefix, 'bin', 'gem') - const bundlerVersionConstraint = /^\d+\.\d+\.\d+/.test(bundlerVersion) ? bundlerVersion : `~> ${bundlerVersion}` // Workaround for https://github.com/rubygems/rubygems/issues/5245 const force = (platform.startsWith('windows-') && engine === 'ruby' && floatVersion >= 3.1) ? ['--force'] : [] + + const versionParts = [...bundlerVersion.matchAll(/\d+/g)].length + const bundlerVersionConstraint = versionParts === 3 ? bundlerVersion : `~> ${bundlerVersion}.0` + await exec.exec(gem, ['install', 'bundler', ...force, '-v', bundlerVersionConstraint]) }