Skip to content

ruby-bundler: Fix detection, gem caching and app startup - #211

Merged
AidanDelaney merged 2 commits into
buildpacks:mainfrom
NopeCharon:fix/ruby-bundler-buildpack
Nov 22, 2026
Merged

AidanDelaney merged 2 commits into
buildpacks:mainfrom
NopeCharon:fix/ruby-bundler-buildpack

Conversation

@NopeCharon

Copy link
Copy Markdown
Contributor

Fixes #208

This gets the ruby-bundler sample working end to end again:

  • detect: put the Ruby version under [requires.metadata]. A top-level version key is rejected by the lifecycle, so the buildpack errored during detection and apps/ruby-bundler was built by samples/hello-universe instead. bin/build already reads .metadata.version.
  • build: write layer content metadata to a real [metadata] table (version for ruby, checksum for bundler) rather than as metadata = ... inside [types]. Before this, the Gemfile.lock checksum never matched and gems were reinstalled on every build.
  • build: write bundler.toml (with [types]) on every build. The restored layer metadata never includes [types], so on the "Reusing gems" path the layer would otherwise be dropped from the image.
  • build: compute the Gemfile.lock checksum only if the file exists. An app with only a Gemfile passes detection, but the build used to abort at sha256sum under set -eo pipefail.
  • app: add webrick to the Gemfile/lockfile. Ruby 3.x no longer ships it as a default gem, and the app failed to start with Couldn't find handler for: thin, puma, reel, HTTP, webrick. The lockfile was regenerated with bundle lock (Bundler 2.3.26, the version it was already locked with); existing gem versions are unchanged.

Testing

I ran the real lifecycle detector and builder (v0.21.26) on an image built from base-images/resolute/{base,build}, with this buildpack and apps/ruby-bundler:

  • Build 1: samples/ruby-bundler passes detection, Ruby 3.1.3 is installed, gems are installed. bundler.toml now holds [metadata] checksum = "...".
  • I then simulated a restore as the spec describes: kept the [metadata], dropped [types] and the non-cached ruby layer contents.
  • Build 2: prints ---> Reusing gems, and bundler.toml has cache = true / launch = true again.
  • Starting bundle exec ruby app.rb from the built workspace serves Hello World! on port 8080.
  • With Gemfile.lock removed, both builds succeed and install gems. Before this change the build stopped at sha256sum.

@NopeCharon
NopeCharon requested a review from a team as a code owner November 1, 2026 19:20
@AidanDelaney

Copy link
Copy Markdown
Member

Thanks for this. It will take me a few days to work through it.

@AidanDelaney AidanDelaney left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a good set of changes. Apologies for the delay in the review, but I had to brush up on my understanding of the ruby ecosystem. I have one suggested change. Please let me know if the suggestion makes sense?

Comment thread buildpacks/ruby-bundler/bin/build Outdated
if [[ -f Gemfile.lock ]]; then
local_bundler_checksum=$(sha256sum Gemfile.lock | cut -d ' ' -f 1)
fi
remote_bundler_checksum=$(cat "${bundler_layer_dir}.toml" 2>/dev/null | yj -t | jq -r .metadata.checksum 2>/dev/null || echo 'not found')

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On a first build the file is missing. Because of pipefail, cat’s failure makes the whole pipeline fail. However, yj and jq still run on empty input and print what they print. Can we stick to the "" is not found convention? And only set it to the checksum if the cached file exists?

Suggested change
remote_bundler_checksum=$(cat "${bundler_layer_dir}.toml" 2>/dev/null | yj -t | jq -r .metadata.checksum 2>/dev/null || echo 'not found')
remote_bundler_checksum=""
if [[ -f "${bundler_layer_dir}.toml" ]]; then
remote_bundler_checksum=$(yj -t < "${bundler_layer_dir}.toml" | jq -r '.metadata.checksum // empty')
fi

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, applied your suggestion. The cached checksum is now only read when the toml exists, and an empty string means not found. Fixed in 3aae092.

The ruby-bundler sample has not been usable end to end:

* bin/detect wrote `version` directly under `[[requires]]`. The lifecycle
  rejects this for current Buildpack APIs ("buildpack samples/ruby-bundler
  has a "version" key which is not supported. "metadata.version" should be
  used instead"), so the buildpack errored during detection and
  `apps/ruby-bundler` was silently built by the `samples/hello-universe`
  group instead. bin/build already reads `.metadata.version` from the
  plan, so the version now goes into `[requires.metadata]`.

* bin/build wrote `metadata = "..."` inside the `[types]` table of the
  `ruby` and `bundler` layer TOML files. That is `types.metadata`, not the
  layer content metadata, so the lifecycle restores an empty `[metadata]`
  table and the Gemfile.lock checksum comparison never matched: gems were
  reinstalled on every build. The values now live in a proper
  `[metadata]` table (`version` and `checksum`).

* The restored layer TOML never contains `[types]`, so when the gems
  were reused the bundler layer would have been dropped from the image.
  The bundler layer TOML is now written on every build, in both the
  reuse and install paths.

* `sha256sum Gemfile.lock` ran unconditionally under `set -eo pipefail`,
  so an app with a Gemfile but no Gemfile.lock (which detect accepts)
  failed the build. The checksum is now only computed when the lockfile
  exists; without one the gems are always installed.

* The sample app could not start on Ruby 3.x because WEBrick is no longer
  a default gem and the Gemfile declared no web server ("Couldn't find
  handler for: thin, puma, reel, HTTP, webrick"). Add `webrick` to the
  Gemfile and lockfile.

Signed-off-by: NopeCharon <115794254+NopeCharon@users.noreply.github.com>
Signed-off-by: NopeCharon <115794254+NopeCharon@users.noreply.github.com>
@AidanDelaney
AidanDelaney force-pushed the fix/ruby-bundler-buildpack branch from 3aae092 to df08e5b Compare November 22, 2026 09:52

@AidanDelaney AidanDelaney left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

@AidanDelaney
AidanDelaney merged commit 93fa56b into buildpacks:main Nov 22, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ruby-bundler sample buildpack fails detection, never reuses cached gems, and the app cannot start

2 participants