diff --git a/CHANGELOG.md b/CHANGELOG.md index 0842b23..f5f3e9c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ - [#92](https://github.com/dblock/ruby-gem-release/pull/92): Document manual recovery from the preserved bundle when the default branch advances during publication - [@dblock](https://github.com/dblock). - [#93](https://github.com/dblock/ruby-gem-release/pull/93): Document disabling the local Bundler `release` tasks so the workflow is the only release path - [@dblock](https://github.com/dblock). - [#95](https://github.com/dblock/ruby-gem-release/pull/95): Rewrite the documentation in plain language, consolidating repeated rules so each lives in one place - [@dblock](https://github.com/dblock). +- [#97](https://github.com/dblock/ruby-gem-release/pull/97): Name the `.gitignore` lines to add when Ruby setup output such as `vendor/bundle` fails the clean-checkout check, including in dry runs, and document them - [@dblock](https://github.com/dblock). - [#98](https://github.com/dblock/ruby-gem-release/pull/98): Document installing release tasks from a separate release Gemfile with `Gem.loaded_specs` instead of `rescue LoadError` - [@dblock](https://github.com/dblock). - Your contribution here. diff --git a/README.md b/README.md index 3f66237..c8e7d7e 100644 --- a/README.md +++ b/README.md @@ -45,6 +45,16 @@ With these defaults, a release: If your Rakefile also installs the Bundler gem tasks, disable the local Bundler `release` tasks so nobody can publish around the workflow. See [Bundler Release Tasks](docs/usage.md#bundler-release-tasks). +Every release starts by checking that the checkout is clean, and Ruby setup installs gems inside it. Add what it writes to `.gitignore`, leaving out the lockfile if you commit it: + +```gitignore +/vendor/bundle/ +/.bundle/ +/Gemfile.lock +``` + +With a [release Gemfile](docs/actions/release.md#release-gemfile), ignore its lockfile instead, such as `/gemfiles/release.gemfile.lock`. A [dry run](docs/actions/release.md#dry-run) fails and lists any lines that are missing. + ### 2. Add the Release Workflow Copy the [sample workflow](examples/.github/workflows/release.yml) to `.github/workflows/release.yml` and set your Ruby version: diff --git a/docs/actions/release.md b/docs/actions/release.md index a6d6c1d..b0cb167 100644 --- a/docs/actions/release.md +++ b/docs/actions/release.md @@ -45,7 +45,7 @@ The check cannot confirm the publisher belongs to this gem, or that RubyGems wil ### Dry Run -Set `dry-run: true` to run only the [prerequisite checks](#release-prerequisites). A separate `preflight` job replaces the `release` job, in the same `release` environment, so required reviewers still approve it. It never edits, commits, tags, pushes, builds, or publishes. To dispatch dry runs, add a boolean input: +Set `dry-run: true` to run only the [prerequisite checks](#release-prerequisites). A separate `preflight` job replaces the `release` job, in the same `release` environment, so required reviewers still approve it. It never edits, commits, tags, pushes, builds, or publishes. It also checks that the checkout is clean after Ruby setup, so a dry run catches setup output such as `vendor/bundle` that `.gitignore` is missing. To dispatch dry runs, add a boolean input: ```yaml on: @@ -138,7 +138,7 @@ The workflow sets `GH_TOKEN` during publication. Branch rules must allow fast-fo The job runs on `ubuntu-latest` in the `release` environment with a full checkout. Releases share one concurrency group per repository, so a new release waits rather than canceling a running one. -1. **Capture:** after Ruby setup, the checkout must be clean. Changed, staged, or untracked files fail with their paths. Ignored Bundler output is fine; tracked dependency files are not. +1. **Capture:** after Ruby setup, the checkout must be clean. Changed, staged, or untracked files fail with their paths. Ruby setup installs gems into `vendor/bundle`, writes `.bundle/config`, and creates a lockfile when none is committed; [ignore them](../../README.md#1-configure-your-gem). When they are not ignored, the error lists the `.gitignore` lines to add. Tracked dependency files are not allowed. 2. **Validate, prepare, test:** files changed by `prepare` are staged before tests. Publication rejects any later change instead of committing it. Custom bump callbacks wrap edits in `git.stage_changes!`, as in [Detailed Usage](../usage.md#custom-callbacks). 3. **Checkpoint:** the workflow commits, builds, and tags locally, then uploads [recovery artifacts](#recovery-artifacts). This must succeed before `publish` and `bump`. 4. **Publish, bump, notify:** any failure stops later steps. diff --git a/lib/ruby-gem-release/source_control/git/preparation.rb b/lib/ruby-gem-release/source_control/git/preparation.rb index 069556d..c7bc380 100644 --- a/lib/ruby-gem-release/source_control/git/preparation.rb +++ b/lib/ruby-gem-release/source_control/git/preparation.rb @@ -6,6 +6,11 @@ module RubyGemRelease module SourceControl class Git class Preparation + # Directories ruby/setup-ruby's bundler-cache writes into the checkout. + SETUP_DIRECTORIES = %w[vendor/bundle/ .bundle/].freeze + # The lockfile Bundler writes next to a Gemfile when none is committed. + SETUP_LOCKFILE = %r{(?:\A|/)(?:Gemfile\.lock|gems\.locked|[^/]+\.gemfile\.lock)\z} + def initialize(directory:, &command) @path = File.join(directory, 'prepared-tree') @command = command @@ -15,7 +20,11 @@ def check_clean! entries = changes return if entries.empty? - raise unexpected_changes('Release requires a clean checkout after dependency installation', entries) + message = unexpected_changes('Release requires a clean checkout after dependency installation', entries) + ignores = setup_ignores(entries) + raise message if ignores.empty? + + raise "#{message}\n#{setup_hint(ignores)}" end def stage_changes! @@ -66,7 +75,40 @@ def changes entries end + # Ignore lines for untracked Ruby setup output, in a stable order. + def setup_ignores(entries) + untracked = entries.filter_map { |status, path| path if status == '??' } + directories = SETUP_DIRECTORIES.select { |directory| untracked.any? { |path| setup_directory(path) == directory } } + lockfiles = untracked.select { |path| setup_lockfile?(path) } + (directories + lockfiles).map { |path| "/#{path}" } + end + + def setup_lockfile?(path) + !setup_directory(path) && SETUP_LOCKFILE.match?(path) + end + + def setup_directory(path) + SETUP_DIRECTORIES.find { |directory| path.start_with?(directory) } + end + + def setup_hint(ignores) + lockfile = ' (or commit the lockfile)' if ignores.any? { |line| SETUP_LOCKFILE.match?(line) } + "Ruby setup wrote these into the checkout; add them to .gitignore#{lockfile}:\n#{ignores.map { |line| " #{line}" }.join("\n")}" + end + + # Folds untracked files under setup directories into one line per directory. + def folded(entries) + entries.each_with_object([]) do |entry, result| + status, path = entry + directory = setup_directory(path) if status == '??' + next result << entry unless directory + + result << ['??', directory] unless result.include?(['??', directory]) + end + end + def unexpected_changes(message, entries) + entries = folded(entries) details = entries.first(20).map { |status, path, source| " #{status} #{path.inspect}#{" (from #{source.inspect})" if source}" } details << " ... #{entries.size - 20} more paths" if entries.size > 20 "#{message} (#{entries.size} paths):\n#{details.join("\n")}\n" \ diff --git a/spec/ruby_gem_release/source_control/git_spec.rb b/spec/ruby_gem_release/source_control/git_spec.rb index b6ceb40..4d403e9 100644 --- a/spec/ruby_gem_release/source_control/git_spec.rb +++ b/spec/ruby_gem_release/source_control/git_spec.rb @@ -226,6 +226,30 @@ def prepare_tag(ctx = release) expect(git.head).to eq(initial) end + it 'names the gitignore lines for unignored Ruby setup output and folds its directories', :aggregate_failures do + FileUtils.mkdir_p(['.bundle', 'vendor/bundle/ruby/4.0.0/gems/rake-13.3.0', 'gemfiles']) + File.write('.bundle/config', "BUNDLE_PATH: vendor/bundle\n") + 30.times { |index| File.write("vendor/bundle/ruby/4.0.0/gems/rake-13.3.0/file#{index}.rb", '') } + File.write('Gemfile.lock', 'lock') + File.write('gemfiles/release.gemfile.lock', 'lock') + expect { git.check_clean! }.to raise_error(RuntimeError) { |error| + expect(error.message).to include('(4 paths):', ' ?? ".bundle/"', ' ?? "vendor/bundle/"', ' ?? "Gemfile.lock"') + expect(error.message).not_to include('rake-13.3.0') + expect(error.message).to end_with( + "Ruby setup wrote these into the checkout; add them to .gitignore (or commit the lockfile):\n /vendor/bundle/\n /.bundle/\n /Gemfile.lock\n /gemfiles/release.gemfile.lock" + ) + } + end + + it 'omits the lockfile advice without an untracked lockfile and the hint for other files', :aggregate_failures do + FileUtils.mkdir_p('vendor/bundle') + File.write('vendor/bundle/generated.rb', 'dependency') + expect { git.check_clean! }.to raise_error(%r{add them to \.gitignore:\n /vendor/bundle/\z}) + FileUtils.rm_rf('vendor') + File.write('notes.txt', 'unexpected') + expect { git.check_clean! }.to raise_error(RuntimeError) { |error| expect(error.message).not_to include('Ruby setup') } + end + it 'permits ignored dependency output without staging it in release preparation' do File.write('.gitignore', ".bundle/\nvendor/bundle/\n") command('add', '.gitignore')