Skip to content
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

Load a script file in the repl without some things getting evaluated, but when the script is run they should be evaluated #4

Closed
PEZ opened this issue Apr 28, 2022 · 12 comments · Fixed by #39
Assignees
Labels

Comments

@PEZ
Copy link
Collaborator

PEZ commented Apr 28, 2022

I want to be able to load a script file in the repl without some things getting evaluated, and have those evaluated if the script is being run.

Something like:

(ns something ...)

(defn some-fn-that-should-only-be-called-when-script-run []
  ...)

(when-not *repl-eval?*
  (some-fn-that-should-only-be-called-when-script-run))

And I'd like to take a stab at this. 😄

@PEZ PEZ added the repl label Apr 28, 2022
@PEZ PEZ self-assigned this Apr 28, 2022
@borkdude
Copy link
Collaborator

@PEZ I think it's good to ask ourselves: how can we support this without adding options. Are there any existing solutions around that Clojure users are already familiar with? Why is there no such a thing in Clojure proper? Etc.

@borkdude
Copy link
Collaborator

E.g. one solution that already just works in user space is:

(def dev? true)

(when-not dev?
  ...)

@PEZ
Copy link
Collaborator Author

PEZ commented Apr 28, 2022

Yes, there are probably much better alternatives. I am not really floating solution ideas here, more the need. The ”idea”, insofar there is one, was inspired by how I use Babashka.

One solution would be to do the same as with Babashka. I.e. set *file* when it is run as a script, but not when loaded in the REPL. (Though, you told me offline that dynamic vars might not fit in this execution context, but anyway.)

Why is there no such a thing in Clojure proper?

I think it is because it was not designed as a scripting language.

@PEZ
Copy link
Collaborator Author

PEZ commented Apr 28, 2022

I solved my use case for now like so:

(ns ignore-form
  (:require ["vscode" :as vsode]
            [promesa.core :as p]
            [z-joylib.vscode-utils :as vsu]))

(defonce run-main? true)

