-
Notifications
You must be signed in to change notification settings - Fork 228
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
Documentation for module/expand-path
is wrong and confusing.
#1370
Comments
After looking at the current implementation, trying some invocations and revisiting the docstring, I think I've got a clearer sense of it. To review part of the docstring:
I think an important point about this is the last bit of text:
That is, it's a function with the purpose of producing paths that can be used for importing files, specifically in the context of Currently,
So it seems to me that it's possible that
(^^; As to the parts of the docstring that follow "The replacements are as follows", I also found some of them confusing (specifically |
TLDR:
Regarding related commits:
The first two commits mentioned above are from 2019-06, while the third one is from 2019-12. One useful interpretation might be that the code is in its intended state and that the docstring is not aligned (as the issue claims). Details below...
/* Calculate dirpath from current file */
const char *curname = curfile + strlen(curfile);
while (curname > curfile) {
if (is_path_sep(*curname)) break;
curname--;
}
const char *curdir;
int32_t curlen;
if (curname == curfile) {
/* Current file has one or zero path segments, so
* we are in the . directory. */
curdir = ".";
curlen = 1;
} else {
/* Current file has 2 or more segments, so we
* can cut off the last segment. */
curdir = curfile;
curlen = (int32_t)(curname - curfile);
} and this bit: } else if (strncmp(template + i, ":cur:", 5) == 0) {
janet_buffer_push_bytes(out, (const uint8_t *)curdir, curlen);
i += 4; Also, there are these tests: (setdyn :current-file "some-dir/some-file")
(defn test-expand [path temp]
(string (module/expand-path path temp)))
(assert (= (test-expand "abc" ":cur:/:all:") "some-dir/abc")
"module/expand-path 1")
(assert (= (test-expand "./abc" ":cur:/:all:") "some-dir/abc")
"module/expand-path 2")
(assert (= (test-expand "abc/def.txt" ":cur:/:name:") "some-dir/def.txt")
"module/expand-path 3")
(assert (= (test-expand "abc/def.txt" ":cur:/:dir:/sub/:name:")
"some-dir/abc/sub/def.txt") "module/expand-path 4") Based on the above information (and observed behavior) I agree that:
seems off. So to repeat, On a side note, it seems that
It looks like one place that the dynamic variable associated with I think there are at least two ways it can be an absolute path though:
The second way can be demonstrated like:
Update: I don't have anything comparable about
|
The current path expansion logic feels unclean. Janet doesn't have string interpolation, so this is more of a special case. idea:
|
Based on the initial report and some of the exploration above, I'm thinking of suggesting the following modification to the docstring: diff --git a/src/core/corelib.c b/src/core/corelib.c
index c4dec6f0..e6a60d7b 100644
--- a/src/core/corelib.c
+++ b/src/core/corelib.c
@@ -116,8 +116,8 @@ JANET_CORE_FN(janet_core_expand_path,
"* :@all: -- Same as :all:, but if `path` starts with the @ character, "
"the first path segment is replaced with a dynamic binding "
"`(dyn <first path segment as keyword>)`.\n\n"
- "* :cur: -- the current file, or (dyn :current-file)\n\n"
- "* :dir: -- the directory containing the current file\n\n"
+ "* :cur: -- the directory portion, if any, of (dyn :current-file)\n\n"
+ "* :dir: -- the directory portion, if any, of the path argument\n\n"
"* :name: -- the name component of path, with extension if given\n\n"
"* :native: -- the extension used to load natives, .so or .dll\n\n"
"* :sys: -- the system path, or (dyn :syspath)") { As a reminder, here are some of the relevant tests: (setdyn :current-file "some-dir/some-file")
(defn test-expand [path temp]
(string (module/expand-path path temp)))
(assert (= (test-expand "abc" ":cur:/:all:") "some-dir/abc")
"module/expand-path 1")
(assert (= (test-expand "./abc" ":cur:/:all:") "some-dir/abc")
"module/expand-path 2")
(assert (= (test-expand "abc/def.txt" ":cur:/:name:") "some-dir/def.txt")
"module/expand-path 3")
(assert (= (test-expand "abc/def.txt" ":cur:/:dir:/sub/:name:")
"some-dir/abc/sub/def.txt") "module/expand-path 4") |
@amano-kenji I filed #1373 and it was merged. Do you think it addressed your concerns? If so, please close this issue. |
🐈 |
Actually, this needs to be addressed, too. I don't think the explanation is plain enough. This can be a good summary of what it does for someone who already knows what it does. But, I'm still a newbie. |
Although I can see that one might think the explanation may not be plain enough on its own, I don't agree that:
is "wrong and confusing" (what this issue is about). Also, the docstring mentions |
This is not wrong, but the language is confusing for newbies.
I would write something like
If you read my j3blocks function doc, you would realize that I explain things in multiple sentences... I use simple unambiguous sentences. That's how newbies should learn new things... In english, one word can have multiple meanings. I want to avoid connotations and confusions. I would try to explain something in several ways. Most open-source developers are bad teachers... Explain it as you would to a newbie. |
I am not a friend of the lengthy documentation in the code. And |
Think of me as a newbie. That's not very long. That's only a little bit longer. |
I find the current text to no longer be "wrong" nor "confusing". I think this issue has helped to bring about improvement in the existing documentation, but since #1373 was merged, I don't feel it needs more work [1]. If someone feels further changes might be helpful, they can submit a PR. If the content is deemed a net positive change, may be merging will follow :) [1] (I'll add that I think docstrings can be helpful, but in Janet's case there is also website documentation as well that is complementary and I believe better for some things.) |
@sogaiu, I meant it the way you wrote it in [1] for the last comment. That is why I specifically wrote: "in the code." And I do not see any newbie who needs to understand how modules are found and loaded. If they do, the language has failed them. |
Okay. Closing for now. |
This was really confusing without reverse-engineering
module/expand-path
by calling it many times with different arguments. If this was an IQ test, I would fail.It is the directory part of
path
argument.This is the directory part of the janet file that's compiled. It's not the directory of the current source file. Let's assume that
test.janet
is at $HOME.test.janet's content
If the current working directory is $HOME.
If the current working directory is
/
.:cur:
is really not what the documentation says it is.The text was updated successfully, but these errors were encountered: