-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[TODO] Nim now allows modules with same name in a package #8614
Conversation
It would be cool if this limitation is lifted, I also find that it just leads to hacky workarounds |
FYI this was actually on my TODO list too for the upcoming release. |
compiler/packagehandling.nim
Outdated
@@ -18,7 +18,9 @@ iterator myParentDirs(p: string): string = | |||
proc resetPackageCache*(conf: ConfigRef) = | |||
conf.packageCache = newPackageCache() | |||
|
|||
proc getPackageName*(conf: ConfigRef; path: string): string = | |||
when false: | |||
proc getPackageName*(conf: ConfigRef; path: string): string = |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indenting here looks wrong, it should be 2 spaces where this appears to just be 1...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In fact, why not just remove this code instead of disabling it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great, I've been hit by this limitation a couple of times now and would definitely welcome this change!
@Araq Is that TODO list public? |
Minor point, by why |
@Clyybber With this feature implemented and my |
That won't stay, it's not allowed on Windows anyway. |
50b1867
to
d2b9e8e
Compare
d2b9e8e
to
90db796
Compare
…d of root of filesystem
90db796
to
b67bc2f
Compare
Actually However, I agree PTAL (top-level PR msg reflects latest PR status) |
Why don't you just use |
which is unambiguous (unless a Nim source dir/file contains an inner "." which we can disallow for dirs (it's already disallowed for nim files, and may even already be disallowed for Nim source dirs) with your suggestion it would generate: (other eg:
|
import std/sha1 as sha1_2 | ||
# TODO: Error: cannot open file: stdlib/std/sha1; inconsistent with the fact we | ||
# generate `stdlib.std.sha1` | ||
# import stdlib/std/sha1 as sha1_3 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why would import stdlib/std/sha1
be valid? It shouldn't be valid.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is hard to follow and I don't understand why it's necessary. For the frontend you can just disable this check. For the backend you can replicate the Path structure or detect the part of the path required for disambiguation. Or you hash the full path and append it to the filename. These ideas should all lead to simpler code that doesn't touch so many parts of the compiler.
@@ -12,7 +12,7 @@ import ospaths | |||
warning("uninit", off) | |||
hint("processing", off) | |||
#--verbosity:2 | |||
patchFile("stdlib", "math", "mymath") | |||
patchFile("stdlib.pure", "math", "mymath") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Get of this .pure
requirement.
@@ -229,12 +229,20 @@ proc cmpMsgs(r: var TResults, expected, given: TSpec, test: TTest, target: TTarg | |||
r.addResult(test, target, expected.msg, given.msg, reSuccess) | |||
inc(r.passed) | |||
|
|||
import compiler/options |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh come on, the tester does not import the compiler.
@@ -262,6 +270,7 @@ proc codegenCheck(test: TTest, target: TTarget, spec: TSpec, expectedMsg: var st | |||
echo getCurrentExceptionMsg() | |||
except IOError: | |||
given.err = reCodeNotFound | |||
echo getCurrentExceptionMsg() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is nothing to report here, reCodeNotFound
is sufficient.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is introduced in this dependent PR #8631 and discussed there
@@ -92,7 +92,7 @@ proc patchFile*(package, filename, replacement: string) = | |||
## | |||
## .. code-block:: nim | |||
## | |||
## patchFile("stdlib", "asyncdispatch", "patches/replacement") | |||
## patchFile("stdlib.pure", "asyncdispatch", "patches/replacement") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The .pure
part is an implementation detail and should not be exposed.
@@ -16,19 +16,19 @@ from math import sqrt, ln, log10, log2, exp, round, arccos, arcsin, | |||
from os import getEnv, existsEnv, dirExists, fileExists, putEnv, walkDir | |||
|
|||
template mathop(op) {.dirty.} = | |||
registerCallback(c, "stdlib.math." & astToStr(op), `op Wrapper`) | |||
registerCallback(c, "stdlib.pure.math." & astToStr(op), `op Wrapper`) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These should all not be changed, other tools also rely on registerCallback
.
@@ -12,38 +12,53 @@ iterator myParentDirs(p: string): string = | |||
var current = p | |||
while true: | |||
current = current.parentDir | |||
if current.len == 0: break | |||
# IMPROVE:parentDir is buggy, "foo.nim".parentDir should be ".", not "" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What? It should be .
? No.
var parents = 0 | ||
var dirs:seq[string] = @[] | ||
var pkg = "" | ||
var path_root = "" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The compiler and all of Nim's stdlib use camelCase
.
I think the docgen must be changed as well, it currently puts every HTML file in the same directory which will cause conflicts if there are duplicate module names. |
I disagree. Worst case is the user will have to write |
Using |
@timotheecour @Araq Will we get this merged before 0.19? If the comments are addressed at least? |
Unlikely, sorry. v0.19 is overdue and bugs like #7854 take up all resources. :-) |
As I said.
|
No language I know except Nim has this restriction; lifting it just makes sense
Nim now allows modules with same name in a package, eg:
foo/bar.nim and bar.nim
this avoids having to use previous hacks like
foo/foo_bar.nim
that were there to avoid module name clashes; these were not DRY.this enables to have
tests/stdlib/ospaths.nim
instead oftests/stdlib/tospaths.nim
for a test ofospaths
(EDIT as done for 1 test in this PR)likewise, it allows things like:
etc which are more natural than:
redis/redis_types
if a user wants to import 2 modules with same basename in 1 module, he can just write:
links
NOTE
/foo/bar/jesterRepo/util/baz.nim
with
/foo/bar/jesterRepo/jester.nimble
it generates:
jester.util
as package name EDIT(ie using
getPackageName
which finds the first dir which contains a nimble file)EDIT The following may be a bit surprising but are result of current placement of files in Nim:
EDIT it generates package parents hierachically, exactly as follows:
module ospaths
->package stdlib.pure
(note: fully qualified) =>package stdlib
EDIT files like these will be generated in nimcache:
--hint[CC]=on
):TODO
registerCallback c, "stdlib.*.staticWalkDir", proc (a: VmArgs) {.nimcall.} =
; @Araq ?TODO (future PR)
import std/random
) => PR [TODO] [pending #8636] rename stdlib.nimble to std.nimble for consistency #8637depends on:
previous versions of this PR
a previous version of this PR was using a simpler scheme:
/foo/bar/jester/util/baz.nim
was mapped to@foo@bar@jester@util@
as package name ; this caused issues, eg withregisterCallback
and other tests, and made packages depend on location of nimble packages on filesystem, which is not desirablethen I made changes to use
@jester@util@
instead of@foo@bar@jester@util@
(ie usinggetPackageName
to find package root)then I made changes to use
jester.util
instead of@jester@util@
which looks more natural