Skip to content

Better support for running scripts. #7842

Closed

Description

@bacchanalia

cabal run can run scripts, but the utility of this is limited because under the current design it recompiles on every invocation.

I propose adding the following:

  • fake packages for scripts to be written under ~/.local/share (or os equivalent)
    • specifically ~/.local/share/cabal/<abs path of script without extension>/ to avoid collisions
  • the option --tmp-script-artifacts (current behaviour. keep as default?)
  • the option --persist-script-artifacts (new behaviour)
  • the option --script-artifact-dir=DIR (place fake package in user supplied directory)
  • script support for cabal build and cabal repl
  • updates to the relevant docs

I have done a quick test with hard coded paths, and it works as expected without further changes. The only changes to run necessary are to add the flags and pass the correct directory based on them. I haven't scoped out the work needed to add support to build and repl, but I assume it would be very similar to how it works now in run.

Activity

  1. jneira commented on Nov 30, 2021

    @jneira
    Member
  2. Mikolaj commented on Nov 30, 2021

    @Mikolaj
    Member

    also #7393

  3. jneira commented on Nov 30, 2021

    @jneira
    Member

    the option --tmp-script-artifacts (current behaviour. keep as default?)
    the option --persist-script-artifacts (new behaviour)
    the option --script-artifact-dir=DIR (place fake package in user supplied directory)

    bikeshedding:

    • no option set or -no-persist-fake-package > current behavior
    • persist-fake-package > save to ./local/share
    • persist-fake-package = dir > save to dir

    Trying to generalize the behaviour for fake packages and minimize the number of flags, independently of where are being used.
    Not sure if the generalization makes sense, in that case replace persist-fake-package by persist-script-build.
    Otoh the proper solution should be remove the need of fake-package at all in the entire codebase (see #6977), which could open the door to other improvements

  4. bacchanalia commented on Nov 30, 2021

    @bacchanalia
    CollaboratorAuthor

    When else are fake packages used other than to build scripts?

    edit: This is discussed in issue #6977. I shouldn't comment before coffee.

  5. bacchanalia commented on Nov 30, 2021

    @bacchanalia
    CollaboratorAuthor

    Cache folders on different OS's (please correct me if I'm wrong):

    • on Linux save to ~/.cache (not ~/.local/share)
    • on Windows save to %LOCALAPPDATA%
    • on OSX save to ~/Library/Caches

    It probably makes sense to add a utility function (in Distribution.Simple.Utils?) to get the cache folder.
    Rather than putting the fake-package directory structures directly in <cache>/cabal, it might be a better idea to put them in <cache>/cabal/fake-packages in case other uses for <cache>/cabal come up.

  6. bacchanalia commented on Nov 30, 2021

    @bacchanalia
    CollaboratorAuthor

    @Mikolaj I'm not sure I see the relevance of #7393?

    @fendor What's the status of the work you were doing on #6149?

    @jneira re #6977: I'm not sure caching makes sense for other uses of fake package. At the very least you'd need a completely separate scheme for where to place/how to identify cached instances.

  7. Mikolaj commented on Nov 30, 2021

    @Mikolaj
    Member

    @bacchanalia: re #7393, you complain "it recompiles on every invocation", and ghc --run would be able to execute a script without producing any compilation artefacts (and without linking) at all. Therefore, it could potentially be used as, in addition to or instead of --tmp-script-artifact. If we assume scripts are never computationally intensive, perhaps it could be used instead of cabal run always? Am I missing something?

  8. bacchanalia commented on Nov 30, 2021

    @bacchanalia
    CollaboratorAuthor

    @Mikolaj running a script with cabal run allows you do have a cabal block

    {- cabal:
    build-depends: ...
    -}

    while ghc --run presumably just uses the global environment.

    If we assume scripts are never computationally intensive

    I don't think this is a good assumption. Scripts and packages have different ergonomics, and when you might prefer one over the doesn't neatly fall on that boundary, for me, anyway.

    Another consideration is start-up time. On my machine running hello.hs with runghc takes 25ms, while cached cabal run only takes 15ms (and hopefully that could go lower, but that's out of scope) which is important in some applications.

  9. Mikolaj commented on Nov 30, 2021

    @Mikolaj
    Member

    Fine. And what about ghc --run (or --eval, but I don't remember the details) underlying --tmp-script-artifact? Is there a value in the temporary artifacts being written somewhere at all?

  10. fendor commented on Nov 30, 2021

    @fendor
    Collaborator

    What's the status of the work you were doing on #6149?

    Stalled. I don't know how much everyone else knows about this feature, so I'll repeat a bit how this currently works: The script section (which is quite literally an executable section from a .cabal file [btw, you can break it if you put main-is: SomeName.hs into the script section]) is parsed and based on it, a .cabal file is generated and written to a project file in $TMP. The source is copied to that directory as well, iirc, and then cabal build && cabal exec is basically executed on that project in $TMP.

    Doing the same for v2-repl File.hs doesn't cut it, since updates to our original script file won't be noticed, we lose the current working directory, etc... I played around with workarounds, e.g. after invoking cabal v2-repl in that directory, cd to the previous working directory, and listing the full path to the script instead of copying the source script... I can't remember whether there was a blocking issue with cding and listing the full path to the source file, but it was so weird that I wondered whether I would make everything worse by applying duct-tape to this design and stopped with my experiments.

  11. bacchanalia commented on Nov 30, 2021

    @bacchanalia
    CollaboratorAuthor

    @Mikolaj That might be preferable in the --tmp-script-artifact case, but there might be some value in having consistency between the cases both in terms of implementation and use? Also a user might want to inspect the output. I don't really feel strongly about it either way.

  12. Mikolaj commented on Nov 30, 2021

    @Mikolaj
    Member

    Sure, it's your call as the implementer and, personally, I'd not expend any extra effort (unless minimal) until users express interest.

  13. bacchanalia commented on Nov 30, 2021

    @bacchanalia
    CollaboratorAuthor

    @fendor In the thread for #6149 you mention that stack handles :r correctly. Did you investigate what they do?

  14. 24 remaining items

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions