Repository: railwayapp/nixpacks
Pull request #510 (2022-09-07 06:33:07)

Set `PYTHONUNBUFFERED=1` for the Python provider
This PR sets `PYTHONUNBUFFERED=1` for the Python provider so that logs are immediately sent to stdout instead of buffering.

Fixes https://github.com/railwayapp/nixpacks/issues/503

https://stackoverflow.com/a/59812588


Patch:
diff --git a/src/nixpacks/builder/docker/dockerfile_generation.rs b/src/nixpacks/builder/docker/dockerfile_generation.rs
index f2e6760..d31c16c 100644
--- a/src/nixpacks/builder/docker/dockerfile_generation.rs
+++ b/src/nixpacks/builder/docker/dockerfile_generation.rs
@@ -308,7 +308,7 @@ impl DockerfileGenerator for Phase {
                 .to_slash()
                 .context("Failed to convert nix file path to slash path.")?;
             format!(
-                "COPY {nix_file_path} {nix_file_path}\nRUN nix-env -if {nix_file_path}",
+                "COPY {nix_file_path} {nix_file_path}\nRUN nix-env -if {nix_file_path} && nix-collect-garbage -d",
                 nix_file_path = nix_file_path
             )
         } else {
diff --git a/src/providers/python.rs b/src/providers/python.rs
index 7cd87ee..12ae528 100644
--- a/src/providers/python.rs
+++ b/src/providers/python.rs
@@ -61,6 +61,11 @@ impl Provider for PythonProvider {
             plan.set_start_phase(start);
         }
 
+        plan.add_variables(EnvironmentVariables::from([(
+            "PYTHONUNBUFFERED".to_owned(),
+            "1".to_owned(),
+        )]));
+
         if app.includes_file("poetry.lock") {
             plan.add_variables(EnvironmentVariables::from([(
                 "NIXPACKS_POETRY_VERSION".to_string(),


Test patch:
diff --git a/tests/snapshots/generate_plan_tests__python.snap b/tests/snapshots/generate_plan_tests__python.snap
index cc553dd..c810908 100644
--- a/tests/snapshots/generate_plan_tests__python.snap
+++ b/tests/snapshots/generate_plan_tests__python.snap
@@ -6,7 +6,8 @@ expression: plan
   "nixpacksVersion": "[version]",
   "buildImage": "[build_image]",
   "variables": {
-    "NIXPACKS_METADATA": "python"
+    "NIXPACKS_METADATA": "python",
+    "PYTHONUNBUFFERED": "1"
   },
   "phases": [
     {
diff --git a/tests/snapshots/generate_plan_tests__python_2.snap b/tests/snapshots/generate_plan_tests__python_2.snap
index 87ca7c9..3d47001 100644
--- a/tests/snapshots/generate_plan_tests__python_2.snap
+++ b/tests/snapshots/generate_plan_tests__python_2.snap
@@ -6,7 +6,8 @@ expression: plan
   "nixpacksVersion": "[version]",
   "buildImage": "[build_image]",
   "variables": {
-    "NIXPACKS_METADATA": "python"
+    "NIXPACKS_METADATA": "python",
+    "PYTHONUNBUFFERED": "1"
   },
   "phases": [
     {
diff --git a/tests/snapshots/generate_plan_tests__python_2_runtime.snap b/tests/snapshots/generate_plan_tests__python_2_runtime.snap
index 87ca7c9..3d47001 100644
--- a/tests/snapshots/generate_plan_tests__python_2_runtime.snap
+++ b/tests/snapshots/generate_plan_tests__python_2_runtime.snap
@@ -6,7 +6,8 @@ expression: plan
   "nixpacksVersion": "[version]",
   "buildImage": "[build_image]",
   "variables": {
-    "NIXPACKS_METADATA": "python"
+    "NIXPACKS_METADATA": "python",
+    "PYTHONUNBUFFERED": "1"
   },
   "phases": [
     {
diff --git a/tests/snapshots/generate_plan_tests__python_django.snap b/tests/snapshots/generate_plan_tests__python_django.snap
index aeb2a42..282114b 100644
--- a/tests/snapshots/generate_plan_tests__python_django.snap
+++ b/tests/snapshots/generate_plan_tests__python_django.snap
@@ -6,7 +6,8 @@ expression: plan
   "nixpacksVersion": "[version]",
   "buildImage": "[build_image]",
   "variables": {
-    "NIXPACKS_METADATA": "python,django,postgres"
+    "NIXPACKS_METADATA": "python,django,postgres",
+    "PYTHONUNBUFFERED": "1"
   },
   "phases": [
     {
diff --git a/tests/snapshots/generate_plan_tests__python_django_mysql.snap b/tests/snapshots/generate_plan_tests__python_django_mysql.snap
index e380b74..8ef6fa5 100644
--- a/tests/snapshots/generate_plan_tests__python_django_mysql.snap
+++ b/tests/snapshots/generate_plan_tests__python_django_mysql.snap
@@ -6,7 +6,8 @@ expression: plan
   "nixpacksVersion": "[version]",
   "buildImage": "[build_image]",
   "variables": {
-    "NIXPACKS_METADATA": "python,django"
+    "NIXPACKS_METADATA": "python,django",
+    "PYTHONUNBUFFERED": "1"
   },
   "phases": [
     {
diff --git a/tests/snapshots/generate_plan_tests__python_numpy.snap b/tests/snapshots/generate_plan_tests__python_numpy.snap
index cc553dd..c810908 100644
--- a/tests/snapshots/generate_plan_tests__python_numpy.snap
+++ b/tests/snapshots/generate_plan_tests__python_numpy.snap
@@ -6,7 +6,8 @@ expression: plan
   "nixpacksVersion": "[version]",
   "buildImage": "[build_image]",
   "variables": {
-    "NIXPACKS_METADATA": "python"
+    "NIXPACKS_METADATA": "python",
+    "PYTHONUNBUFFERED": "1"
   },
   "phases": [
     {
diff --git a/tests/snapshots/generate_plan_tests__python_poetry.snap b/tests/snapshots/generate_plan_tests__python_poetry.snap
index 0d1379e..3318e3c 100644
--- a/tests/snapshots/generate_plan_tests__python_poetry.snap
+++ b/tests/snapshots/generate_plan_tests__python_poetry.snap
@@ -7,7 +7,8 @@ expression: plan
   "buildImage": "[build_image]",
   "variables": {
     "NIXPACKS_METADATA": "python,poetry",
-    "NIXPACKS_POETRY_VERSION": "1.1.13"
+    "NIXPACKS_POETRY_VERSION": "1.1.13",
+    "PYTHONUNBUFFERED": "1"
   },
   "phases": [
     {
diff --git a/tests/snapshots/generate_plan_tests__python_procfile.snap b/tests/snapshots/generate_plan_tests__python_procfile.snap
index f7641b1..f464157 100644
--- a/tests/snapshots/generate_plan_tests__python_procfile.snap
+++ b/tests/snapshots/generate_plan_tests__python_procfile.snap
@@ -6,7 +6,8 @@ expression: plan
   "nixpacksVersion": "[version]",
   "buildImage": "[build_image]",
   "variables": {
-    "NIXPACKS_METADATA": "python"
+    "NIXPACKS_METADATA": "python",
+    "PYTHONUNBUFFERED": "1"
   },
   "phases": [
     {
diff --git a/tests/snapshots/generate_plan_tests__python_setuptools.snap b/tests/snapshots/generate_plan_tests__python_setuptools.snap
index 1fe1b7f..23a0fa4 100644
--- a/tests/snapshots/generate_plan_tests__python_setuptools.snap
+++ b/tests/snapshots/generate_plan_tests__python_setuptools.snap
@@ -6,7 +6,8 @@ expression: plan
   "nixpacksVersion": "[version]",
   "buildImage": "[build_image]",
   "variables": {
-    "NIXPACKS_METADATA": "python"
+    "NIXPACKS_METADATA": "python",
+    "PYTHONUNBUFFERED": "1"
   },
   "phases": [
     {