-
Notifications
You must be signed in to change notification settings - Fork 56
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
Document how to import an absolute path with import
.
#208
Comments
May be we can figure out a good place to put some helpful text -- though I wonder what sorts of circumstances [1] are a good fit for using absolute paths for imports. For reference, I found the following in Janet's changelog:
This looks like a relevant commit. The docstring for
The above is perhaps not the same, but if there isn't any documentation for it on the website, may be it could go in a similar location as for absolute path imports... [1] I was informed via another channel that it's good for testing and that seems reasonable. |
Perhaps after the Working Directory Imports section, is a reasonable location for a section about imports that involve the I think it makes sense to point out specifically that |
👏 |
I'm going to try to collect some bits here to inform the to-be-constructed section text. Feel free to skip the next bit to the summary of what the details revealed. Details that most people might want to skip...I've found this code in the C implementation of } else if (strncmp(template + i, ":@all:", 6) == 0) {
if (input[0] == '@') {
const char *p = input;
while (*p && !is_path_sep(*p)) p++;
size_t len = p - input - 1;
char *str = janet_smalloc(len + 1);
memcpy(str, input + 1, len);
str[len] = '\0';
janet_formatb(out, "%V", janet_dyn(str));
janet_sfree(str);
janet_buffer_push_cstring(out, p); Using # 0. directory preparation
(os/mkdir "/tmp/")
(os/mkdir "/tmp/import-test")
# 1. @-prefixed path import demo
(spit "/tmp/import-test/module-1.janet" "(def a 1)")
(setdyn :fun "/tmp/import-test")
# after the @, the first path segment is the string "fun"
# so @fun is replaced with "/tmp/import-test", to produce
# the path "/tmp/import-test/module-1"
(import @fun/module-1)
(print module-1/a)
# 2. absolute path import demo
(spit "/tmp/import-test/module-2.janet" "(def b 2)")
# after the @, the first path segment is seen as an empty
# string, and is not replaced with anything
(import @/tmp/import-test/module-2)
(print module-2/b) It was indeed the case that the For 2., janet_formatb(out, "%V", janet_dyn(str)); doesn't really affect Thus in this case, because janet_buffer_push_cstring(out, p); resulted in SummarySo what does this mean? The above investigation made the manner in which absolute path imports work for @-prefixed paths clearer. Specifically, although the quote from the changelog above has the text:
that mentions a dynamic binding, this isn't apparent in the section of code starting with 2. in the above sample The docstring for
The investigation also revealed that "the first path segment" for a path like To clarify a bit, "path separator" can behave differently depending on whether one is using Windows: static int is_path_sep(char c) {
#ifdef JANET_WINDOWS
if (c == '\\') return 1;
#endif
return c == '/';
} This is not to say that In any case, if interested in writing code that can work on things other than Windows, it's probably best to avoid using |
Ok, below is an initial draft that incorporates some earlier text from the changelog along with results from what we learned about absolute path imports.
|
Excellent. |
Thanks for taking a look. PR #213 (with some minor edits) has been submitted. |
As #213 has been merged, I think this can be closed now. |
I recently learned that I can import
/path/to/module.janet
withThis is not documented on the website, yet.
The text was updated successfully, but these errors were encountered: