"Type not found" after turning class into a haxelib

2.5k Views Asked by At

TL;DR: Outside my project directory, installed with haxelib dev, and included in Project.xml like any other library, my library isn't seen by the Haxe compiler. Inside my project directory it works fine. Obviously other libraries don't have to be inside my project directory to avoid compiler errors, so what am I missing here?

I'm trying to release some code as a haxelib, by following this page.

I originally had my package namespace as flixel.addons. The code sat as part of a different project, under its source/flixel/addons subdirectory. Before I started, the project it was part of compiled fine.

I decided it would be better not to invade someone else's namespace when releasing it, so I changed the namespace to mynamespace on the package line of my class file. I looked at another haxelib for ideas, and saw that the library name can be different from the class path, which probably should be the same as the package namespace. So I moved the code out of my project, into a new directory elsewhere:

mylibraryname
\-Readme.md
\-haxelib.json
\-mynamespace
  \-myclass.hx

Per the instructions, I ran haxelib dev with the path to mylibraryname.

In my project, I added <haxelib name="mylibraryname" /> to Project.xml. Where I used it, I changed the import lines from flixel.addons.myclass to mynamespace.myclass.

Now, trying to compile my project (in FlashDevelop, with Haxe 3.2.1) gives the error, Type not found: mynamespace.myclass on the import line where it's used. The irony is that FlashDevelop added that line automatically with Ctrl+Shift+1 pressed with the cursor on myclass where it's used. Further, I can press F4 on the red-underlined part of the import line, and it opens myclass.hx from its new location just fine.

That file contains:

package mynamespace;

And my project files that use it contain:

import mynamespace.myclass;

Meanwhile, the haxelib.json file has:

{
  "name": "mylibraryname",
  "url" : "...",
  "license": "MIT",
  "tags": ["haxe"],
  "description": "...",
  "version": "0.0.1-alpha",
  "classPath": "mynamespace/",
  "releasenote": "...",
  "contributors": ["me"],
  "dependencies": {
    ...
  }
}

Things I tried:

  1. Even though I believed it should be working at this point, I tried the next step of creating a zip file and using haxelib install on it. No change.

  2. I also happened to still have a directory mynamespace in my project, but I didn't really need anything in it anymore, so I deleted it. No change.

  3. Just to be sure there wasn't some typo somewhere, I plopped a copy of the mylibraryname directory (which had installed correctly in ...\haxe\lib\mylibraryname\0.0.1-alpha and made a .current file with 0.0.1-alpha in it, BTW) back into my project. It compiled fine.

  4. Even though it's not mentioned in the docs, I saw another library include a file haxelib.xml alongside haxelib.json, containing this. With or without this file present, no change. (Not even to code completion, which works fine listing my class' unique and inherited variables, but generates the exact same Type not found error in the Output pane as I get during a compile.)

  5. haxelib selfupdate. Was already up-to-date. No change.

  6. Windows restart! Why not? No change.

How can I get my project to compile? Why doesn't Haxe see it when FlashDevelop and haxelib do, and when my lib's directory structure and haxelib.json are modeled after working third-party haxelibs and the docs?

Or am I misunderstanding something, and you have to test haxelibs in the context of a project, until they're released for real? If this is the case, I'm not sure what exactly one is supposed to be doing with the haxelib dev and haxelib install mylib.zip steps.

3

There are 3 best solutions below

4
On BEST ANSWER

The problem should be the classPath directive in haxelib.json.

Its value should be the relative search path for modules in the root/base package, not the path to your package.

Try changing it to "" or omitting that directive altogether.

2
On

Maybe you can try doing this:

  1. Create a folder with the name of your library in HaxeToolkit\haxe\lib (I'm gonna call it "example-lib", you can call it whatever you want).

  2. Create another folder inside the example-lib folder with the title of the version name (e.g: 0,0,1 (note the commas)) and put your classes in it.

  3. Create a file called ".current" inside example-lib folder (if you can't, try to copy one from another haxelib folder and paste into yours) and inside it, enter the version name (e.g: 0.0.1).

  4. In your Project.xml, add

    <haxelib name="example-lib" />

2
On

The structure of your library and setting it up with haxelib dev seems sound, and I'm able to compile a project with a custom library created like that.

The only thing I can think of is that the package inside myclass.hx is declared incorrectly (maybe you forgot to adjust it when moving the file?).

Double-check that the first line in that file looks like this:

package mynamespace;