# Suggestion for a new package: MinimalWorkingExamples.jl

**URL:** <https://discourse.julialang.org/t/suggestion-for-a-new-package-minimalworkingexamples-jl/59391>\
**Category:** Package Announcements\
**Tags:** question, package\
**Created:** [April 16, 2021, 7:43am UTC](https://discourse.julialang.org/t/suggestion-for-a-new-package-minimalworkingexamples-jl/59391 "2021-04-16T07:43:27Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![yakir12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yakir12/32/297_2.png) [@yakir12](https://discourse.julialang.org/u/yakir12)\
**Post date:** [April 16, 2021, 7:43am UTC](https://discourse.julialang.org/t/suggestion-for-a-new-package-minimalworkingexamples-jl/59391/1 "2021-04-16T07:43:27Z")

</div>

I have an idea for a package and am wondering if anyone feels like implementing it.

## `MinimalWorkingExamples.jl`

A package for Minimal Working Examples (MWE), where by `using MinimalWorkingExamples` the current REPL session

1. activates a temporary environment
2. catches all the `using SomePackage`
3. adds them to the environment
4. and finally runs the example

The idea isn’t to cover all edge cases but to be pretty simple as MWEs are expected to be. This will simply save the extra steps of activating a temporary environment and adding all the dependencies before trying out people’s MWEs. This tool should make it a lot easier for people to try out other people’s MWEs: just copy-paste into the REPL.

Of course, this would mean that people would need to have `MinimalWorkingExamples.jl` as a dependency in their main environment, and that all MWE code should begin with `using MinimalWorkingExamples`.

What do you think? Wanna build the next big thing in Julia? 😉

---

<div class="post-metadata">

**Author:** ![feanor12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/feanor12/32/8212_2.png) [@feanor12](https://discourse.julialang.org/u/feanor12)\
**Post date:** [April 16, 2021, 7:57am UTC](https://discourse.julialang.org/t/suggestion-for-a-new-package-minimalworkingexamples-jl/59391/2 "2021-04-16T07:57:39Z")

</div>

So this would be used as a boilerplate for a MWE?

The “catches all the `using SomePackage`” part I don’t get.  
Would `MinimalWorkingExamples` load a jl source file and generate a REPL + environment out of this?

Would this be an user story you think of?  
1.) Person A: write your normal MWE  
2.) Person A: send jl file to person B  
3.) Person B: open repl  
4.) Person B: load MinimalWorkingExamples  
5.) Person B: load jl file using MinimalWorkingExamples  
6.) MinimalWorkingExamples: generate environment  
7.) MinimalWorkingExamples: parse jl file for using or import or include  
8.) MinimalWorkingExamples: add packages to enivronment  
9.) MinimalWorkingExamples: run jl file (+ benchmark, export package versions; julia version, etc)  
10.) Person B: modify jl file  
11.) Person B: goto 5 if not happy  
12.) Person B: Send new jl file and generated statistics / meta info to person A  
13.) Person A is now person B  
14.) Goto 3

---

<div class="post-metadata">

