nim-lang / RFCs

A repository for your Nim proposals.
135 stars 26 forks source link

RFC: `--path:prefix:path` to resolve `import prefix/suffix` #291

Open timotheecour opened 3 years ago

timotheecour commented 3 years ago

this RFC improves on --path:path as follows:

proposal

notes (consequences of above rules)

note that --path:prefix:path operates as a key-value table, unlike --path:path, which guarantees that only 1 dir is searched for for a given prefix and avoids inconsistencies. eg:

nim r --path:compiler:/pathto/Nim1/compiler --path:compiler:/pathto/Nim2/compiler main

# main.nim
import compiler/ast # => resolves to /pathto/Nim2/compiler/ast
import compiler/newmodule # module not found error even if /pathto/Nim1/compiler/newmodule.nim exists

note that you can override a child prefix, which is an important feature (eg for debugging or customization) eg:

nim r --path:compiler:/pathto/Nim1/compiler --path:compiler/ast:/pathto/Nim1/compiler/ast_modif.nim main

import compiler/ast # => resolves to /pathto/Nim1/compiler/ast_modif.nim

It works for packages too: nim r --path:compiler:/pathto/Nim1/compiler --path:compiler/plugins:/pathto/Nim_temp/compiler/plugins main

import compiler/ast # => resolves to /pathto/Nim1/compiler/ast.nim
import compiler/plugins/itersgen.nim # => resolves to /pathto/Nim_temp/compiler/plugins/itersgen.nim

benefits

links

Araq commented 3 years ago

This is a bit similar to my import $var / file ideas (which didn't make it into Nim) but has the downside that it's command-line only. I would like to see {.path ...} as pragmas, maybe with the restriction that only the main Nim file can use it. In general I want to get away both from Nim's config system and also from command-line only features.

timotheecour commented 3 years ago

has the downside that it's command-line only

I also want {.path.}, it simplifies some patterns. Note that {.path: "compiler:/pathto/compiler".} can be supported in addition to --path:prefix:path (and would call same logic) with the semantics that for a relative path, it resolves as usual to currentSourcePath. But that can be done later.

Note that nimble shells out to nim so {.path.} wouldn't be sufficient.

This is a bit similar to my import $var / file ideas

it subsumes it, if you allow a special token @ (or $) in import paths:

--path:@var:pathto/var
import @var # for a package
import @var/sub # for a module

and could simplify/generalize existing hard-coded logic for interpolated paths eg import "$lib/../tools/nimweb"

But the main point of the RFC is it works without modification to existing source files, eg import compiler/foo can be re-targetted via --path:compiler:path

maybe with the restriction that only the main Nim file can use

I don't think that's needed, this would restrict legitimate use case.

extension: versioning

timotheecour commented 3 years ago
timotheecour commented 3 years ago