I have some questions regarding how require exactly works.
I want to build an XML parser and started with crystal init lib xml
create xml/.gitignore
create xml/LICENSE
create xml/README.md
create xml/.travis.yml
create xml/shard.yml
create xml/src/xml.cr
create xml/src/xml/version.cr
create xml/spec/spec_helper.cr
create xml/spec/xml_spec.cr
Initialized empty Git repository in /Users/fibric/Projects/crystal/xml/.git/
Then run cd xml; crystal spec or the bash/zsh version cd xml && crystal spec.
Undefined symbols for architecture x86_64:
"_*Xml@Object::to_s:String", referenced from:
_*describe<Xml:Module, String, Int32, &Proc(Nil)>:Spec::Context+ in _main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Error: execution of command failed with code: 1: `cc -o "/Users/fibric/.cache/crystal/crystal-run-spec.tmp" "${@}" -rdynamic -lxml2 -lpcre -lgc -lpthread /usr/local/Cellar/crystal-lang/0.18.7_1/src/ext/libcrystal.a -levent -liconv -ldl -L/usr/lib -L/usr/local/lib`
I re-tried but this time cd ..; crystal init lib xml.cr
create xml.cr/.gitignore
create xml.cr/LICENSE
create xml.cr/README.md
create xml.cr/.travis.yml
create xml.cr/shard.yml
create xml.cr/src/xml.cr.cr
create xml.cr/src/xml.cr/version.cr
create xml.cr/spec/spec_helper.cr
create xml.cr/spec/xml.cr_spec.cr
Initialized empty Git repository in /Users/fibric/Projects/crystal/xml.cr/.git/
Then run cd xml.cr; crystal spec or the bash/zsh version cd xml.cr && crystal spec.
It created a folder named xml.crwith a file xml.cr.cr inside.
Error in line 1: while requiring "./spec/xml.cr_spec.cr"
in ./spec/xml.cr_spec.cr:1: while requiring "./spec_helper"
require "./spec_helper"
^
in ./spec/spec_helper.cr:2: while requiring "../src/xml.cr": Error reading file: Is a directory
require "../src/xml.cr"
Strange. I couldn't find a require.cr file to see what's happening but here are my thoughts.
In my first attempt require tries to load xml from the standard library. In my second attempt require cuts of .cr and sees now only the folder named xml.cr. Am I right?
Reduced:
module Foo
def self.foo
1 + 2
end
end
Foo.foo
module FOO
def self.foo
1.0 + 2
end
end
FOO.foo
There seems to be a problem with classes that are named the same 'modulo" case sensitivity.
In your case the problem is that the standard library already defines an XML module. I personally would use that, not implement an XML parsing library from scratch (very hard!)
A pure Crystal XML parser is indeed quite the effort, but would be awesome! libxml2 is a nightmare.
And what about the 2nd case? When I named the project xml.cr. It looks to me that require cuts off .cr when it's looking for file system objects.
@fibric Our of curiosity, are you on a mac?
@fibric Yes, there's no need to use ".cr" in requires, as all crystal files end with ".cr"
@asterite yes, I showed it in the movie or brew doctor did.
I used crystal init lib xml.cr if require cuts off .cr from files, I think the init tool may needs to warn or should cut off .cr too after the folder has been created.
@fibric Oh, didn't notice that. The issue only happens in mac because the filesystem is case-insensitive, and we use a type's name as the filename.
In this case, maybe instead of making it work with these filenames, it would be better if the compiler errors if a type with the same name case-insensitive-wise exists, because I think having Foo and FOO as two types in the same level is pretty confusing.
Makes sense too me.
_OT: Apple has done the best possible to screw everyone trying to use a case sensitive HFS+ partition for System+Data. Not sure if the new file system will change that..._
馃憤
Most helpful comment
A pure Crystal XML parser is indeed quite the effort, but would be awesome! libxml2 is a nightmare.