Repository: railwayapp/nixpacks
Pull request #674 (2022-11-11 19:04:16)

servce static files
This PR sets two additional environment variables for rails apps
- `RAILS_SERVE_STATIC_FILES=1`

Fixes https://github.com/railwayapp/nixpacks/issues/672


Patch:
diff --git a/src/nixpacks/app.rs b/src/nixpacks/app.rs
index 4cc34ce..8932955 100644
--- a/src/nixpacks/app.rs
+++ b/src/nixpacks/app.rs
@@ -141,6 +141,25 @@ impl App {
         self.source.join(name).is_dir()
     }
 
+    #[cfg(target_os = "windows")]
+    pub fn is_file_executable(&self, name: &str) -> bool {
+        true
+    }
+
+    /// Check if a path is an executable file
+    #[cfg(not(target_os = "windows"))]
+    pub fn is_file_executable(&self, name: &str) -> bool {
+        use std::os::unix::prelude::PermissionsExt;
+
+        let path = self.source.join(name);
+        if path.is_file() {
+            let metadata = path.metadata().unwrap();
+            metadata.permissions().mode() & 0o111 != 0
+        } else {
+            false
+        }
+    }
+
     pub fn read_json<T>(&self, name: &str) -> Result<T>
     where
         T: DeserializeOwned,
@@ -228,6 +247,15 @@ mod tests {
         Ok(())
     }
 
+    #[test]
+    #[cfg(not(target_os = "windows"))]
+    fn test_is_file_executable() -> Result<()> {
+        let app = App::new("./examples/java-gradle-hello-world")?;
+        assert!(app.is_file_executable("gradlew"));
+        assert!(!app.is_file_executable("build.gradle"));
+        Ok(())
+    }
+
     #[test]
     fn test_read_json_file() -> Result<()> {
         let app = App::new("./examples/node-npm")?;
diff --git a/src/providers/java.rs b/src/providers/java.rs
index 8d78c10..842ec03 100644
--- a/src/providers/java.rs
+++ b/src/providers/java.rs
@@ -36,8 +36,15 @@ impl Provider for JavaProvider {
             let pkgs = self.get_jdk_and_gradle_pkgs(app)?;
             setup = Phase::setup(Some(pkgs));
 
+            let mut build = Phase::build(None);
             let gradle_exe = self.get_gradle_exe(app);
-            let mut build = Phase::build(Some(format!("{} build -x check", gradle_exe)));
+
+            // Ensure the gradlew file is executable
+            if app.includes_file("./gradlew") && !app.is_file_executable("gradlew") {
+                build.add_cmd("chmod +x gradlew");
+            }
+
+            build.add_cmd(format!("{} build -x check", gradle_exe));
             build.add_cache_directory("/root/.gradle");
             build
         } else {
diff --git a/src/providers/ruby.rs b/src/providers/ruby.rs
index 67cd600..78c6207 100644
--- a/src/providers/ruby.rs
+++ b/src/providers/ruby.rs
@@ -150,9 +150,12 @@ impl RubyProvider {
             ),
             ("MALLOC_ARENA_MAX".to_string(), "2".to_string()),
         ]);
+
         if self.is_rails_app(app) {
             env_vars.insert("RAILS_LOG_TO_STDOUT".to_string(), "enabled".to_string());
+            env_vars.insert("RAILS_SERVE_STATIC_FILES".to_string(), "1".to_string());
         }
+
         Ok(env_vars)
     }
 


Test patch:
diff --git a/tests/snapshots/generate_plan_tests__ruby_rails_postgres.snap b/tests/snapshots/generate_plan_tests__ruby_rails_postgres.snap
index 8126cce..feb6029 100644
--- a/tests/snapshots/generate_plan_tests__ruby_rails_postgres.snap
+++ b/tests/snapshots/generate_plan_tests__ruby_rails_postgres.snap
@@ -11,7 +11,8 @@ expression: plan
     "GEM_PATH": "/usr/local/rvm/gems/3.1.2:/usr/local/rvm/gems/3.1.2@global",
     "MALLOC_ARENA_MAX": "2",
     "NIXPACKS_METADATA": "ruby",
-    "RAILS_LOG_TO_STDOUT": "enabled"
+    "RAILS_LOG_TO_STDOUT": "enabled",
+    "RAILS_SERVE_STATIC_FILES": "1"
   },
   "phases": {
     "build": {