Repository navigation
ruby-bundler: Fix detection, gem caching and app startup - #211
Conversation
|
Thanks for this. It will take me a few days to work through it. |
AidanDelaney
left a comment
There was a problem hiding this comment.
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?
| 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') |
There was a problem hiding this comment.
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?
| 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 |
There was a problem hiding this comment.
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>
3aae092 to
df08e5b
Compare
Fixes #208
This gets the
ruby-bundlersample working end to end again:[requires.metadata]. A top-levelversionkey is rejected by the lifecycle, so the buildpack errored during detection andapps/ruby-bundlerwas built bysamples/hello-universeinstead.bin/buildalready reads.metadata.version.[metadata]table (versionforruby,checksumforbundler) rather than asmetadata = ...inside[types]. Before this, the Gemfile.lock checksum never matched and gems were reinstalled on every build.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.Gemfilepasses detection, but the build used to abort atsha256sumunderset -eo pipefail.webrickto the Gemfile/lockfile. Ruby 3.x no longer ships it as a default gem, and the app failed to start withCouldn't find handler for: thin, puma, reel, HTTP, webrick. The lockfile was regenerated withbundle lock(Bundler 2.3.26, the version it was already locked with); existing gem versions are unchanged.Testing
I ran the real lifecycle
detectorandbuilder(v0.21.26) on an image built frombase-images/resolute/{base,build}, with this buildpack andapps/ruby-bundler:samples/ruby-bundlerpasses detection, Ruby 3.1.3 is installed, gems are installed.bundler.tomlnow holds[metadata] checksum = "...".[metadata], dropped[types]and the non-cachedrubylayer contents.---> Reusing gems, andbundler.tomlhascache = true/launch = trueagain.bundle exec ruby app.rbfrom the built workspace servesHello World!on port 8080.Gemfile.lockremoved, both builds succeed and install gems. Before this change the build stopped atsha256sum.