Repository navigation
Description
Description
When a module is loaded from disk and LY_CTX_DISABLE_SEARCHDIR_CWD is not
set, libyang searches the current working directory and all its
subdirectories. In 5.x libyang searched only the current working directory
itself, and the comments in the code still say so ("add CWD, not searched
recursively", "subdirectories are not taken into account in current working
dir").
The behavior changed in d29bfc1 (tree schema UPDATE min-recommended-version
support). We found it in v6.4.13, and the code is the same in the current
devel (95f8bdb).
An application started from a large directory tree walks the whole tree on
every module lookup. Our tool runs from a directory with about 130k files on
a file system shared into a virtual machine. A run that loads one module took
about 20 s instead of 0.01 s. libyang can also load an unrelated module of the
same name from any subdirectory of the current working directory.
Cause
lys_search_localfile_file_type() (src/tree_schema.c:3201 in v6.4.13) decides
whether to descend into a subdirectory with this condition:
if (is_dir && (dirs->count || !implicit_cwd)) {The condition relied on the old loop in lys_search_localfile(), which popped
directories from the end of the set (dirs->count--). The current working
directory was added to the set first, so the loop processed it last. At that
point dirs->count was already 0, and the subdirectories were skipped.
d29bfc1 replaced the old loop with a forward loop in _lys_search_localfile()
(src/tree_schema.c:3410):
for (i = 0; (i < dirs->count) && (match < 2); ++i) {The new loop does not remove anything from dirs. dirs->count is always at
least 1, so the condition above is always true and every subdirectory of the
current working directory is added and searched. The forward loop also visits
the current working directory first, before the search dirs of the context.
The old loop visited it last.
Reproduction
We ran the reproduction against v6.4.13 (f7e0cc1), built as a static library
on Linux aarch64.
mkdir -p /tmp/lyrepro/cwd/a/b/c /tmp/lyrepro/other
cat > /tmp/lyrepro/cwd/a/b/c/foo.yang <<'EOF'
module foo { yang-version 1.1; namespace "urn:foo"; prefix f; leaf x { type string; } }
EOF#include <stdio.h>
#include <libyang/libyang.h>
int main(void) {
struct ly_ctx *ctx;
const struct lys_module *mod;
/* MODDIR is the installed libyang module dir (share/yang/modules/libyang) */
if (ly_ctx_new(MODDIR, 0, &ctx)) {
return 2;
}
mod = ly_ctx_load_module(ctx, "foo", NULL, NULL);
printf("module \"foo\" %s\n", mod ? "FOUND" : "not found");
if (mod) {
printf("loaded from: %s\n", mod->filepath);
}
ly_ctx_destroy(ctx);
return 0;
}Run from /tmp/lyrepro/cwd, the program finds foo in a/b/c. Run from
/tmp/lyrepro/other, it does not:
$ cd /tmp/lyrepro/cwd && ../repro
module "foo" FOUND
loaded from: /tmp/lyrepro/cwd/a/b/c/foo.yang
$ cd /tmp/lyrepro/other && ../repro
libyang[0]: Loading "foo@<any>" module failed, not found.
module "foo" not found
We expect foo not to be found in either case, because a/b/c is a
subdirectory of the current working directory. We did not run the program
against 5.6.1. The expected result comes from reading the 5.6.1 code.
If you want, we can prepare a pull request with a fix.
Activity
tree schema BUGFIX cwd is not searched recursively
michalvasko commentedon Nov 18, 2026
Thanks, this was really a regression that should now be fixed. But it is exactly the reason why I refactored it in the first place, to make the code more readable and not include such obscure logic.