Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix user binstubs not on PATH and fix PATH order #383

Merged
merged 22 commits into from
Jan 13, 2025
Merged
Show file tree
Hide file tree
Changes from 20 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"
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