Imports: fully qualified or relative paths?

I'm curious what everyone thinks about this. Programming languages generally have a way to import code from another file/module. Some languages have some kind of abstract namespacing scheme that maps to a location in the file system (e.g., Java), whereas other languages allow you to provide a literal path to the file/module (e.g., JavaScript), though sometimes the file extension is omitted. Languages in the former category typically require fully qualified namespaces, whereas languages in the latter category typically allow for relative paths. And of course some languages deviate from this and/or implement some kind of hybrid system.

Another consideration is that some languages don't have a notion of "project" or "root" directory, so fully qualified paths don't make sense. Those languages tend to use relative paths.

What are the tradeoffs here? What are you doing for your language project(s)?

Posted Mar 17, 2021, 8:37 PM UTC

1410
jesseschalken•

With JavaScript, imports look like either import "../../blah/foo" or import "mylib/blah/foo" depending on whether blah/foo is in a different package or not.

I prefer fully qualified paths so that imports dont have to change when code is moved to or from a different package.

Additionally, when a file is moved with absolute imports, the imports inside it are still valid and only the importers have to change. With relative imports, the imports inside the file also have to change.

12
poiu-•

I'd use both types: relative if you're writing different modules of a single program, where it's unlikely that files ever get moved around much. More obvious that it's from your program.

Absolute if you're using stuff that might be replaced with external libs.

1
AthasFuthark•

I like relative paths. I wrote at detail about my motivation once, and I don't claim the choice is best for all languages.

However, the very nice property is that every source file can be seen as the starting point of the program, with respect to type checking and analysis. This means that editor tooling (or other tooling) doesn't have to be told about a "project" or "root" directory so that imports can be resolved correctly. If the program type checks as a whole, then every file will also type check when viewed as the root of a program. This might not matter for languages that can afford to implement complex tooling, but it has proven very useful for writing quick tools that just work (e.g. futhark-mode is one of the few Emacs language modes where go-to-definition works out of the box with zero setup).

9
[deleted]•

I don't like literal paths. The algorithm for locating such a file, checking for duplicates etc, can be complex. And the file name might be unsuitable for use an identifier for the module name.

When I say:

import abc

then 'abc' is a module name, not a file name. A separate mechanism is used to control the places that are searched for the source file corresponding to that module.

It also means that if 'abc' is imported twice, then it is very easy to tell. But if an arbitrary path is allowed, it could be two different modules of the same name but different locations, or two different ways of referring to the same file.

4
xactacoXyl•

Just going to follow up to say that perhaps the worst way that modern languages do modules (IMO) is to use a filename and path as a basis for the module name with some modifications, but to make all imports use identifiers instead of strings. Bonus points if the search path is weird and inflexible, or if it's hard to import something from a parent directory.

I'm looking at you, Python.

1
simon_o•
  1. OK: Fully qualified.
  2. Bad: Relative.
  3. Really bad: Both.

(See Rust.)

2
theangryepicbananaStar•

In Star, you can have all of the above.

Relative files are automatically included when in the same directory (and if there's a directory with the same name as the file/module, then everything in that too) as the current file/module. Outer modules may also be referred to without importing them, but their contents must be referred to with a type (dot) path (so at this point, it's similar to java/haxe behavior).

Of course, absolute paths are always possible as they're very similar to relative paths. In the case you shadow a module name, you can prefix the path with a _. (where _ is the the "name") to refer to the previous module defined with that name (again, relative to the current file/module).

This effectively makes name resolution act the same as a file system, where modules are files/directories, their contents are in the directory, and directory. Relative paths can be referred to normally, outer and nested paths can be referred to with ../, and absolute paths are referred to using a full path.

It's a balance that I really like because it solves a lot of the problems/annoyances/etc of existing name resolution methods.

1
L8_4_Dinner•

As you said, there seem to be two general categories of approaches: File based, and global namespace based.

What are you doing for your language project(s)?

  • Module-centric, with modules following the domain naming system (e.g. "libraryname.organization.com")
  • Each module acting as its own private and self-contained namespace with a root (the module itself, example) and a recursive name tree (packages, classes, and so-on) within it.
  • Modules (specified using the domain naming system approach mentioned above) imported as local package names (a local alias within the importing module's namespace, example), thus "mounting" one module's entire namespace within another module's namespace.
  • All access within source code is then with respect to the local module's namespace, and can be relative or absolute, and supports aliases. Also, imports are scoped, and unlike most language constructs, avoid forward references to other subsequently imported names.
  • Separately, and unrelated, the compilation allows for resources to be loaded at compile-time using file system pathing, since developers often have resource files that will be used at runtime, and we allow those to be bound into the compiled result by inclusion (example).
1
friedbrice•

Why even make it possible to write source code that's sensitive to file paths? That just seems silly to me. The compiler should have a notion of modules in mind so that source code doesn't break if you move some files around.

1
transistor_fet•

It depends on the language and how the module system or lack thereof can be accessed from within the language. For a language like Python, the module has an in-memory representation that can be accessed dynamically from within the language itself, so by forcing the import names to be valid Python identifiers, and mapping them to files, it ensures everything defined in a module's file will be available through a normal python reference. This type of module and import system makes sense for a heavy dynamic language like Python.

For other languages, the overhead of such a module system might not be desirable, and so direct filenames makes more sense. That said, there are a fair number of languages that use direct filename that also use text concatenation when including a file, which is more simpler, but comes with its own host of problems when files are included multiple times (C, PHP, etc).

Having a more explicit module system makes it easier to treat modules as a more abstract concept, with their own behaviours and ways of interacting with them, but that usually means there needs to be a way to refer to modules within the language, so again you need a language compatible reference that maps to a file somehow, which leads you towards non-filename style imports. Because of the advantages of more abstracted module concepts, there are a fair number of modern languages that opt for abstracted imports (Rust, Java, etc)

1