# Help question with running Stan.jl in VSCode on Windows

**URL:** <https://discourse.julialang.org/t/help-question-with-running-stan-jl-in-vscode-on-windows/57571>\
**Category:** General Usage\
**Tags:** question\
**Created:** [March 19, 2021, 10:08pm UTC](https://discourse.julialang.org/t/help-question-with-running-stan-jl-in-vscode-on-windows/57571 "2021-03-19T22:08:28Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![goedman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goedman/32/217_2.png) [@goedman](https://discourse.julialang.org/u/goedman)\
**Post date:** [March 19, 2021, 10:08pm UTC](https://discourse.julialang.org/t/help-question-with-running-stan-jl-in-vscode-on-windows/57571/1 "2021-03-19T22:08:28Z")

</div>

Hi,

When I run below script in VSCode on Windows:

```julia
using CmdStan, Statistics
bm = "
  data { 
    int<lower=1> N; 
    int<lower=0,upper=1> y[N];
  } 
  parameters {
    real<lower=0,upper=1> theta;
  } 
  model {
    theta ~ beta(1,1);
    y ~ bernoulli(theta);
  }
";
data = Dict("N" => 10, "y" => [0, 1, 0, 1, 0, 0, 0, 0, 0, 1])
sm = Stanmodel(name="bernoulli", model=bm, printsummary=false)
sm |> display
rc, samples, cnames = stan(sm, data; summary=false, CmdStanDir=CMDSTAN_HOME);
nt = CmdStan.convert_a3d(samples, cnames, Val(:namedtuple))
mean(nt.theta, dims=1) |> display

```

then, after the call to `stan()`, screen make-up characters are showing up, e.g. in [https://user-images.githubusercontent.com/29783567/111507734-f8ea4680-8718-11eb-9a43-33a344851bfb.png](https://user-images.githubusercontent.com/29783567/111507734-f8ea4680-8718-11eb-9a43-33a344851bfb.png) .

A typical sequence would be: “\<-[?2004l\<-0m\<-0m(tree depth\_\_ = [1.0 …” where the last part is correct and is the beginning of the `nt` NamedTuple.

I suggested to switch to Powershell (which has helped in the past, but in this case it doesn’t seem to).

Is this a VSCode issue? Is there another development setup with a separate Powershell that maybe works?

I don’t have access to a Windows machine nor do I normally work in VSCode but if I bring it up on macOS it works fine.

Thanks for your help!

---

<div class="post-metadata">

**Author:** ![davidanthoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/davidanthoff/32/223493_2.png) [@davidanthoff](https://discourse.julialang.org/u/davidanthoff)\
**Post date:** [March 19, 2021, 11:25pm UTC](https://discourse.julialang.org/t/help-question-with-running-stan-jl-in-vscode-on-windows/57571/2 "2021-03-19T23:25:22Z")

</div>

I think what is happening there is that some Julia code is spawning some command line executable that is changing the console mode of the (shared) console, and in particular is switching off support for escape sequence support in the console. But Julia is still sending control sequences to the console, and then you get that kind of corrupted output.

I think the proper fix for this would probably be that Julia stops spawning child process in a way that gives them full access to the main console, because at the end of the day any of these child processes can always change something about the console that will break output. Instead, Julia probably should spawn child processes connected to a pseudo console instead, and then make sure output that the child process displays on the pseudo console is displayed in the main console. That way Julia could probably guarantee that a child process can’t cause this kind of problem.

Not sure whether you have control over the binary that is spawned, but you could also try to prevent it from changing the console mode…

---

<div class="post-metadata">

**Author:** ![goedman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goedman/32/217_2.png) [@goedman](https://discourse.julialang.org/u/goedman)\
**Post date:** [March 19, 2021, 11:48pm UTC](https://discourse.julialang.org/t/help-question-with-running-stan-jl-in-vscode-on-windows/57571/3 "2021-03-19T23:48:58Z")

</div>

Thank you David! Very insightful, and hints on ways I could try to fix this, e.g. direct all output from the spawned ‘cmdstan’ executable to stderr or so as I don’t need it.

---

<div class="post-metadata">

**Author:** ![davidanthoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/davidanthoff/32/223493_2.png) [@davidanthoff](https://discourse.julialang.org/u/davidanthoff)\
**Post date:** [March 20, 2021, 12:23am UTC](https://discourse.julialang.org/t/help-question-with-running-stan-jl-in-vscode-on-windows/57571/4 "2021-03-20T00:23:33Z")

</div>

Yeah, that might work, but not sure… Just a bit more detail: I think the child process is almost certainly calling `SetConsoleMode` ([SetConsoleMode function - Windows Console | Microsoft Docs](https://docs.microsoft.com/en-us/windows/console/setconsolemode)) without the `ENABLE_VIRTUAL_TERMINAL_PROCESSING` flag set, and that switches the entire console from processing control sequences to just displaying them as if they were text input…

---

<div class="post-metadata">

**Author:** ![goedman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goedman/32/217_2.png) [@goedman](https://discourse.julialang.org/u/goedman)\
**Post date:** [March 20, 2021, 1:51am UTC](https://discourse.julialang.org/t/help-question-with-running-stan-jl-in-vscode-on-windows/57571/5 "2021-03-20T01:51:49Z")

</div>

I should be able to find that in the sources and I’ll check back with the Stan team. Thanks again.

---

<div class="post-metadata">

**Author:** ![goedman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goedman/32/217_2.png) [@goedman](https://discourse.julialang.org/u/goedman)\
**Post date:** [March 20, 2021, 2:42pm UTC](https://discourse.julialang.org/t/help-question-with-running-stan-jl-in-vscode-on-windows/57571/6 "2021-03-20T14:42:36Z")

</div>

About a week ago I helped out someone else and he [confirmed](https://github.com/StanJulia/Stan.jl/issues/96#issuecomment-798835589) after some back and forth trying:

“I now confirmed the walkthroughs for both CmdStan and StanSamples work if I start julia from Command Prompt or from Powershell, but fail if I start julia from git bash.”

I wonder if somehow inside VSCode we are still starting Julia REPL in git bash as [in this case](https://github.com/StanJulia/CmdStan.jl/issues/112#issuecomment-801262605) it doesn’t seem to work.

Did a quick check in the cmdstan and stansummary C++ sources but no visible call to SetConsoleMode so need to dig deeper into that possibility.

---

<div class="post-metadata">

**Author:** ![pfitzseb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pfitzseb/32/45566_2.png) [@pfitzseb](https://discourse.julialang.org/u/pfitzseb)\
**Post date:** [March 20, 2021, 2:47pm UTC](https://discourse.julialang.org/t/help-question-with-running-stan-jl-in-vscode-on-windows/57571/7 "2021-03-20T14:47:53Z")

</div>

The integrated “Julia REPL” doesn’t go through a shell at all, we just start Julia directly.

---

<div class="post-metadata">

**Author:** ![goedman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goedman/32/217_2.png) [@goedman](https://discourse.julialang.org/u/goedman)\
**Post date:** [March 20, 2021, 4:13pm UTC](https://discourse.julialang.org/t/help-question-with-running-stan-jl-in-vscode-on-windows/57571/8 "2021-03-20T16:13:58Z")

</div>

Thank you.

`ENV` on my test setup for VSCode shows:

```julia
julia> ENV
Base.EnvDict with 52 entries:
  "USER" => "rob"
  "__CFBundleIdentifier" => "com.microsoft.VSCode"
  "COMMAND_MODE" => "unix2003"
  "DISPLAY" => "/private/tmp/com.apple.launchd.dxa1j0rGUj/org.macosforge.xquartz:0"
  "LOGNAME" => "rob"
  "PATH" => "/Users/rob/.julia/packages/Homebrew/l8kUw/deps/usr/opt/gettext/bin:/Users/rob/Library/Python/3.6/bin:/usr/local/opt/bison/bin:/Applications/MacVim.app/Contents/bin…
  "SSH_AUTH_SOCK" => "/private/tmp/com.apple.launchd.X5Sa1Alq4C/Listeners"
  "SHELL" => "/bin/zsh"
  "HOME" => "/Users/rob"
  "__CF_USER_TEXT_ENCODING" => "0x1F5:0:0"
  "TMPDIR" => "/var/folders/l7/pr04h0650q5dvqttnvs8s2c00000gn/T/"
  "XPC_SERVICE_NAME" => "application.com.microsoft.VSCode.78777835.78777841"
  "JULIA_PKG_SERVER" => "https://us-east.pkg.julialang.org"
  "JULIA_PKG3_PRECOMPILE" => "true"
  "JULIA_MPI_PATH" => "/usr/local/Cellar/open-mpi/4.0.2"
  "JULIA_SPECIALFUNCTIONS_BUILD_SOURCE" => "true"
  "JULIA_SVG_BROWSER" => "Google Chrome.app"
  "JAGS_HOME" => "/usr/local/bin/jags"

```

I asked Justin what [ENV](https://github.com/StanJulia/CmdStan.jl/issues/112#issuecomment-802824484) looked like on his setup and it shows:

```julia
...
  "JULIA_EDITOR" => "\"C:\\Users\\Justin\\AppData\\L… "JULIA_NUM_THREADS" => ""
  "TERM_PROGRAM" => "vscode"
  "TERM_PROGRAM_VERSION" => "1.54.3"
  "LANG" => "en_US.UTF-8"
  "COLORTERM" => "truecolor"
...

```

and, as you point out, no shell. But, unix thinking probably, VSCode on Windows must use some kind of environment to spawn the Julia REPL which works fine until I call `stan()` and immediately after that call the terminal doesn’t handle the the escape characters anymore.

As mentioned above, I checked the cmdstan and stansummary sources, to no avail, my next step is to see what’s included in the to C++ compiled/translated Stan Language program.

---

<div class="post-metadata">

**Author:** ![devel-chm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/devel-chm/32/3572_2.png) [@devel-chm](https://discourse.julialang.org/u/devel-chm)\
**Post date:** [March 20, 2021, 4:30pm UTC](https://discourse.julialang.org/t/help-question-with-running-stan-jl-in-vscode-on-windows/57571/9 "2021-03-20T16:30:45Z")

</div>

I believe this may be the same or similar to this issue reported by me:

[https://discourse.julialang.org/t/reset-repl-terminal-mode-to-working-default-settings/51892](https://discourse.julialang.org/t/reset-repl-terminal-mode-to-working-default-settings/51892)

I wanted to use the REPL shell mode to call cygwin programs:

```julia
shell> ls
20201105_12 20201106_11 Manifest.toml Project.toml io_caen movers test_proj tutorial

←[7C←[32m←[1mjulia> ←[0m←[0m

```

but the teminal mode in the julia repl gets screwed up. A sort of work around is to use the windows cmd.exe to indirectly call the desired program like this:

```julia
shell> cmd /c ls
20201105_12 20201106_11 Manifest.toml Project.toml io_caen movers test_proj tutorial

julia>

```

Note that the line mode at the Julia REPL is now correct. After seeing your issue, I think maybe the problem is that the julia mode in the REPL is letting terminal settings from shell (and presumably help) mode leak in. Would adding a reset to the julia the current julia terminal mode fix the problem?

---

<div class="post-metadata">

**Author:** ![goedman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goedman/32/217_2.png) [@goedman](https://discourse.julialang.org/u/goedman)\
**Post date:** [March 20, 2021, 4:55pm UTC](https://discourse.julialang.org/t/help-question-with-running-stan-jl-in-vscode-on-windows/57571/10 "2021-03-20T16:55:22Z")

</div>

Thanks a lot! I should be able to do that as well!

---

<div class="post-metadata">

**Author:** ![goedman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goedman/32/217_2.png) [@goedman](https://discourse.julialang.org/u/goedman)\
**Post date:** [March 21, 2021, 3:17pm UTC](https://discourse.julialang.org/t/help-question-with-running-stan-jl-in-vscode-on-windows/57571/11 "2021-03-21T15:17:38Z")

</div>

Well, not quite a solution, but apparently Stan.jl (based on StanSample.jl, StanOptimize.jl, etc.) works fine in VSCode on Windows.

I need to check where/why CmdStan.jl is not handling above problem correctly. The `cmd /c` part had been added some time ago but maybe there is a path where this is not done right.

CmdStan.jl is in maintenance mode, so it’s not a top priority on my list of things to fix in the combination CmdStan + VSCode + Julia REPL given that Stan.jl is working in that setting.

As always, from all issues you take something away. In this case I took another look at VSCode and, compared to a year ago when I wanted to step away from TextMate and in the end opted for Sublime Text, this time around liked it quite a bit.

Thanks for the great help on this issue!
