-
Couldn't load subscription status.
- Fork 23
Description
This PR nim-lang/Nim#18496 improves and generalizes (and in fact subsumes) patchFile as follows:
--moduleoverridecan be used in cmdline (and therefore, in user config too)- it uses the canonical module names introduced in fix #16973 ; nim doc now shows correct, canonical import name in title Nim#16999 to display module names in docs in an un-ambiguous way (also used in
--filenames:abs|canonical|legacyRelProjfor filenames in compiler msgs (replaces--listfullpaths:on|off) Nim#17746 in processing:canonical)
the most important aspect of --moduleoverride though is that it allows applying the module override in a non-global way, using module prefixes, eg:
--moduleoverride:std/sequtils:std/sequtilsv0 # applies globally
--moduleoverride:std/sequtils:std/sequtilsv0:pkg/mypkg # override only applies within pkg/mypkg
--moduleoverride:std/sequtils:std/sequtilsv0:pkg/mypkg/sub # only within pkg/mypkg/sub
prefix is an optional comma delimited set of prefixes, eg:
std/strutils,pkg/mypkg,pkg/cligen/bar
it can also contain absolute paths (but not relative paths; a std/ or system/ or pkg/ prefix with a canonical name must be used if the path is not absolute)
example use case 1: breaking change mitigation
this can be used to override a module for just a package, without affecting the behavior of other packages. For eg, when upgrading nim to a new version, if some package foo requires a pre-existing behavior in some stdlib module (eg std/sequtils), you can override std/sequtils as needed and make the override apply only within package foo, unlike a flag like -d:nimLegacyFoo which has global effect:
nim --moduleoverride:std/sequtils:pkg/foo/sequtils_override:pkg/foo /pathto/main.nim
example use case 2: custom behavior for a dependency
this is useful for experimentation or if you want to change behavior of a 3rd party package foo in a way that doesn't affect other dependencies that also depend on foo.
example use case 3: replacement patchFile that can work on cmdline/cfg/nims files
(unlike patchFile which is only nimscript)
note
the overridden module can co-exist with the non-overridden module within a project, and this ends up being 2 separate modules (PSym).
use --processing:filenames to see what modules are being imported (shows import stacks since nim-lang/Nim#18372)
note: rationale for canonical modules names
the way patchFile refers to module names has inherent ambiguities, eg:
- duplicate module names within a package eg
foo/bar/bazvsfoo/bazare confused, both as foo_baz foo_bar/bazvsfoo/bar_bazare confused, both as foo_bar_baz
furthermore, it doesn't correspond to how you'd import a module.
future work
-
--symoverride:mod1.sym:sym2:prefix: variant which allows to override a fully qualified symbolmod1.symbymod1.sym2within the context of modules matchingprefix(egsystem.delete=>system.deleteV0to get back old behavior within the context of some pkg/foo only).--moduleoverrideis more general (because you can override a whole module as you need) but--symoverridecan be easier to use in some cases especially in cases where a behavior for an API was changed and you want the old behavior for a package without affecting other packages.