How to make vscode aware of user modules

May I ask some follow-on questions? I’m having similar problems moving to VSCode from Juno.

  1. Do you keep your package source code in ~/.julia/dev? (Correct me if I’m wrong, but this seems to be the default location for PkgTemplates, and is required for introspection by the VSCode language server.)
  2. If not, how do you synchronize your package source code with .julia/dev? (I’ve read most of the Pkg documentation, but must have missed this part.)
  3. More generally, would you mind sketching out your package development workflow?

Let me know if I ought to be starting a different topic. Thank you very much.

No.

I don’t.

I keep code at an arbitrary location (~/code/julia/Foo for Foo.jl), and activate it if I am working on it.

This also isn’t true. You just need to point VSCode to the correct environment via the env selector in the lower left (modulo some bugs in our env handling that should be fixed in the next minor release).

2 Likes

I’m unable to get autocompletion to work in VSCode for user-defined modules except for those present in .julia/dev as packages. Is this user-error, or one of the bugs to which you alluded?

3 Likes

Can someone post a step-by-step guide to creating a project where autocomplete works across different files in that project?

4 Likes

Well, for me it works if I have one file in which I define a module, lets call it Utils.jl

module Utils
export my_test

function my_test()
    println("my_test")
end

end

and a main program that includes this module in the same directory:

include("./Utils.jl")
using .Utils

my_test()

If I now right click on my_test() I can select “Go to Definition” and it works.

The main program and the module(s) that you include do not have to be in the same directory,
but if they are not you have to adapt the relative path in the include statement.

Typing: my<TAB> should result in auto completion to my_test.

Does that work for you?

4 Likes

This works for me, thank you!

In contrast,

push!(LOAD_PATH, @__DIR__)
using Utils

my_test()

, which is what I was using before, runs but does not autocomplete.

That is a known issue which might or might not be fixed at some point in time:

1 Like

Yeah, honestly, I’d recommend setting up a local environment instead of any LOAD_PATH shenanigans.

But that does not solve this issue. It is not even a work around.

Why not? If you’re using LOAD_PATH, it’s because you want to load a package somewhere. Instead just put that relationship into a Manifest and you’re good to go, no?

Could you say more about this workflow or post a link that explains it? I have a Project.toml and Manifest.toml that contain external libraries, which were auto-generated via Pkg.add, how do I add my own files to it?

If you’re using LOAD_PATH , it’s because you want to load a package somewhere.

I was using LOAD_PATH in the past not to load a package, but a module, usually in a file in the same directory.

A local environment does only help if you are a package developer. But many people just write projects, not packages. In that case my work around helps.

2 Likes

Sure, but you weren’t doing anything with LOAD_PATH :slight_smile: That’s a perfectly fine solution for referencing local files.

Take a look at the Pkg docs. What I did to generate the above example was basically

$ mkdir VSCode-Test
$ cd VSCode-Test
$ mkdir Foo
$ echo 'name = "Foo"\nuuid = "a33f4f40-c364-4a1b-9be3-145a5eb10e1c"' > ./Foo/Project.toml
$ mkdir Foo/src
$ echo 'module Foo\nf(x)=x\nend' > ./src/Foo.jl
$ code .
$ julia --project=.
(VSCode-Test) pkg>dev ./Foo

and then right click on the Project.toml in VSCode and select Activate This Environment.

4 Likes

I created a package, which I named Experiments and I dump all my scripts in src. I also set up a startup.jl file inside .doc/config/ with something like

cd("path/to/experiments/")  # set up path to package
import Pkg
Pkg.activate(".")

This is an all-purpose environment. I don’t have to have a push command or similar. This seems okay for short scripts and quick debugging of loops. Once in a while I delete everything below the # This file is machine-generated - editing it directly is not advised header in the Manifest.toml file and re-install packages as I need them.

Anything seriously wrong with this approach?

Sorry to dig this up again but I cannot get user defined modules to work with code completion. Reading through these post it seems if you have generated a package and added it to dev it should work.

[dustin@Odin-pc ~]$ julia
               _
   _       _ _(_)_     |  Documentation: https://docs.julialang.org
  (_)     | (_) (_)    |
   _ _   _| |_  __ _   |  Type "?" for help, "]?" for Pkg help.
  | | | | | | |/ _` |  |
  | | |_| | | | (_| |  |  Version 1.6.1 (2021-04-23)
 _/ |\__'_|_|_|\__'_|  |  
|__/                   |

(@v1.6) pkg> status
      Status `~/.julia/environments/v1.6/Project.toml`
  [023a3684] GenieVue v0.1.0 `~/Git/Projects/genieplayground/GenieVue`
  [6696d0b5] RPGCombat v0.1.0 `~/Git/Projects/Katas/RPGCombat`

(@v1.6) pkg>

My Project.toml file looks like

name = "RPGCombat"
uuid = "6696d0b5-b9c1-4907-9ab0-32591776c20d"
authors = "Dustin"
version = "0.1.0"

[deps]
Revise = "295af30f-e4ad-537b-8983-00126c2a3abe"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

My env: is RPGcombat in VScode
I am setting something up incorrectly?

1 Like

I have custom modules I want to use, so I set julia.environmentPath in vscode, but this flag does not seem to be doing anything at all. I have also tried:
using Pkg and Pkg.activate(/path/to/whatever) in the julia file but this is an aweful solution polluting the julia file. On top of that, it does not seem to be loading modules from the environment of my choosing and seems to load modules from ~/.julia even when environment is selected differently.

using somePackage
pathof(somePackage)

This always returns ~/.julia/path/to/some/package

The only way I have found it working was when I export JULIA_DEPOT_PATH and JULIA_PROJECT and run julia from the terminal.

I can’t seem to set the custom julia environment (without polluting julia sorce file) and get packages from that environment correctly in vscode. I’d appreciate it if anyone could share how to get these two environment variables to work in vscode so that the julia environment is consistent with packages defined in that environment.

I have tried adding these environment variables in launch.json file, but vscode does not seem to care

Thanks!

I am not using the work around I suggested earlier in this thread any longer, because it makes pre-compilation impossible.

It is pretty clear that the current vscode for Julia team will not fix https://github.com/julia-vscode/julia-vscode/issues/307 . So the only way to go forward is to create a fork, implement a good linter and fix this issue. A first step could be to define the requirements and interface of a language server/ linter, that finds modules in the load path.

1 Like

I have followed Julia community for 2 months. There are so many people smart at compilers, but making unbelievable common sense decisions. I notice you said the code was very hard to read. what would it take to just read JULIA_LOAD_PATH one time on startup?

This solved it for me. Now VScode can Go to Definition of the symbols in my user-defined packages.

For clarity.
I am using DrWatson. I create a new environment by using: DrWatson.initialize_project("Foo")

Once in the new environment (which looks pretty much like a package!) I move in the src folder and create a new package with ]generate myFooPackage.

At this point I activate the environment (with shell in: /projects/Foo/).

]activate .
]dev ./src/myFooPackage

Now to have the symbols in myFooPackage available I have to go to Code options (Maiusc-Command-P) and click on Julia: Change Current Environment. There I select Foo and it works.