compile schema FEATURE store cur mod for leafrefs

Because they may differ from the node itself.
Test with deviation included.
diff --git a/src/plugins_types.c b/src/plugins_types.c
index 0564eeb..552ca62 100644
--- a/src/plugins_types.c
+++ b/src/plugins_types.c
@@ -1904,7 +1904,7 @@
     uint32_t i;
 
     /* find all target data instances */
-    ret = lyxp_eval(lref->path, node->schema->module, LY_PREF_SCHEMA_RESOLVED, lref->prefixes, node, tree, &set, 0);
+    ret = lyxp_eval(lref->path, lref->cur_mod, LY_PREF_SCHEMA_RESOLVED, lref->prefixes, node, tree, &set, 0);
     if (ret) {
         ret = LY_ENOTFOUND;
         val_str = lref->plugin->print(value, LY_PREF_JSON, NULL, &dynamic);
diff --git a/src/schema_compile.c b/src/schema_compile.c
index 70e423e..17f3158 100644
--- a/src/schema_compile.c
+++ b/src/schema_compile.c
@@ -1091,7 +1091,7 @@
     assert(node->nodetype & (LYS_LEAF | LYS_LEAFLIST));
 
     /* try to find the target */
-    LY_CHECK_RET(ly_path_compile(ctx->ctx, node->module, node, lref->path, LY_PATH_LREF_TRUE, lysc_is_output(node) ?
+    LY_CHECK_RET(ly_path_compile(ctx->ctx, lref->cur_mod, node, lref->path, LY_PATH_LREF_TRUE, lysc_is_output(node) ?
             LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT, LY_PATH_TARGET_MANY, LY_PREF_SCHEMA_RESOLVED, lref->prefixes,
             unres, &p));
 
diff --git a/src/schema_compile_node.c b/src/schema_compile_node.c
index 9593d09..8571aca 100644
--- a/src/schema_compile_node.c
+++ b/src/schema_compile_node.c
@@ -1536,6 +1536,7 @@
             LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type", "");
             return LY_EVALID;
         }
+        lref->cur_mod = type_p->pmod->mod;
         break;
     case LY_TYPE_INST:
         /* RFC 7950 9.9.3 - require-instance */
@@ -1585,6 +1586,7 @@
                             lref->basetype = LY_TYPE_LEAFREF;
                             LY_CHECK_RET(lyxp_expr_dup(ctx->ctx, ((struct lysc_type_leafref *)un_aux->types[v])->path, &lref->path));
                             lref->refcount = 1;
+                            lref->cur_mod = ((struct lysc_type_leafref *)un_aux->types[v])->cur_mod;
                             lref->require_instance = ((struct lysc_type_leafref *)un_aux->types[v])->require_instance;
                             LY_CHECK_RET(lysc_prefixes_dup(((struct lysc_type_leafref *)un_aux->types[v])->prefixes,
                                     &lref->prefixes));
diff --git a/src/tree_schema.h b/src/tree_schema.h
index e977f96..f7b854b 100644
--- a/src/tree_schema.h
+++ b/src/tree_schema.h
@@ -1448,6 +1448,7 @@
     uint32_t refcount;               /**< reference counter for type sharing */
     struct lyxp_expr *path;          /**< parsed target path, compiled path cannot be stored because of type sharing */
     struct lysc_prefix *prefixes;    /**< resolved prefixes used in the path */
+    const struct lys_module *cur_mod;/**< current module for the leafref (path) */
     struct lysc_type *realtype;      /**< pointer to the real (first non-leafref in possible leafrefs chain) type. */
     uint8_t require_instance;        /**< require-instance flag */
 };
diff --git a/src/xpath.c b/src/xpath.c
index 14738c7..811a8a0 100644
--- a/src/xpath.c
+++ b/src/xpath.c
@@ -3700,7 +3700,7 @@
             oper = lysc_is_output((struct lysc_node *)sleaf) ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT;
 
             /* it was already evaluated on schema, it must succeed */
-            rc = ly_path_compile(set->ctx, sleaf->module, (struct lysc_node *)sleaf, lref->path,
+            rc = ly_path_compile(set->ctx, lref->cur_mod, (struct lysc_node *)sleaf, lref->path,
                     LY_PATH_LREF_TRUE, oper, LY_PATH_TARGET_MANY, LY_PREF_SCHEMA_RESOLVED, lref->prefixes, NULL, &p);
             assert(!rc);
 
diff --git a/tests/utests/schema/test_tree_schema_compile.c b/tests/utests/schema/test_tree_schema_compile.c
index 7c5666f..4e0f571 100644
--- a/tests/utests/schema/test_tree_schema_compile.c
+++ b/tests/utests/schema/test_tree_schema_compile.c
@@ -3119,6 +3119,21 @@
     assert_string_equal("l", leaf->name);
     assert_int_equal(LY_TYPE_IDENT, leaf->type->basetype);
 
+    ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "module y {namespace urn:y;prefix y;"
+                                  "container cont {leaf l {type string;}}"
+                                  "leaf bl2 {type string;}"
+                                  "}");
+    assert_int_equal(LY_SUCCESS, lys_parse_mem(ctx, "module z {namespace urn:z;prefix z;"
+                                  "import y {prefix y;}"
+                                  "deviation \"/y:cont/y:l\" {deviate replace {type leafref {path \"/al\";}}}"
+                                  "leaf al {type string;}"
+                                  "leaf al2 {type leafref {path \"/y:bl2\";}}"
+                                  "}", LYS_IN_YANG, NULL));
+    assert_non_null((mod = ly_ctx_get_module_implemented(ctx, "y")));
+    assert_non_null(leaf = (struct lysc_node_leaf *)lysc_node_children(mod->compiled->data, 0));
+    assert_string_equal("l", leaf->name);
+    assert_int_equal(LY_TYPE_LEAFREF, leaf->type->basetype);
+
     assert_int_equal(LY_ENOTFOUND, lys_parse_mem(ctx, "module aa1 {namespace urn:aa1;prefix aa1;import a {prefix a;}"
                               "deviation /a:top/a:z {deviate not-supported;}}", LYS_IN_YANG, &mod));
     logbuf_assert("Deviation(s) target node \"/a:top/a:z\" from module \"aa1\" was not found.");