go/build: document that Context.HasSubdir does not check existence of dir #17888
Labels
Documentation
FrozenDueToAge
NeedsFix
The path to resolution is known, but the work has not been done.
Milestone
What version of Go are you using (
go version
)?What operating system and processor architecture are you using (
go env
)?What did you do?
I read the documentation for
Context.HasSubdir
:https://godoc.org/go/build#Context.HasSubdir
What did you expect to see?
Based on the name of the func and the phrase "whether dir is a subdirectory of root", I expected the default implementation (when HasSubdir is nil), which uses filepath.EvalSymlinks, to return false if the directory doesn't exist.
I also asked another person, and that was their understanding of the expected behavior too.
What did you see instead?
After looking into the implementation, I learned that the default implementation (and therefore, all custom implementation that are expected to act in the same way) does not try to ensure that
dir
is a subdirectory of root.Instead, it simply reports "whether dir is a path that points to what would be a subdirectory (possibly multiple levels below) of root".
In other words, it does not check that
dir
is a directory that actually exists. It simply does a lexical path check (also usesfilepath.EvalSymlinks
to resolve potential symlinks).This is one of those situations where I'm not completely sure if this is a bug in the implementation, or a case of slightly poor/misleading documentation.Looking more into the history of the relevant code, and the fact thatIsDir
exists solely to check existence of a directory, I am 80%+ confident the current behavior is correct and this is just a documentation issue.Update: After spending more time with the code, I am now 97%+ confident the current behavior is correct, and this is just a documentation/naming issue.
IsDir
exists for checking if a directory exists, so it's not the responsibility ofHasSubdir
to check that too.What do other people think about the current phrasing?
I think perhaps it should be clarified that
dir
does not need to exist forHasSubdir
to return true.Maybe it can be clarified by rewriting it to be something like this (except better)?
The text was updated successfully, but these errors were encountered: