Skip to content

Commit cfe7964

Browse files
dblockclaude
andcommitted
Name the gitignore lines for Ruby setup output
ruby/setup-ruby with bundler-cache installs gems into vendor/bundle, writes .bundle/config, and creates a lockfile when none is committed, all inside the checkout. Unless .gitignore covers them, the clean checkout check at capture fails in every release and dry run. Document the lines to add in the README and workflow reference. When the check fails on this output, fold each setup directory into one line and end the error with the exact ignore lines, so a dry run says what to add. Other untracked or changed files are reported as before. Co-Authored-By: Claude <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_011Gr21htyJfr8XZGHBcPwkW
1 parent 4389652 commit cfe7964

5 files changed

Lines changed: 80 additions & 3 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
- [#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).
88
- [#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).
99
- [#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).
10+
- [#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).
1011
- [#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).
1112
- Your contribution here.
1213

‎README.md‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,16 @@ With these defaults, a release:
4545

4646
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).
4747

48+
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:
49+
50+
```gitignore
51+
/vendor/bundle/
52+
/.bundle/
53+
/Gemfile.lock
54+
```
55+
56+
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.
57+
4858
### 2. Add the Release Workflow
4959

5060
Copy the [sample workflow](examples/.github/workflows/release.yml) to `.github/workflows/release.yml` and set your Ruby version:

‎docs/actions/release.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ The check cannot confirm the publisher belongs to this gem, or that RubyGems wil
4545

4646
### Dry Run
4747

48-
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:
48+
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:
4949

5050
```yaml
5151
on:
@@ -138,7 +138,7 @@ The workflow sets `GH_TOKEN` during publication. Branch rules must allow fast-fo
138138

139139
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.
140140

141-
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.
141+
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.
142142
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).
143143
3. **Checkpoint:** the workflow commits, builds, and tags locally, then uploads [recovery artifacts](#recovery-artifacts). This must succeed before `publish` and `bump`.
144144
4. **Publish, bump, notify:** any failure stops later steps.

‎lib/ruby-gem-release/source_control/git/preparation.rb‎

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ module RubyGemRelease
66
module SourceControl
77
class Git
88
class Preparation
9+
# Directories ruby/setup-ruby's bundler-cache writes into the checkout.
10+
SETUP_DIRECTORIES = %w[vendor/bundle/ .bundle/].freeze
11+
# The lockfile Bundler writes next to a Gemfile when none is committed.
12+
SETUP_LOCKFILE = %r{(?:\A|/)(?:Gemfile\.lock|gems\.locked|[^/]+\.gemfile\.lock)\z}
13+
914
def initialize(directory:, &command)
1015
@path = File.join(directory, 'prepared-tree')
1116
@command = command
@@ -15,7 +20,11 @@ def check_clean!
1520
entries = changes
1621
return if entries.empty?
1722

18-
raise unexpected_changes('Release requires a clean checkout after dependency installation', entries)
23+
message = unexpected_changes('Release requires a clean checkout after dependency installation', entries)
24+
ignores = setup_ignores(entries)
25+
raise message if ignores.empty?
26+
27+
raise "#{message}\n#{setup_hint(ignores)}"
1928
end
2029

2130
def stage_changes!
@@ -66,7 +75,40 @@ def changes
6675
entries
6776
end
6877

78+
# Ignore lines for untracked Ruby setup output, in a stable order.
79+
def setup_ignores(entries)
80+
untracked = entries.filter_map { |status, path| path if status == '??' }
81+
directories = SETUP_DIRECTORIES.select { |directory| untracked.any? { |path| setup_directory(path) == directory } }
82+
lockfiles = untracked.select { |path| setup_lockfile?(path) }
83+
(directories + lockfiles).map { |path| "/#{path}" }
84+
end
85+
86+
def setup_lockfile?(path)
87+
!setup_directory(path) && SETUP_LOCKFILE.match?(path)
88+
end
89+
90+
def setup_directory(path)
91+
SETUP_DIRECTORIES.find { |directory| path.start_with?(directory) }
92+
end
93+
94+
def setup_hint(ignores)
95+
lockfile = ' (or commit the lockfile)' if ignores.any? { |line| SETUP_LOCKFILE.match?(line) }
96+
"Ruby setup wrote these into the checkout; add them to .gitignore#{lockfile}:\n#{ignores.map { |line| " #{line}" }.join("\n")}"
97+
end
98+
99+
# Folds untracked files under setup directories into one line per directory.
100+
def folded(entries)
101+
entries.each_with_object([]) do |entry, result|
102+
status, path = entry
103+
directory = setup_directory(path) if status == '??'
104+
next result << entry unless directory
105+
106+
result << ['??', directory] unless result.include?(['??', directory])
107+
end
108+
end
109+
69110
def unexpected_changes(message, entries)
111+
entries = folded(entries)
70112
details = entries.first(20).map { |status, path, source| " #{status} #{path.inspect}#{" (from #{source.inspect})" if source}" }
71113
details << " ... #{entries.size - 20} more paths" if entries.size > 20
72114
"#{message} (#{entries.size} paths):\n#{details.join("\n")}\n" \

‎spec/ruby_gem_release/source_control/git_spec.rb‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,30 @@ def prepare_tag(ctx = release)
226226
expect(git.head).to eq(initial)
227227
end
228228

229+
it 'names the gitignore lines for unignored Ruby setup output and folds its directories', :aggregate_failures do
230+
FileUtils.mkdir_p(['.bundle', 'vendor/bundle/ruby/4.0.0/gems/rake-13.3.0', 'gemfiles'])
231+
File.write('.bundle/config', "BUNDLE_PATH: vendor/bundle\n")
232+
30.times { |index| File.write("vendor/bundle/ruby/4.0.0/gems/rake-13.3.0/file#{index}.rb", '') }
233+
File.write('Gemfile.lock', 'lock')
234+
File.write('gemfiles/release.gemfile.lock', 'lock')
235+
expect { git.check_clean! }.to raise_error(RuntimeError) { |error|
236+
expect(error.message).to include('(4 paths):', ' ?? ".bundle/"', ' ?? "vendor/bundle/"', ' ?? "Gemfile.lock"')
237+
expect(error.message).not_to include('rake-13.3.0')
238+
expect(error.message).to end_with(
239+
"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"
240+
)
241+
}
242+
end
243+
244+
it 'omits the lockfile advice without an untracked lockfile and the hint for other files', :aggregate_failures do
245+
FileUtils.mkdir_p('vendor/bundle')
246+
File.write('vendor/bundle/generated.rb', 'dependency')
247+
expect { git.check_clean! }.to raise_error(%r{add them to \.gitignore:\n /vendor/bundle/\z})
248+
FileUtils.rm_rf('vendor')
249+
File.write('notes.txt', 'unexpected')
250+
expect { git.check_clean! }.to raise_error(RuntimeError) { |error| expect(error.message).not_to include('Ruby setup') }
251+
end
252+
229253
it 'permits ignored dependency output without staging it in release preparation' do
230254
File.write('.gitignore', ".bundle/\nvendor/bundle/\n")
231255
command('add', '.gitignore')

0 commit comments

Comments
 (0)