COMMUNITY

VSCode Haxe: how to use sources from multiple directories?

Heya’ :). Would greatly appreciate some help setting up what I think is a very simple project structure for most seasoned Haxe users of VS Code. By “setting up” I mean having code navigation & compilation based on dependencies.

I have a multi-directory Haxe project. I used to have it in IntelliJ where one would add “Modules” to a project. In VS Code I understand I should use the Workspace feature. However, I don’t understand how to set the references between directories and their dependency on one another. Somebody told me I should use “haxelib dev”, but I didn’t exactly understand how that works. I don’t see how that interacts with the language server in VS Code.

The structure of my project is as follows:

  • JavaScript client
  • Haxe library 1 that is referred to by the JavaScript client
  • Haxe library 2 that is referred to by Haxe library 1.

Therefore, the JS client depends on Haxe Lib 1, which in turns depends on Haxe Lib 2. The goal is to be able to “go to definition” between all classes involved. And that everything gets compiled in the JavaScript client.

This is how I’m compiling it (from tasks.json)

			"label": "JS Client (debug)",
			"type": "shell",
			"command": "npx",
			"args": [
				"haxe",
				"build.hxml",
				"-js",
				"export/run.js"
			],

I think your first goal should be to get your project to compile with your build.hxml (Unrelated to vscode)

If your libraries are just a collection of source files that you want included then you should simply add one -p /path/to/library/source/root per library path.

An example build.hxml could be:

-p jsclient/src
-p library1/src
-p library2/src
-js export/run.js
-main path.to.jsclient.Main

If your libraries are actually haxelib libraries then you should instead reference library1 by name using -L library1 and libary1 should have library2 as its dependency in its haxelib.json file.

In this case the haxelib dev command is there to tell haxe to use some path from your computer as the source for a given library instead of looking for it in the haxelib path.

1 Like

Hey @basro :). Thanks for answering! Maybe you and others can help me wrap my head around how Haxe expects me to bundle things. I’m sure it’s easy but I’m simply missing some vital pieces it seems. IntelliJ Haxe Plugin was doing a lot of this for me, and now without it I’m out in the cold. Also, I just cannot find enough examples about how to bundle complex projects in Haxe! Nor enough documentation about how each step in HXML works. Even less about how to do it with VS Code! :slight_smile:

That’s why I’m creating a bunch of repos in GitHub where I plan to keep all this very thoroughly documented & explained, so that other people can benefit. But I’m stuck very early. And probably laughably for most of you here :).

So, now, I can’t even make a class compile even though I specified the paths to its dependencies in HXML. Maybe it has to do with how npx invokes Haxe? I see that in tasks.json (as per my OP), build.hxml is specified as an argument, but then also -js and export/run.js are. So I guess that’s a way to overwrite/add arguments alongside those from build.hxml?

Here is the GitHub repo where I’m trying to get these things to build together. The idea with this repo is to be an example for using multiple targets, language features and workflows IN A SINGLE REPO.

Hopefully one day this can return to @Gama11 somehow, as he’s made the original “HaxeRepro” and I think it would be useful for that repo to show off more stuff, since it’s already rather known in the community :slight_smile:

I’ve made a pull request with changes that should fix the build errors.

Make sure to select JavaScript target in VSCode since you are using the js only libs in your code:
image

However, I don’t think HaxeRepro is intended to be used as a scaffold for projects. The description says it’s to be used for reproducing haxe compiler issues.

My recommendation to you is to start simpler because HaxeRepro is going to make it harder to understand the basics.

I’ve made a repo with a simpler project layout here: https://github.com/basro/kyliathy-simple-project

You can build this project by running haxe build.hxml (Asuming you have haxe installed, I don’t normally use npm haxe but tell me if you want help with that).

If you open this folder with VSCode the Haxe VSCode extension should detect all the .hxml files in the root folder, you can select one of them as the active haxe configuration. It will also automatically create an implicit build task for every .hxml so you can easily build from VSCode without using the terminal.

1 Like

Forgot to answer your questions about hxml:

You can think of hxml files as bundled compiler arguments inside a file.
Instead of haxe -p Lib1 -p Lib2 -p src -js bin/main.js --main Main you can write haxe build-js.hxml and put all those arguments inside the .hxml file separated by newlines.

1 Like

Thank you so much, Mario! :). This was a very kind help and I’ll put it to good use. As it often happens, it was carelessness that caused my issue (improper package name of all things).

Yes, you’re right, but HaxeRepro was the best thing I had when I wanted to see how things work together, and it did teach me some things. But once I understand this more, I’ll remove some of the over-complicated stuff.

I probably should create a number of very simple projects in a master repository. Just like the one you kindly created for me :slight_smile: .

Thanks a ton! I’m on my way to the next challenge thanks to you!

Glad to be of help!

1 Like