-
Couldn't load subscription status.
- Fork 10
Add Bazel path mapping support #71
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change | ||
|---|---|---|---|---|
|
|
@@ -3,17 +3,20 @@ | |||
| [clojure.data.json :as json] | ||||
| [clojure.java.io :as io] | ||||
| [clojure.spec.alpha :as s] | ||||
| [clojure.string :as str] | ||||
| [clojure.tools.cli :refer [parse-opts]] | ||||
| [rules-clojure.fs :as fs] | ||||
| [rules-clojure.jar :as jar] | ||||
| [rules-clojure.util :as util] | ||||
| [rules-clojure.persistent-classloader :as pcl]) | ||||
| (:import java.nio.charset.StandardCharsets | ||||
| java.util.concurrent.TimeUnit)) | ||||
| java.util.concurrent.TimeUnit | ||||
| [java.util.logging ConsoleHandler FileHandler Logger Level SimpleFormatter] | ||||
| java.lang.ProcessHandle)) | ||||
|
|
||||
| (s/def ::classes-dir string?) ;; path to the *compile-files* dir | ||||
| (s/def ::output-jar string?) ;; path where the output jar should be written | ||||
| (s/def ::srcs (s/coll-of string?)) ;; seq of paths to src files to put on classpath while compiling. Relative to src-dir | ||||
| (s/def ::src-dir (s/nilable string?)) ;; path to root of source tree, relative to execroot | ||||
| (s/def ::srcs (s/coll-of string?)) ;; seq of paths to src files to put on classpath while compiling. | ||||
| (s/def ::resources (s/coll-of string?)) ;; seq of paths to include in the jar | ||||
| (s/def ::aot-nses (s/coll-of string?)) ;; seq of namespaces to AOT | ||||
| (s/def ::classpath (s/coll-of string?)) ;; seq of jars to put on compile classpath | ||||
|
|
@@ -23,8 +26,7 @@ | |||
| ::classpath | ||||
| ::aot-nses] | ||||
| :opt-un [::resources | ||||
| ::srcs | ||||
| ::src-dir])) | ||||
| ::srcs])) | ||||
|
|
||||
| (s/def ::arguments (s/cat :c ::compile-req)) | ||||
|
|
||||
|
|
@@ -46,6 +48,24 @@ | |||
| (defn all-classpath-jars [classpath] | ||||
| (set classpath)) | ||||
|
|
||||
| ;; bazel requires us to write to stdout, and doesn't reliably report | ||||
| ;; stderr, so log to a temp file to guarantee we find everything. | ||||
|
|
||||
| (defn pid [] | ||||
| (-> (ProcessHandle/current) .pid)) | ||||
|
|
||||
| (defn configure-logging! [] | ||||
| (let [handler (FileHandler. (format "/tmp/rules-clojure-worker-%s.log" (pid))) | ||||
| formatter (SimpleFormatter.) | ||||
| logger (Logger/getLogger (str *ns*))] | ||||
| (.setFormatter handler formatter) | ||||
| (.addHandler logger handler) | ||||
| (.addHandler logger (ConsoleHandler.)) | ||||
| (.setLevel logger Level/INFO))) | ||||
|
|
||||
| (defn log [& args] | ||||
| (Logger/.log (Logger/getLogger (str *ns*)) Level/INFO (apply str args))) | ||||
|
|
||||
| (defn process-request | ||||
| [{:keys [classloader-strategy | ||||
| input-map] :as req}] | ||||
|
|
@@ -103,10 +123,9 @@ | |||
| real-out *out*] | ||||
| (let [exit (binding [*out* out-printer] | ||||
| (try | ||||
| (let [compile-req (json/read-str (first arguments) :key-fn keyword)] | ||||
| (process-request (assoc compile-req | ||||
| :classloader-strategy classloader-strategy | ||||
| :input-map (input-map inputs)))) | ||||
| (process-request (assoc work-req | ||||
| :classloader-strategy classloader-strategy | ||||
| :input-map (input-map inputs))) | ||||
| 0 | ||||
| (catch Throwable t | ||||
| (println t) ;; print to bazel str | ||||
|
|
@@ -117,42 +136,83 @@ | |||
| :output (str baos)} | ||||
| (when requestId | ||||
| {:requestId requestId}))] | ||||
| (util/print-err "persistent done:" resp) | ||||
| (.write real-out (json/write-str resp)) | ||||
| (.write real-out "\n") | ||||
| (.flush real-out)))) | ||||
|
|
||||
| ;; [--classes-dir bazel-out/darwin_arm64-fastbuild/bin/external/deps/.ns_metosin_reitit_core_reitit_exception.classes --output-jar bazel-out/darwin_arm64-fastbuild/bin/external/deps/ns_metosin_reitit_core_reitit_exception.jar --resource-strip-prefix '' --aot-ns reitit.exception --classpath external/deps/repository/metosin/reitit-core/0.6.0/reitit-core-0.6.0.jar:external/deps/repository/meta-merge/meta-merge/1.0.0/meta-merge-1.0.0.jar:external/deps/repository/org/clojure/clojure/1.12.2/clojure-1.12.2.jar:external/deps/repository/org/clojure/core.specs.alpha/0.4.74/core.specs.alpha-0.4.74.jar:external/deps/repository/org/clojure/spec.alpha/0.5.238/spec.alpha-0.5.238.jar:bazel-out/darwin_arm64-fastbuild/bin/external/rules_clojure/src/rules_clojure/libcompile.jar] | ||||
|
|
||||
|
Comment on lines
+144
to
+145
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||
| (defn parse-classpath [classpath-str] | ||||
| (str/split classpath-str #":")) | ||||
|
|
||||
| (def cli-options | ||||
| ;; An option with an argument | ||||
| [[nil "--classes-dir dir" "output directory where classfiles will be written"] | ||||
| [nil "--output-jar jar" "output jar name"] | ||||
| [nil "--resource-strip-prefix path" ] | ||||
| [nil "--aot-nses ns" "names of namespaces to AOT. May be repeated" | ||||
| :default [] | ||||
| :update-fn conj | ||||
| :multi true] | ||||
| [nil "--classpath cp" "classpath to use while compiling, separated by :" | ||||
| :parse-fn parse-classpath]]) | ||||
|
Comment on lines
+151
to
+159
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. does this need |
||||
|
|
||||
| (defn parse-arguments [^String args] | ||||
| {:post [(do (log "worker parse-req" args "=>" %) true)]} | ||||
| (-> args | ||||
| (parse-opts cli-options) | ||||
| :options)) | ||||
|
|
||||
| (defn process-persistent [] | ||||
| (let [executor (java.util.concurrent.Executors/newWorkStealingPool) | ||||
| classloader-strategy (pcl/caching-threadsafe)] | ||||
| (loop [] | ||||
| (if-let [line (read-line)] | ||||
| (let [work-req (json/read-str line :key-fn keyword)] | ||||
| (util/print-err "got req" work-req) | ||||
| (let [out *out* | ||||
| err *err*] | ||||
| (.submit executor ^Runnable (fn [] | ||||
| (binding [*out* out | ||||
| *err* err] | ||||
| (process-persistent-1 (assoc work-req | ||||
| :classloader-strategy classloader-strategy))))) | ||||
| (recur))) | ||||
| (do | ||||
| (util/print-err "no request, exiting") | ||||
| (.shutdown executor) | ||||
| (util/print-err "awating task completion") | ||||
| (util/print-err "finished cleanly?" (.awaitTermination executor 60 TimeUnit/SECONDS)) | ||||
| :exit))))) | ||||
| (log "blocking on read-line") | ||||
| (let [line (read-line)] | ||||
| (if (and line (seq line)) | ||||
| (let [_ (log "persistent: line" line) | ||||
| work-req (json/read-str line :key-fn keyword) | ||||
| arguments (parse-arguments (:arguments work-req)) | ||||
| prefix (:resource-strip-prefix arguments) | ||||
| _ (log "persistent: prefix:" prefix) | ||||
| arguments (if (seq prefix) | ||||
| (update arguments :classpath (fn [classpath] (distinct (conj classpath prefix)))) | ||||
| arguments) | ||||
| work-req (-> work-req | ||||
| (dissoc :arguments) | ||||
| (merge arguments))] | ||||
| (log "persistent: req" work-req) | ||||
| (let [out *out* | ||||
| err *err*] | ||||
| (.submit executor ^Runnable (fn [] | ||||
| (binding [*out* out | ||||
| *err* err] | ||||
| (process-persistent-1 (assoc work-req | ||||
| :classloader-strategy classloader-strategy))))) | ||||
| (recur))) | ||||
| (do | ||||
| (log "no request, exiting") | ||||
| (.shutdown executor) | ||||
| (log "awating task completion") | ||||
| (log "finished cleanly?" (.awaitTermination executor 60 TimeUnit/SECONDS)) | ||||
| :exit)))))) | ||||
|
|
||||
| (defn set-uncaught-exception-handler! [] | ||||
| (Thread/setDefaultUncaughtExceptionHandler | ||||
| (reify Thread$UncaughtExceptionHandler | ||||
| (uncaughtException [_ _ ex] | ||||
| (util/print-err ex "uncaught exception"))))) | ||||
| (log ex "uncaught exception"))))) | ||||
|
|
||||
| (defn -main [& args] | ||||
| (set-uncaught-exception-handler!) | ||||
| (configure-logging!) | ||||
| (let [persistent? (some (fn [a] (= "--persistent_worker" a)) args) | ||||
| f (if persistent? | ||||
| (fn [_args] (process-persistent)) | ||||
| process-ephemeral)] | ||||
| (f args))) | ||||
| (try | ||||
| (f args) | ||||
| (catch Exception e | ||||
| (log e) | ||||
| (throw e))))) | ||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
weird. these docs say
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah. It works ~80-90% of the time. The main place I've noticed it not working is when rules_clojure hangs forever and you have to interrupt it (Ctrl+C), you get no logs.