**Author:** ![yakir12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yakir12/32/297_2.png) [@yakir12](https://discourse.julialang.org/u/yakir12)\
**Post date:** [April 16, 2021, 8:05am UTC](https://discourse.julialang.org/t/suggestion-for-a-new-package-minimalworkingexamples-jl/59391/3 "2021-04-16T08:05:49Z")

</div>

> [@feanor12](#):
>
> So this would be used as a boilerplate for a MWE?

Yes.

> [@feanor12](#):
>
> The "catches all the `using SomePackage` " part I don’t get.  
> Would `MinimalWorkingExamples` load a jl source file and generate a REPL + environment out of this?

No. The main use case would be code you want people to copy (from Discourse, Slack, Zulip, Discord, or anywhere really) and paste in the (existing or fresh) REPL. No files involved, just simple code. For more complicated (and thus perhaps not so minimal MWE) examples, ideally a Project and Manifest files will be involved, but that’s not the intended use of `MinimalWorkingExamples.jl`.

And to clarify the "catches all the `using SomePackage` ", I was hoping that some meta-programming magic could do that…

---

<div class="post-metadata">

**Author:** ![feanor12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/feanor12/32/8212_2.png) [@feanor12](https://discourse.julialang.org/u/feanor12)\
**Post date:** [April 16, 2021, 8:21am UTC](https://discourse.julialang.org/t/suggestion-for-a-new-package-minimalworkingexamples-jl/59391/4 "2021-04-16T08:21:03Z")

</div>

To start julia with a temp env you can run

```julia
julia --project=$(mktemp -d) # does not work on windows
julia --eval "using Pkg;Pkg.activate(mktempdir())" # does not enter the REPL

```

> [@Cmdline option/shortcut for activate --temp?](https://discourse.julialang.org/t/cmdline-option-shortcut-for-activate-temp/53956):
>
> Do we have a command line option to start julia with a temporary environment, instead of having to manually ] activate --temp? I imagine something like julia --project=@temp. If not, what do you think, should we add something like this?

Basically the new package would need to introduce a new REPL mode where packages are auto-installed on entering a using/import command.  
1.) Open REPL  
2.) Switch to “experiment” mode  
2.1) New environment is generated and activated  
3.) Enter code  
3.1) Code is scanned for using and intercepted to install the packages

---

<div class="post-metadata">

**Author:** ![yakir12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yakir12/32/297_2.png) [@yakir12](https://discourse.julialang.org/u/yakir12)\
**Post date:** [April 16, 2021, 8:30am UTC](https://discourse.julialang.org/t/suggestion-for-a-new-package-minimalworkingexamples-jl/59391/5 "2021-04-16T08:30:38Z")

</div>

> [@feanor12](#):
>
> To start julia with a temp env you can

Not that it matters too much how that specific step is achieved, but

```julia
julia> import Pkg

julia> Pkg.activate(temp = true)
  Activating new environment at `/tmp/jl_Rqkofw/Project.toml`

```

Or

```julia
]
(@v1.6) pkg> activate --temp
  Activating new environment at `/tmp/jl_Z2tBdP/Project.toml`

(jl_Z2tBdP) pkg> 

```

> [@feanor12](#):
>
> introduce a new REPL mode

Well, if that’s the only way. But in the interest of keeping things as simple as possible for the user, it would be great if there was no need to switch to a new REPL mode. To have that work, we would not only need people to have a `MinimalWorkingExamples` dependency in their main environment, but also import it in every Julia session (in case they want to use the new REPL mode), which seems a bit too much to hope for.

---

<div class="post-metadata">

**Author:** ![feanor12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/feanor12/32/8212_2.png) [@feanor12](https://discourse.julialang.org/u/feanor12)\
**Post date:** [April 16, 2021, 8:42am UTC](https://discourse.julialang.org/t/suggestion-for-a-new-package-minimalworkingexamples-jl/59391/6 "2021-04-16T08:42:09Z")

</div>

I did not know about the temp switch. 👍

The problem I see with this is that it would be an extra step in the REPL.  
To my knowledge there is no temp switch for the julia binary.

The REPL mode is a bit much maybe, but I don’t see a good way to scanning the code without manually adding macros in the REPL. Maybe a lower level intercept would work, but I don’t have experience with [Cassette.jl](https://github.com/JuliaLabs/Cassette.jl) or similar packages.

---

<div class="post-metadata">

**Author:** ![yakir12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yakir12/32/297_2.png) [@yakir12](https://discourse.julialang.org/u/yakir12)\
**Post date:** [April 16, 2021, 8:56am UTC](https://discourse.julialang.org/t/suggestion-for-a-new-package-minimalworkingexamples-jl/59391/7 "2021-04-16T08:56:44Z")

</div>

> [@feanor12](#):
>
> I did not know about the temp switch.

I love that switch, it’s so useful.

I stand corrected though: Couldn’t the activation of the REPL mode be elicited by the user `using MinimalWorkingExamples`? So that would work really well.

Or even something as simple as this:

```julia
module MinimalWorkingExamples

import Pkg
Pkg.activate(temp = true)

for command in readlines()
    if startswith(command, ["using", "import"])
        pkgs = split(command)[2:end]
        Pkg.add(pkgs)
    end
    @eval command # I suspect this is wrong, my meta-foo is worthless
end

end

```

---

<div class="post-metadata">

**Author:** ![feanor12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/feanor12/32/8212_2.png) [@feanor12](https://discourse.julialang.org/u/feanor12)\
**Post date:** [April 16, 2021, 9:08am UTC](https://discourse.julialang.org/t/suggestion-for-a-new-package-minimalworkingexamples-jl/59391/8 "2021-04-16T09:08:58Z")

</div>

One way would be to read in the whole example code at once. I just don’t know how to terminate `readlines`. Maybe an end of file command, but this seems to require additional knowledge of the user.

The other option would be to check if an input is a valid command. For this parsing the input and checking for errors might work. And if it is OK run it.

Still the problem remains on how to exit the input loop automatically.  
If we don’t exit the loop it is almost like a REPL mode 😉

Maybe print a message like: Please press “CTRL+D” if you finished the example code input.

---

<div class="post-metadata">

**Author:** ![ericphanson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ericphanson/32/215186_2.png) [@ericphanson](https://discourse.julialang.org/u/ericphanson)\
**Post date:** [April 16, 2021, 9:18am UTC](https://discourse.julialang.org/t/suggestion-for-a-new-package-minimalworkingexamples-jl/59391/9 "2021-04-16T09:18:17Z")

</div>

There are hooks in Julia 1.5+ to transform code run in the repl: [https://github.com/JuliaLang/julia/issues/37047](https://github.com/JuliaLang/julia/issues/37047). So I don’t think you need a separate repl mode.

---

<div class="post-metadata">

**Author:** ![feanor12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/feanor12/32/8212_2.png) [@feanor12](https://discourse.julialang.org/u/feanor12)\
**Post date:** [April 16, 2021, 9:26am UTC](https://discourse.julialang.org/t/suggestion-for-a-new-package-minimalworkingexamples-jl/59391/10 "2021-04-16T09:26:01Z")

</div>

As it is not documented I guess it is not for public use yet.

---

<div class="post-metadata">

**Author:** ![jzr](https://avatars.discourse-cdn.com/v4/letter/j/eb9ed0/32.png) [@jzr](https://discourse.julialang.org/u/jzr)\
**Post date:** [April 16, 2021, 9:38am UTC](https://discourse.julialang.org/t/suggestion-for-a-new-package-minimalworkingexamples-jl/59391/11 "2021-04-16T09:38:46Z")

</div>

> [@feanor12](#):
>
> The problem I see with this is that it would be an extra step in the REPL.  
> To my knowledge there is no temp switch for the julia binary.

```bash
julia --project=$(mktemp -d)

```

---

<div class="post-metadata">

**Author:** ![feanor12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/feanor12/32/8212_2.png) [@feanor12](https://discourse.julialang.org/u/feanor12)\
**Post date:** [April 16, 2021, 9:41am UTC](https://discourse.julialang.org/t/suggestion-for-a-new-package-minimalworkingexamples-jl/59391/12 "2021-04-16T09:41:29Z")

</div>

This does not work on windows.

```julia
> julia --project=$(mktemp -d)
mktemp : The term 'mktemp' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the
spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:19
+ julia --project=$(mktemp -d)
+ ~~~~~~
    + CategoryInfo : ObjectNotFound: (mktemp:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

```

---

<div class="post-metadata">

**Author:** ![yakir12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yakir12/32/297_2.png) [@yakir12](https://discourse.julialang.org/u/yakir12)\
**Post date:** [April 16, 2021, 10:23am UTC](https://discourse.julialang.org/t/suggestion-for-a-new-package-minimalworkingexamples-jl/59391/13 "2021-04-16T10:23:31Z")

</div>

I mean, there is no real reason to exit that loop, it will just wait for more lines to “eat”. It’s not like the user is going to want to maintain that session open/functional beyond testing that MWE.

---

<div class="post-metadata">

**Author:** ![feanor12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/feanor12/32/8212_2.png) [@feanor12](https://discourse.julialang.org/u/feanor12)\
**Post date:** [April 16, 2021, 10:27am UTC](https://discourse.julialang.org/t/suggestion-for-a-new-package-minimalworkingexamples-jl/59391/14 "2021-04-16T10:27:34Z")

</div>

How do you end a MWE session? CTRL+C ?

Regarding the possible methods I like the REPL hooks introduces by @ericphanson , but without documentation it seems quite troublesome to get it working.

---

<div class="post-metadata">

**Author:** ![yakir12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yakir12/32/297_2.png) [@yakir12](https://discourse.julialang.org/u/yakir12)\
**Post date:** [April 16, 2021, 10:37am UTC](https://discourse.julialang.org/t/suggestion-for-a-new-package-minimalworkingexamples-jl/59391/15 "2021-04-16T10:37:21Z")

</div>

> [@feanor12](#):
>
> How do you end a MWE session? CTRL+C ?

Oh, I was just assuming the user would close the terminal. Like I said, it’s not like they need to use that specific session for anything. So once they’re done playing around they can just close the terminal. But yea, this is just an implementation detail relevant to the `readlines` loop, which might not be ideal.

---

<div class="post-metadata">

**Author:** ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)\
**Post date:** [April 16, 2021, 11:56am UTC](https://discourse.julialang.org/t/suggestion-for-a-new-package-minimalworkingexamples-jl/59391/16 "2021-04-16T11:56:22Z")

</div>

If you want a complete sandbox you should start a new julia process with a new depot ([`JULIA_DEPOT_PATH`](https://docs.julialang.org/en/v1/manual/environment-variables/#JULIA_DEPOT_PATH)), something like:

```julia
julia> withenv("JULIA_DEPOT_PATH" => mktempdir()) do
           run(`$(joinpath(Sys.BINDIR, Base.julia_exename())) -q --startup-file=no`)
       end;

```

or at least a new load path ([`JULIA_LOAD_PATH`](https://docs.julialang.org/en/v1/manual/environment-variables/#JULIA_LOAD_PATH)) (and maybe a new project as well, [`JULIA_PROJECT`](https://docs.julialang.org/en/v1/manual/environment-variables/#JULIA_PROJECT)):

```julia
julia> withenv("JULIA_LOAD_PATH" => mktempdir(), "JULIA_PROJECT" => mktempdir()) do
           run(`$(joinpath(Sys.BINDIR, Base.julia_exename())) -q --startup-file=no`)
       end;

```

---

<div class="post-metadata">

**Author:** ![ericphanson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ericphanson/32/215186_2.png) [@ericphanson](https://discourse.julialang.org/u/ericphanson)\
**Post date:** [April 16, 2021, 12:00pm UTC](https://discourse.julialang.org/t/suggestion-for-a-new-package-minimalworkingexamples-jl/59391/17 "2021-04-16T12:00:34Z")

</div>

Once Pluto’s [package management PR](https://github.com/fonsp/Pluto.jl/pull/844) lands it might be the easiest way to get a self-contained reproducible environment, although a reactive one which might be a bit different than what some users are accustomed to. I think I saw also some code somewhere for storing Project/Manifests in regular Julia scripts too, but I can’t find it now.

---

<div class="post-metadata">

**Author:** ![yakir12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yakir12/32/297_2.png) [@yakir12](https://discourse.julialang.org/u/yakir12)\
**Post date:** [April 16, 2021, 12:29pm UTC](https://discourse.julialang.org/t/suggestion-for-a-new-package-minimalworkingexamples-jl/59391/18 "2021-04-16T12:29:03Z")

</div>

Wow, yea, that would work very well, minus the reactivity. I didn’t check out that PR in detail, but if using Package would add it automatically (ha, I had a similar idea a year ago [Add a addusing convenience function · Issue #1812 · JuliaLang/Pkg.jl · GitHub](https://github.com/JuliaLang/Pkg.jl/issues/1812)) then that would make this a lot easier.

---

<div class="post-metadata">

**Author:** ![ericphanson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ericphanson/32/215186_2.png) [@ericphanson](https://discourse.julialang.org/u/ericphanson)\
**Post date:** [April 16, 2021, 12:47pm UTC](https://discourse.julialang.org/t/suggestion-for-a-new-package-minimalworkingexamples-jl/59391/19 "2021-04-16T12:47:32Z")

</div>

Yeah, as I understand it you just type `using PkgX` and it installs it into a local environment that gets copied into the Pluto notebook file itself (and it doesn’t use packages from the global environment), so then the notebook file itself holds all the information about packages and versions etc. (It literally stores the Project.toml and Manifest.toml as a string inside the notebook! But it handles making use of those automatically, so it looks pretty smooth). My info is based on the cool PlutoCon talk on it, [Built-In Package Management | Fons van der Plas | PlutoCon 2021 - YouTube](https://www.youtube.com/watch?v=hVeW_JjmOKQ).

---

<div class="post-metadata">

**Author:** ![yakir12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yakir12/32/297_2.png) [@yakir12](https://discourse.julialang.org/u/yakir12)\
**Post date:** [April 16, 2021, 12:52pm UTC](https://discourse.julialang.org/t/suggestion-for-a-new-package-minimalworkingexamples-jl/59391/20 "2021-04-16T12:52:12Z")

</div>

Ah nice.  
TBH, I imagined something a lot simpler (and perhaps less robust) that just alleviates the need to

1. activate a temp env
2. add the mentioned packages

The whole point of MWE is that they are as minimal as they can be while still showcasing some point/issue/behavior. So there shouldn’t need to be any compt issues or needs.

[Next page](https://discourse.julialang.org/t/suggestion-for-a-new-package-minimalworkingexamples-jl/59391.md?page=2)
