Import statement support for paths relative to the project root folder

Unless I'm mistaken, all paths given to the import statement are relative to the file's location.

I'm putting together a little dependency management system for Playdate and it would make cross-dependencies a lot easier if we could also use paths relative to the project root folder. Syntax could be simply something like:

import '/source/test.lua'

which would convert to <Path to the Project Folder>/source/test.lua.

Another way to solve this would be to add include paths support for pdc but it seems like some environments may not make it easy to provide these (via env variable or command line arguments) so this seems like a much easier way to provide the functionality.

1 Like

You can use

import '../shared/whatever.lua'

(for example) to import from outside of the Source folder. Does that work for you?

It works (that's what I'm using for now) but has two issues in uses cases like mine:

  • It makes both the location of the importer and importee non-relocatable. You change either of those and the import breaks. Allowing for absolute paths (from the project dir) as an option would make at least the importer to be relocatable within the project folder. Similar to how C include works with the preprocessor and include paths, but in a much simpler way to implement.
  • My current tests seem to indicate that pdc concatenates these relative paths ad-infinitum. I've seen any error message looking like ../../Dir1/Folder/../OtherFolder/../Test. Since there seems to be a 127 character limit this is not going to work in all uses cases.

Adding something like #import '/Dir/MyLua.lua' seems easy to parse in pdc, keeps the paths shorter and makes the importer easily relocatable.

Sidenote: The reason relocatable matters in my case is that a third-party library may be used in one place by the developer of a project and in another place when installed in an end-user's project.

Sorry for the novel-size reply 🫢

These are great points. We'll consider it, thanks!

That's all I can ask for :hugs:

Thanks for all the hard work!

2 Likes

Yes please! This also bothers me when writing libraries