I got an error in my project
https://github.com/Cirru/interpreter.nim
=>> nimble build
Verifying dependencies for cirruInterpreter@0.0.1
Installing cirru-parser@>= 0.0.3
Downloading https://github.com/Cirru/parser.nim using git
Verifying dependencies for cirruParser@0.0.3
Installing cirruParser@0.0.3
Prompt: cirruParser@0.0.3 already exists. Overwrite? [y/N]
Answer: y
Success: cirruParser installed successfully.
Building cirruInterpreter/main using c backend
Tip: 10 messages have been suppressed, use --verbose to show them.
Error: Build failed for package: cirruInterpreter
... Details:
... Execution failed with exit code 1
... Command: "/usr/local/Cellar/nim/0.20.2/nim/bin/nim" c --noBabelPath --path:"/var/folders/6p/qnt9f2tn4p5gmchth6g7brjm0000gn/T/nimble_52915/githubcom_Cirruparsernim_0.0.3/src" -o:"/Users/chen/repo/cirru/interpreter.nim/out/main" "/Users/chen/repo/cirru/interpreter.nim/src/main.nim"
... Output: Hint: used config file '/usr/local/Cellar/nim/0.20.2/nim/config/nim.cfg' [Conf]
... Hint: system [Processing]
... Hint: widestrs [Processing]
... Hint: io [Processing]
... Hint: main [Processing]
... Hint: os [Processing]
... Hint: strutils [Processing]
... Hint: parseutils [Processing]
... Hint: math [Processing]
... Hint: bitops [Processing]
... Hint: macros [Processing]
... Hint: algorithm [Processing]
... Hint: unicode [Processing]
... Hint: pathnorm [Processing]
... Hint: osseps [Processing]
... Hint: posix [Processing]
... Hint: times [Processing]
... Hint: options [Processing]
... Hint: typetraits [Processing]
... Hint: re [Processing]
... Hint: pcre [Processing]
... Hint: rtarrays [Processing]
... Hint: cirruParser [Processing]
... Hint: json [Processing]
... Hint: hashes [Processing]
... Hint: tables [Processing]
... Hint: lexbase [Processing]
... Hint: streams [Processing]
... Hint: parsejson [Processing]
... Hint: types [Processing]
... Hint: lexer [Processing]
... Hint: strformat [Processing]
... Hint: transformer [Processing]
... Hint: helpers [Processing]
... Hint: sequtils [Processing]
... Hint: types [Processing]
... /Users/chen/repo/cirru/interpreter.nim/src/main.nim(11, 24) Error: redefinition of 'types'; previous declaration here: /Users/chen/repo/cirru/interpreter.nim/src/cirruInterpreter/types.nim(1, 2)
The error
... /Users/chen/repo/cirru/interpreter.nim/src/main.nim(11, 24) Error: redefinition of 'types'; previous declaration here: /Users/chen/repo/cirru/interpreter.nim/src/cirruInterpreter/types.nim(1, 2)
was caused by the filename cirruInterpreter/types.nim, which is duplicated with the name in cirruParser/types.nim. I can fix the error by rename the file to cirruInterpreter/interpreterTypes.nim. But it looks quite strange.
Was it by design that we can't have same filenames in one build even through they are in different packages? Is there any quick fix that I can use the filename types.nim again?
This is because modules' names get defined in the importing module, so that you can write e.g. types.x, and in this case types would be ambiguous.
It would be possible to import the same named modules by different names, like import cirruInterpreter/types as interpreterTypes (would be enough to do that for one of them), but then compiler runs into a bug (as I understand), compiling modules into the same C files, so then compilation fails at C side.
So the problem types got re-defined since I called import cirruParser/types before current importing. I got a solution finally by modifying the source of the parser to export functions at namespace cirruParser then I don't need to import cirruPraser/types anymore. Works for now.
Turned out it can be bad habit to import pkg/file modules from other packages. All functions should be exported at pkg's namespace.
Quick fix:
import cirruParser / types as cpTypes
This is because the actual module name is types, not cirruParser/types, so it will conflict when you import yet another types elsewhere. In such case, the import as statement becomes handy.
That doesn't work, if modules contain some executable code. Simple test:
# test.nim
import cirruParser / types as cpTypes
import anotherModule / types as otherTypes
p1()
p2()
# cirruParser/types.nim
proc p1* = echo "cirruParser"
# anotherModule/types.nim
proc p2* = echo "anotherModule"
Another one:
# test.nim
import cirruParser / types as cpTypes
import anotherModule / types as otherTypes
# cirruParser/types.nim
echo "cirruTypes"
# anotherModule/types.nim
echo "otherTypes"
They fail at C side, as described above (one and the same C output file is used for both).
With only types or constants in both imported modules it works, because no C files are generated for them then.