I made a library that does this, but it's slow and a bit overkill for something so simple: https://github.com/paulcsmith/require_helpers.cr
My thought process is that using relative paths work great when things are int he same folder, but if you need to go up a few folders it can get confusing quickly. You have to count how many folders to go back, and it makes moving files a pain because you need to update the require paths. Instead we could have a require_wd
that requires a file with the working directory as the root.
require_wd "spec/spec_helper"
# Rather than, which is harder to read, write and change
require "../../../spec_helper"
We could also add shortcuts for require_src
and require_spec
, but maybe that's overkill for the standard lib. Thoughts?
I just opened a number of suggestion issues. I like getting suggestions for my projects as issues, but if there is a better place for me to put these, LMK and I'll happily move them!
And maybe the naming isn't good, but I think being able to require from the project root would be really helpful
Adding more names for require is ugly I think. If anything, you could add a special symbol to mean the src
dir, for example require "@/controllers/foo"
instead of require "../src/controllers/foo"
in specs. This is used in vbuild
in JS-land, and I find it pretty useful.
Yeah I don't really love the idea of different requires, but some way to require from the working directory would be incredible. I like the @
sign idea a lot actually
but then, for a require "@/hello"
in a file located at /lib/my_lib/src/just_require_hello.cr
, the required file should be looked at /lib/my_lib/src/
?
@bew yes, for libraries @
would have to refer to the library's src
.
I'm not really seriously suggesting my idea be implemented, just that if the problem that OP's library solves becomes a major pain point, I would use @
instead of multiple require
s.
How about require "/my/thing"
I can't imagine anyone _ever_ wanting to require a file using absolute path; so /
would be reasonable to signify _project root_.
Otherwise //my/thing
. Or so.
FWIW using double slashes is what Bazel and family do to reference project root.
I really like the way it is now: simple :tada:
I have no opinion on whether this is worth doing, but I'll note that svn uses ^
for the root level of a repository. eg:
Relative URL: ^/trunk/myStuff
I haven't worked with Bazel, but in some sysadmin contexts I've seen scripts create filenames which have two slashes in a row (even though they didn't intend to), but the script works fine because unix treats multiple consecutive slashes the same as a single slash.
Just do require "../../some_file.cr"
. Let's not add more and more features and special cases because of programmer laziness.
@asterite Thanks for reviewing. I think keeping features and special cases to a minimum is a great goal.
Most helpful comment
I really like the way it is now: simple :tada: