FIX: race condition during deploys creating stylesheets

FIX: race condition during deploys creating stylesheets

In some cases stylesheet cache path may be missing, ensure it exists prior to writing stylesheet

diff --git a/app/controllers/stylesheets_controller.rb b/app/controllers/stylesheets_controller.rb
index aa25b78..e5474da 100644
--- a/app/controllers/stylesheets_controller.rb
+++ b/app/controllers/stylesheets_controller.rb
@@ -57,7 +57,9 @@ class StylesheetsController < ApplicationController
 
     # Security note, safe due to route constraint
     underscore_digest = digest ? "_" + digest : ""
-    location = "#{Rails.root}/#{Stylesheet::Manager::CACHE_PATH}/#{target}#{underscore_digest}#{extension}"
+
+    cache_path = "#{Rails.root}/#{Stylesheet::Manager::CACHE_PATH}"
+    location = "#{cache_path}/#{target}#{underscore_digest}#{extension}"
 
     stylesheet_time = query.pluck(:created_at).first
 
@@ -71,6 +73,7 @@ class StylesheetsController < ApplicationController
 
     unless File.exist?(location)
       if current = query.limit(1).pluck(source_map ? :source_map : :content).first
+        FileUtils.mkdir_p(cache_path)
         File.write(location, current)
       else
         raise Discourse::NotFound

GitHub sha: 0bf3316a

1 Like