(comment
  ;; Loading this in the REPL w/o evaluating `main`:
  (defonce run-main? false)  ; <- First evaluate this
  (ns-unmap *ns* 'run-main?) ; <- Evaluate this when you are done
                             ;    or want to test-run the script
  )

(defn main []
  (p/let [editor ^js vscode/window.activeTextEditor
          original-selection (vsu/current-selection)
          _ (vscode/commands.executeCommand "paredit.backwardUpSexp")
          insert-position (.-active (vsu/current-selection))]
    (aset editor "selection" original-selection)
    (p/do! (vsu/insert-text!+ "#_" editor insert-position))))

(when run-main?
  (main))

(comment  
  (main)
  )

@borkdude
Copy link
Collaborator

I.e. set file when it is run as a script, but not when loaded in the REPL.

Depending on how the nREPL implementation works, that might already be true.

For nbb there is a similar issue: babashka/nbb#95

The way it's phased there is "detect the invoked script", I think that might be a better way to describe the problem.

The solution there looks as follows:

(= nbb.core/*file* (:file (meta #'init)))

which says: is the file that's currently being executed, the same as where one of the functions in this namespace is defined?

I don't think *file* is being set at all currently which might be a nice thing to fix first if you want :).

@PEZ
Copy link
Collaborator Author

PEZ commented Apr 28, 2022

Depending on how the nREPL implementation works, that might already be true.

First thing I tried. 😄 Didn't work. Could be that I didn't check it properly. Now that I try it I see that I have an error:

SyntaxError: Invalid regular expression: /*file*/: Nothing to repeat
    at new RegExp (<anonymous>)
    at Object.cljs$core$re_pattern [as re_pattern] (/Users/pez/Projects/joyride/out/js/cljs-runtime/cljs/core.cljs:10181:5)
    at Object.joyride$repl_utils$match [as match] (/Users/pez/Projects/joyride/out/js/cljs-runtime/joyride/repl_utils.cljs:22:13)
    at /Users/pez/Projects/joyride/out/js/cljs-runtime/joyride/repl_utils.cljs:103:38
    at /Users/pez/Projects/joyride/out/js/cljs-runtime/cljs/core.cljs:4441:22
    at /Users/pez/Projects/joyride/out/js/cljs-runtime/cljs/core.cljs:4442:15
    at Object.sval (/Users/pez/Projects/joyride/out/js/cljs-runtime/cljs/core.cljs:3456:18)
    at Object.cljs$core$ISeqable$_seq$arity$1 (/Users/pez/Projects/joyride/out/js/cljs-runtime/cljs/core.cljs:3446:1)
    at Object.cljs$core$seq [as seq] (/Users/pez/Projects/joyride/out/js/cljs-runtime/cljs/core.cljs:1236:13)
    at /Users/pez/Projects/joyride/out/js/cljs-runtime/cljs/core.cljs:3811:15
    at Object.sval (/Users/pez/Projects/joyride/out/js/cljs-runtime/cljs/core.cljs:3456:18)
    at Object.cljs$core$ISeqable$_seq$arity$1 (/Users/pez/Projects/joyride/out/js/cljs-runtime/cljs/core.cljs:3446:1)
    at Object.cljs$core$seq [as seq] (/Users/pez/Projects/joyride/out/js/cljs-runtime/cljs/core.cljs:1236:13)
    at /Users/pez/Projects/joyride/out/js/cljs-runtime/cljs/core.cljs:4750:18
    at Object.sval (/Users/pez/Projects/joyride/out/js/cljs-runtime/cljs/core.cljs:3456:18)
    at Object.cljs$core$ISeqable$_seq$arity$1 (/Users/pez/Projects/joyride/out/js/cljs-runtime/cljs/core.cljs:3446:1)
    at Object.cljs$core$seq [as seq] (/Users/pez/Projects/joyride/out/js/cljs-runtime/cljs/core.cljs:1236:13)
    at Function.cljs$core$IFn$_invoke$arity$3 (/Users/pez/Projects/joyride/out/js/cljs-runtime/cljs/core.cljs:1866:28)
    at Function.cljs$core$IFn$_invoke$arity$3 (/Users/pez/Projects/joyride/out/js/cljs-runtime/cljs/core.cljs:1936:10)
    at /Users/pez/Projects/joyride/out/js/cljs-runtime/cljs/core.cljs:9531:21
    at /Users/pez/Projects/joyride/out/js/cljs-runtime/cljs.core.js:30971:3
    at Object.sval (/Users/pez/Projects/joyride/out/js/cljs-runtime/cljs/core.cljs:3456:18)
    at Object.cljs$core$ISeqable$_seq$arity$1 (/Users/pez/Projects/joyride/out/js/cljs-runtime/cljs/core.cljs:3446:1)
    at Object.cljs$core$seq [as seq] (/Users/pez/Projects/joyride/out/js/cljs-runtime/cljs/core.cljs:1236:13)
    at Function.cljs$core$IFn$_invoke$arity$3 (/Users/pez/Projects/joyride/out/js/cljs-runtime/cljs/core.cljs:2497:26)
    at Object.cljs$core$IReduce$_reduce$arity$3 (/Users/pez/Projects/joyride/out/js/cljs-runtime/cljs/core.cljs:3523:28)
    at Function.cljs$core$IFn$_invoke$arity$3 (/Users/pez/Projects/joyride/out/js/cljs-runtime/cljs/core.cljs:2569:17)
    at Object.cljs$core$vec [as vec] (/Users/pez/Projects/joyride/out/js/cljs-runtime/cljs/core.cljs:5755:7)
    at Object.joyride$repl_utils$handle_complete_STAR_ [as handle_complete_STAR_] (/Users/pez/Projects/joyride/out/js/cljs-runtime/joyride/repl_utils.cljs:106:29)
    at joyride$nrepl$handle_complete (/Users/pez/Projects/joyride/out/js/cljs-runtime/joyride/nrepl.cljs:123:20)
    at joyride$nrepl$handle_request (/Users/pez/Projects/joyride/out/js/cljs-runtime/joyride/nrepl.cljs:139:6)
    at /Users/pez/Projects/joyride/out/js/cljs-runtime/joyride/nrepl.cljs:31:6
    at /Users/pez/Projects/joyride/out/js/cljs-runtime/joyride/nrepl.cljs:47:6
    at /Users/pez/Projects/joyride/out/js/cljs-runtime/joyride/nrepl.cljs:37:6
    at Socket.<anonymous> (/Users/pez/Projects/joyride/out/js/cljs-runtime/joyride/nrepl.cljs:176:14)
    at Socket.emit (/Users/pez/Projects/joyride/lib/events.js:390:28)
    at addChunk (/Users/pez/Projects/joyride/lib/internal/streams/readable.js:315:12)
    at readableAddChunk (/Users/pez/Projects/joyride/lib/internal/streams/readable.js:289:9)
    at Socket.push (/Users/pez/Projects/joyride/lib/internal/streams/readable.js:228:10)
    at TCP.onStreamRead (/Users/pez/Projects/joyride/lib/internal/stream_base_commons.js:199:23)
    at TCP.callbackTrampoline (node:internal/async_hooks:130:17) {stack: 'SyntaxError: Invalid regular expression: /*fi…Trampoline (node:internal/async_hooks:130:17)', message: 'Invalid regular expression: /*file*/: Nothing to repeat'}

@borkdude
Copy link
Collaborator

@PEZ It looks like you copy pasted from markdown ;)

@PEZ PEZ changed the title Some way to check if it's the REPL that evaluating or a script being run Load a script file in the repl without some things getting evaluated, but when the script is run they should be evaluated Apr 28, 2022
@PEZ
Copy link
Collaborator Author

PEZ commented Apr 28, 2022

It looks like you copy pasted from markdown ;)

Please elaborate, I want to understand the joke. 😄

@borkdude
Copy link
Collaborator

I see /*file*/ in your error which seems markdown for marking up *file* in cursive.

@PEZ PEZ linked a pull request May 1, 2022 that will close this issue
@PEZ
Copy link
Collaborator Author

PEZ commented May 1, 2022

Follow up on comments on #18:

Hmm... So not for this PR, but thinking about how to solve #4:

In babashska I do things like:

(let [run-script? (= *file* (System/getProperty "babashka.file")) ...])

Where the babashka.file property is nil on nREPL load-file, but the script file name when run by bb.

I like that pattern (think I picked it up from the Babashka book). What would a Joyride equivalent be?

@borkdude
Copy link
Collaborator

borkdude commented May 1, 2022

I think that would be a function: (joyride.core/invoked-script)

@PEZ
Copy link
Collaborator Author

PEZ commented May 1, 2022

Awesome. You have suggested that elsewhere. Now I understand it. 😄

@PEZ PEZ removed a link to a pull request May 1, 2022
PEZ added a commit that referenced this issue May 8, 2022
@PEZ PEZ closed this as completed in #39 May 9, 2022
PEZ added a commit that referenced this issue May 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants