From 586dc166f8d12774d3242984e43279aae8258567 Mon Sep 17 00:00:00 2001 From: Josh W Lewis Date: Thu, 12 Dec 2024 11:10:31 -0600 Subject: [PATCH 1/2] Don't set processes when Procfile is present --- buildpacks/go/CHANGELOG.md | 2 ++ buildpacks/go/src/main.rs | 15 +++++++------ .../tests/fixtures/procfile_http_123/Procfile | 1 + .../tests/fixtures/procfile_http_123/go.mod | 3 +++ .../tests/fixtures/procfile_http_123/main.go | 21 +++++++++++++++++++ buildpacks/go/tests/integration_test.rs | 13 ++++++++++++ 6 files changed, 49 insertions(+), 6 deletions(-) create mode 100644 buildpacks/go/tests/fixtures/procfile_http_123/Procfile create mode 100644 buildpacks/go/tests/fixtures/procfile_http_123/go.mod create mode 100644 buildpacks/go/tests/fixtures/procfile_http_123/main.go diff --git a/buildpacks/go/CHANGELOG.md b/buildpacks/go/CHANGELOG.md index 9e482b32..e62ce686 100644 --- a/buildpacks/go/CHANGELOG.md +++ b/buildpacks/go/CHANGELOG.md @@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +- Now prefers processes set by Procfile, and no longer adds it's own processes if a Procfile is present. + ## [0.4.7] - 2024-12-06 - Added go1.22.10 (linux-amd64), go1.22.10 (linux-arm64), go1.23.4 (linux-amd64), go1.23.4 (linux-arm64). diff --git a/buildpacks/go/src/main.rs b/buildpacks/go/src/main.rs index 0a458ed4..651df8d2 100644 --- a/buildpacks/go/src/main.rs +++ b/buildpacks/go/src/main.rs @@ -11,7 +11,7 @@ use layers::dist::{DistLayer, DistLayerError}; use layers::target::{TargetLayer, TargetLayerError}; use libcnb::build::{BuildContext, BuildResult, BuildResultBuilder}; use libcnb::data::build_plan::BuildPlanBuilder; -use libcnb::data::launch::LaunchBuilder; +use libcnb::data::launch::{LaunchBuilder, Process}; use libcnb::data::layer_name; use libcnb::detect::{DetectContext, DetectResult, DetectResultBuilder}; use libcnb::generic::GenericMetadata; @@ -139,11 +139,14 @@ impl Buildpack for GoBuildpack { } cmd::go_install(&packages, &go_env).map_err(GoBuildpackError::GoBuild)?; - log_header("Setting launch table"); - let procs = proc::build_procs(&packages).map_err(GoBuildpackError::Proc)?; - log_info("Detected processes:"); - for proc in &procs { - log_info(format!(" - {}: {}", proc.r#type, proc.command.join(" "))); + let mut procs: Vec = vec![]; + if !Path::exists(&context.app_dir.join("Procfile")) { + log_header("Setting launch table"); + procs = proc::build_procs(&packages).map_err(GoBuildpackError::Proc)?; + log_info("Detected processes:"); + for proc in &procs { + log_info(format!(" - {}: {}", proc.r#type, proc.command.join(" "))); + } } BuildResultBuilder::new() diff --git a/buildpacks/go/tests/fixtures/procfile_http_123/Procfile b/buildpacks/go/tests/fixtures/procfile_http_123/Procfile new file mode 100644 index 00000000..4d97e877 --- /dev/null +++ b/buildpacks/go/tests/fixtures/procfile_http_123/Procfile @@ -0,0 +1 @@ +web: procfile_http_123 diff --git a/buildpacks/go/tests/fixtures/procfile_http_123/go.mod b/buildpacks/go/tests/fixtures/procfile_http_123/go.mod new file mode 100644 index 00000000..e14d58fe --- /dev/null +++ b/buildpacks/go/tests/fixtures/procfile_http_123/go.mod @@ -0,0 +1,3 @@ +module example.com/procfile_http_123 + +go 1.23 diff --git a/buildpacks/go/tests/fixtures/procfile_http_123/main.go b/buildpacks/go/tests/fixtures/procfile_http_123/main.go new file mode 100644 index 00000000..30eb36a5 --- /dev/null +++ b/buildpacks/go/tests/fixtures/procfile_http_123/main.go @@ -0,0 +1,21 @@ +// +build heroku + +package main + +import ( + "fmt" + "os" + "net/http" +) + +func root(w http.ResponseWriter, req *http.Request) { + fmt.Fprintf(w, "procfile_http_123") +} + +func main() { + port := os.Getenv("PORT") + if port == "" { port = "8080" } + + http.HandleFunc("/", root) + http.ListenAndServe(":" + port, nil) +} diff --git a/buildpacks/go/tests/integration_test.rs b/buildpacks/go/tests/integration_test.rs index 3f1581df..5963c0c6 100644 --- a/buildpacks/go/tests/integration_test.rs +++ b/buildpacks/go/tests/integration_test.rs @@ -121,6 +121,7 @@ fn test_worker_http_118() { &[ "Detected Go version requirement: ~1.18.1", "Installing go1.18.", + "Detected processes", "example.com/worker_http_118/cmd/web", "example.com/worker_http_118/cmd/worker", ], @@ -141,6 +142,18 @@ fn test_basic_http_119() { ); } +#[test] +#[ignore = "integration test"] +fn test_procfile_http_123() { + let build_config: BuildConfig = IntegrationTestConfig::new("procfile_http_123").into(); + TestRunner::default().build(build_config, |ctx| { + assert_contains!(ctx.pack_stdout, "Detected Go version requirement: =1.23"); + assert_contains!(ctx.pack_stdout, "Installing go1.23."); + assert_not_contains!(ctx.pack_stdout, "Setting launch table"); + assert_not_contains!(ctx.pack_stdout, "Detected processes:"); + }); +} + #[test] #[ignore = "integration test"] fn test_vendor_fasthttp_120() { From 1935d9cf516c1360421e73bd37edb1013cce818b Mon Sep 17 00:00:00 2001 From: Josh W Lewis Date: Thu, 12 Dec 2024 15:30:26 -0600 Subject: [PATCH 2/2] Log when we're skipping process detection --- buildpacks/go/src/main.rs | 6 ++++-- buildpacks/go/tests/integration_test.rs | 5 +++-- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/buildpacks/go/src/main.rs b/buildpacks/go/src/main.rs index 651df8d2..ef8a631f 100644 --- a/buildpacks/go/src/main.rs +++ b/buildpacks/go/src/main.rs @@ -140,8 +140,10 @@ impl Buildpack for GoBuildpack { cmd::go_install(&packages, &go_env).map_err(GoBuildpackError::GoBuild)?; let mut procs: Vec = vec![]; - if !Path::exists(&context.app_dir.join("Procfile")) { - log_header("Setting launch table"); + if Path::exists(&context.app_dir.join("Procfile")) { + log_info("Skipping launch process registration (Procfile detected)"); + } else { + log_header("Registering launch processes"); procs = proc::build_procs(&packages).map_err(GoBuildpackError::Proc)?; log_info("Detected processes:"); for proc in &procs { diff --git a/buildpacks/go/tests/integration_test.rs b/buildpacks/go/tests/integration_test.rs index 5963c0c6..8f21c4a0 100644 --- a/buildpacks/go/tests/integration_test.rs +++ b/buildpacks/go/tests/integration_test.rs @@ -121,7 +121,7 @@ fn test_worker_http_118() { &[ "Detected Go version requirement: ~1.18.1", "Installing go1.18.", - "Detected processes", + "Detected processes:", "example.com/worker_http_118/cmd/web", "example.com/worker_http_118/cmd/worker", ], @@ -149,7 +149,8 @@ fn test_procfile_http_123() { TestRunner::default().build(build_config, |ctx| { assert_contains!(ctx.pack_stdout, "Detected Go version requirement: =1.23"); assert_contains!(ctx.pack_stdout, "Installing go1.23."); - assert_not_contains!(ctx.pack_stdout, "Setting launch table"); + assert_contains!(ctx.pack_stdout, "Skipping launch process registration"); + assert_not_contains!(ctx.pack_stdout, "Registering launch processes"); assert_not_contains!(ctx.pack_stdout, "Detected processes:"); }); }