Search code examples
packageddub

Having trouble with package.d


I am having trouble with D's package.d feature. I have my package.d file:

module dew;

public
import dew.util;

I then have util.d:

module dew.util;

struct Size
{
    int width;
    int height;
}

When I try to use it in another project, it gives me this error:the error I know that this should work, because projects on GitHub use it, specifically bindbc-sdl.


Solution

  • Your code is correct. By the error message I'm assuming your project layout with DUB probably looks something like this:

    • dew/
      • dub.sdl/dub.json
      • source/
        • package.d
        • util.d

    However this would be incorrect. By default DUB only uses import folders instead of specifying all input source files, so the compiler attempts to discover the files from the import paths in the filesystem. The compiler dumped the import paths it searches in in your error screenshot.

    The rough compiler equivalent to what it's doing right now is (see dub -v)

    dmd source/app.d -Isource -I../dew/source
    

    You need to imagine that the path whatever is written in there is invisible to the compiler, so the dew/source/ path is just some opaque string the compiler doesn't interpret. Now all that the compiler sees is a package.d and util.d in its search paths. However for imports from folders to function, they need to correspond to the filesystem layout, i.e. you need to have a folder named dew where your files are stored in.

    So a module named dew.util would correspond to dew/util.d

    And your package dew would correspond to dew/package.d

    So for your dub project that means essentially that you need to move all your source files into

    • dew/
      • dub.sdl/dub.json
      • source/
        • dew/
          • package.d
          • util.d

    Alternatively it would be possible to manually specify all files one-by-one where the compiler can look them up because of the module declarations at the top of the file, however you lose the convenience of module names being mapped to filesystem paths, which might be something other community-made D source tools and IDEs are expecting. On the command line that would be equivalent to

    dmd source/app.d ../dew/source/dew/package.d ../dew/source/dew/util.d