Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions buildpacks/ruby/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Fixed

- Executables from the applications `bin` directory will be placed on the path before dependencies installed via bundler ([#383](https://github.com/heroku/buildpacks-ruby/pull/383))
- Binaries from user installed gems will be placed on the path before binaries that ship with Ruby ([#383](https://github.com/heroku/buildpacks-ruby/pull/383))

## [5.0.0] - 2024-12-17

### Changed
Expand Down
1 change: 1 addition & 0 deletions buildpacks/ruby/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,4 @@ cache_diff = { version = "1.0.0", features = ["bullet_stream"] }

[dev-dependencies]
libcnb-test = "=0.26.1"
pretty_assertions = "1.4.1"
52 changes: 28 additions & 24 deletions buildpacks/ruby/src/layers/bundle_download_layer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ pub(crate) fn handle(
match &layer_ref.state {
LayerState::Restored { cause } => {
bullet = bullet.sub_bullet(cause);

layer_ref.write_env(layer_env(&layer_ref.path()))?;
Ok((bullet, layer_ref.read_env()?))
}
LayerState::Empty { cause } => {
Expand All @@ -46,9 +48,9 @@ pub(crate) fn handle(
bullet = bullet.sub_bullet(cause);
}
}
let (bullet, layer_env) = download_bundler(bullet, env, metadata, &layer_ref.path())?;
layer_ref.write_env(&layer_env)?;
let bullet = download_bundler(bullet, env, metadata, &layer_ref.path())?;

layer_ref.write_env(layer_env(&layer_ref.path()))?;
Ok((bullet, layer_ref.read_env()?))
}
}
Expand All @@ -73,15 +75,33 @@ pub(crate) enum MetadataError {
// Update if migrating between a metadata version can error
}

fn layer_env(gem_path: &Path) -> LayerEnv {
let bin_dir = gem_path.join("bin");

LayerEnv::new()
.chainable_insert(Scope::All, ModificationBehavior::Delimiter, "PATH", ":")
.chainable_insert(
Scope::All,
ModificationBehavior::Prepend,
"PATH", // Ensure this path comes before default bundler that ships with ruby, don't rely on the lifecycle
&bin_dir,
)
.chainable_insert(Scope::All, ModificationBehavior::Delimiter, "GEM_PATH", ":")
.chainable_insert(
Scope::All,
ModificationBehavior::Prepend,
"GEM_PATH", // Bundler is a gem too, allow it to be required
gem_path,
)
}

fn download_bundler(
bullet: Print<SubBullet<Stdout>>,
env: &Env,
metadata: &Metadata,
path: &Path,
) -> Result<(Print<SubBullet<Stdout>>, LayerEnv), RubyBuildpackError> {
let bin_dir = path.join("bin");
let gem_path = path;

gem_path: &Path,
) -> Result<Print<SubBullet<Stdout>>, RubyBuildpackError> {
let bin_dir = gem_path.join("bin");
let mut cmd = Command::new("gem");
cmd.args(["install", "bundler"]);
cmd.args(["--version", &metadata.version.to_string()]) // Specify exact version to install
Expand All @@ -104,23 +124,7 @@ fn download_bundler(
.map_err(|error| fun_run::map_which_problem(error, cmd.mut_cmd(), env.get("PATH").cloned()))
.map_err(RubyBuildpackError::GemInstallBundlerCommandError)?;

let layer_env = LayerEnv::new()
.chainable_insert(Scope::All, ModificationBehavior::Delimiter, "PATH", ":")
.chainable_insert(
Scope::All,
ModificationBehavior::Prepend,
"PATH", // Ensure this path comes before default bundler that ships with ruby, don't rely on the lifecycle
bin_dir,
)
.chainable_insert(Scope::All, ModificationBehavior::Delimiter, "GEM_PATH", ":")
.chainable_insert(
Scope::All,
ModificationBehavior::Prepend,
"GEM_PATH", // Bundler is a gem too, allow it to be required
gem_path,
);

Ok((timer.done(), layer_env))
Ok(timer.done())
}

#[cfg(test)]
Expand Down
4 changes: 2 additions & 2 deletions buildpacks/ruby/src/layers/bundle_install_layer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -322,9 +322,9 @@ fn display_name(cmd: &mut Command, env: &Env) -> String {

#[cfg(test)]
mod test {
use bullet_stream::strip_ansi;

use super::*;
use bullet_stream::strip_ansi;
use pretty_assertions::assert_eq;
use std::path::PathBuf;

/// `CacheDiff` logic controls cache invalidation
Expand Down
4 changes: 2 additions & 2 deletions buildpacks/ruby/src/layers/metrics_agent_install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,10 +152,10 @@ fn write_execd_script(
fs_err::write(
&execd,
format!(
r#"#!/usr/bin/env bash
r"#!/usr/bin/env bash

{daemon} --log {log} --loop-path {run_loop} --agentmon {agentmon}
"#,
",
log = log.display(),
daemon = daemon.display(),
run_loop = run_loop.display(),
Expand Down
11 changes: 9 additions & 2 deletions buildpacks/ruby/src/layers/ruby_install_layer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use bullet_stream::state::SubBullet;
use bullet_stream::Print;
use cache_diff::CacheDiff;
use commons::gemfile_lock::ResolvedRubyVersion;
use commons::layer::diff_migrate::DiffMigrateLayer;
use commons::layer::diff_migrate::{DiffMigrateLayer, LayerRename};
use flate2::read::GzDecoder;
use libcnb::data::layer_name;
use libcnb::layer::{EmptyLayerCause, LayerState};
Expand All @@ -42,7 +42,14 @@ pub(crate) fn handle(
build: true,
launch: true,
}
.cached_layer(layer_name!("ruby"), context, metadata)?;
.cached_layer_rename(
LayerRename {
to: layer_name!("binruby"),
from: vec![layer_name!("ruby")],
},
context,
metadata,
)?;
match &layer_ref.state {
LayerState::Restored { cause } => {
bullet = bullet.sub_bullet(cause);
Expand Down
28 changes: 27 additions & 1 deletion buildpacks/ruby/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ use layers::{
use libcnb::build::{BuildContext, BuildResult, BuildResultBuilder};
use libcnb::data::build_plan::BuildPlanBuilder;
use libcnb::data::launch::LaunchBuilder;
use libcnb::data::layer_name;
use libcnb::detect::{DetectContext, DetectResult, DetectResultBuilder};
use libcnb::generic::{GenericMetadata, GenericPlatform};
use libcnb::layer_env::Scope;
use libcnb::layer::UncachedLayerDefinition;
use libcnb::layer_env::{LayerEnv, ModificationBehavior, Scope};
use libcnb::Platform;
use libcnb::{buildpack_main, Buildpack};
use std::io::stdout;
Expand All @@ -28,6 +30,8 @@ mod user_errors;

#[cfg(test)]
use libcnb_test as _;
#[cfg(test)]
use pretty_assertions as _;

use clap as _;

Expand Down Expand Up @@ -218,6 +222,28 @@ impl Buildpack for RubyBuildpack {
(bullet.done(), layer_env.apply(Scope::Build, &env))
};

env = {
let user_binstubs = context.uncached_layer(
layer_name!("user_binstubs"),
UncachedLayerDefinition {
build: true,
launch: true,
},
)?;
user_binstubs.write_env(
LayerEnv::new()
.chainable_insert(Scope::All, ModificationBehavior::Delimiter, "PATH", ":")
.chainable_insert(
Scope::All,
ModificationBehavior::Prepend,
"PATH",
context.app_dir.join("bin"),
),
)?;

user_binstubs.read_env()?.apply(Scope::Build, &env)
};

// ## Detect gems
let (mut build_output, gem_list, default_process) = {
let bullet = build_output.bullet("Default process detection");
Expand Down
6 changes: 2 additions & 4 deletions buildpacks/ruby/src/rake_task_detect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,8 @@ pub(crate) fn call<T: IntoIterator<Item = (K, V)>, K: AsRef<OsStr>, V: AsRef<OsS
envs: T,
error_on_failure: bool,
) -> Result<(Print<SubBullet<Stdout>>, RakeDetect), CmdError> {
let mut cmd = Command::new("bundle");
cmd.args(["exec", "rake", "-P", "--trace"])
.env_clear()
.envs(envs);
let mut cmd = Command::new("rake");
cmd.args(["-P", "--trace"]).env_clear().envs(envs);

let timer = bullet.start_timer(format!("Running {}", style::command(cmd.name())));
let output = cmd.named_output().or_else(|error| {
Expand Down
20 changes: 7 additions & 13 deletions buildpacks/ruby/src/steps/rake_assets_install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub(crate) fn rake_assets_install(
let cases = asset_cases(rake_detect);
let rake_assets_precompile = style::value("rake assets:precompile");
let rake_assets_clean = style::value("rake assets:clean");
let rake_detect_cmd = style::value("bundle exec rake -P");
let rake_detect_cmd = style::value("rake -P");

match cases {
AssetCases::None => {
Expand All @@ -33,8 +33,8 @@ pub(crate) fn rake_assets_install(
format!("Compiling assets without cache (Clean task not found via {rake_detect_cmd})"),
).sub_bullet(format!("{help} Enable caching by ensuring {rake_assets_clean} is present when running the detect command locally"));

let mut cmd = Command::new("bundle");
cmd.args(["exec", "rake", "assets:precompile", "--trace"])
let mut cmd = Command::new("rake");
cmd.args(["assets:precompile", "--trace"])
.env_clear()
.envs(env);

Expand Down Expand Up @@ -79,16 +79,10 @@ pub(crate) fn rake_assets_install(
});
}

let mut cmd = Command::new("bundle");
cmd.args([
"exec",
"rake",
"assets:precompile",
"assets:clean",
"--trace",
])
.env_clear()
.envs(env);
let mut cmd = Command::new("rake");
cmd.args(["assets:precompile", "assets:clean", "--trace"])
.env_clear()
.envs(env);

bullet
.stream_with(
Expand Down
Loading