R/member-metadata.R

Defines functions member_resolve_packages member_prepare_startup_data member_prepare_package member_prepare_r6_roots member_prunable_r6_generators member_index_thaw member_index_freeze member_namespace_input member_public_roots member_namespace_export_targets member_namespace_lazydata member_source_input member_snapshot_shape member_snapshot member_snapshot_dollar_context member_subset_registry member_list_storage member_function_identity_index member_function_syntax member_strip_srcref member_binding

# Metadata preparation runs in package workers. Request handlers only read
# serializable snapshots; no runtime object is retained as a completion hook.

member_base_intrinsics <- c(
    "list", "new.env", "invisible", "identity", "local", "withAutoprint",
    "head", "tail", "tryCatch", "structure", "data.frame", "length", "startsWith",
    "c", ":", "seq", "seq_len", "seq_along", "rep", "rep.int", "as.character",
    "character", "as.integer", "integer", "as.numeric", "as.double", "numeric",
    "double", "as.logical", "logical", "as.raw", "raw", "charToRaw", "as.Date",
    "as.POSIXct", "factor", "paste", "paste0", "sprintf", "names", "is.null",
    "missing", "isTRUE", "isFALSE", "inherits", "is.character", "is.numeric",
    "is.integer", "is.logical", "is.list", "lapply", "Reduce", "UseMethod",
    "NextMethod", "stop", "switch", "+", "-", "*", "/", "^", "%%", "%/%", "&",
    "|", "!", "==", "!=", ">", "<", ">=", "<=", "&&", "||"
)

member_binding <- function(env, name, materialize = FALSE) {
    .Call("member_binding_c", env, name, materialize, PACKAGE = "languageserver")
}

# Packages installed with keep.source carry source references on every `{`
# and nested function expression, each pointing at an environment holding the
# whole source file. They would inflate snapshots and make function identities
# depend on how a package was installed.
member_strip_srcref <- function(part) {
    if (is.name(part)) return(part)
    if (inherits(part, "srcref")) return(NULL)
    if (!is.null(attributes(part))) {
        attr(part, "srcref") <- NULL
        attr(part, "wholeSrcref") <- NULL
        attr(part, "srcfile") <- NULL
    }
    if (is.pairlist(part)) {
        # Subassignment turns a pairlist into a list; formals must stay one.
        items <- as.list(part)
        for (i in seq_along(items)) {
            items[i] <- list(member_strip_srcref(items[[i]]))
        }
        return(as.pairlist(items))
    }
    if (is.call(part) || is.expression(part)) {
        for (i in seq_along(part)) {
            part[i] <- list(member_strip_srcref(part[[i]]))
        }
    }
    part
}

member_function_syntax <- function(fn) {
    code <- body(fn)
    syntax <- as.call(list(as.name("function"), formals(fn), code))
    if (is.null(attr(fn, "srcref", exact = TRUE)) && is.null(attr(code, "srcref", exact = TRUE))) {
        return(syntax)
    }
    member_strip_srcref(syntax)
}

# Registry closures often already have a namespace name. Partition them by
# inert syntax before comparing identities, preserving the first matching
# binding even for equivalent copied closures or digest collisions. The memo
# retains each closure only for this extraction, so pointer reuse cannot turn
# an unrelated anonymous method into a cached match.
member_function_identity_index <- function(functions) {
    buckets <- new.env(hash = TRUE, parent = emptyenv())
    memo <- new.env(hash = TRUE, parent = emptyenv())
    record <- function(fn) {
        pointer <- .Call("member_function_key_c", fn, PACKAGE = "languageserver")
        stored <- memo[[pointer]]
        if (is.null(stored)) {
            stored <- list(fn = fn, key = digest::digest(deparse1(member_function_syntax(fn)), algo = "xxhash64"))
            memo[[pointer]] <- stored
        }
        list(pointer = pointer, stored = stored)
    }
    for (name in names(functions)) {
        key <- record(functions[[name]])$stored$key
        buckets[[key]] <- c(buckets[[key]], name)
    }
    function(fn) {
        item <- record(fn)
        stored <- item$stored
        if (!isTRUE(stored$matched)) {
            for (name in buckets[[stored$key]]) {
                if (identical(functions[[name]], fn)) {
                    stored$name <- name
                    break
                }
            }
            stored$matched <- TRUE
            memo[[item$pointer]] <- stored
        }
        stored$name
    }
}

member_list_storage <- function(value) {
    storage <- unclass(value)
    nms <- attr(value, "names", exact = TRUE)
    # Dimensions supply implicit matrix/array classes even after unclass().
    # Keep only names so lapply() cannot dispatch a coercion method.
    attributes(storage) <- if (is.null(nms)) NULL else list(names = nms)
    storage
}

# Internal subset generics use the registered method table in base. Read
# identities only in the worker; request handlers never look up runtime methods.
member_subset_registry <- function(ns, package) {
    record <- member_binding(asNamespace("base"), ".__S3MethodsTable__.")
    if (!identical(record[[1L]], "value") || !is.environment(record[[2L]])) {
        return(list(methods = character(), complete = FALSE))
    }
    table <- record[[2L]]
    keys <- package_metadata_sort(ls(table, all.names = TRUE, sorted = FALSE))
    keys <- keys[startsWith(keys, "[.") | startsWith(keys, "[[.")]
    if (length(keys) > 5000L) return(list(methods = character(), complete = FALSE))
    methods <- stats::setNames(vapply(keys, function(key) {
        registered <- member_binding(table, key)
        if (!identical(registered[[1L]], "value") || typeof(registered[[2L]]) != "closure") return("opaque")
        fn <- registered[[2L]]
        if (key %in% c("[.data.frame", "[[.data.frame")) {
            standard <- member_binding(asNamespace("base"), key)
            if (identical(standard[[1L]], "value") && identical(standard[[2L]], fn)) return("base")
        }
        local <- member_binding(ns, key)
        if (identical(local[[1L]], "value") && identical(local[[2L]], fn)) {
            provider <- package
        } else {
            scope <- environment(fn)
            while (is.environment(scope) && !isNamespace(scope) && !identical(scope, emptyenv())) scope <- parent.env(scope)
            if (!isNamespace(scope)) return("opaque")
            provider <- unname(getNamespaceName(scope))
            original <- member_binding(scope, key)
            if (!identical(original[[1L]], "value") || !identical(original[[2L]], fn)) return("opaque")
        }
        paste0("namespace:", provider, ":", digest::digest(member_function_syntax(fn), algo = "xxhash64"))
    }, character(1L)), keys)
    list(methods = methods, complete = TRUE)
}

member_snapshot_dollar_context <- function(lookup_scope) {
    method_scopes <- list()
    scope <- lookup_scope
    while (is.environment(scope) && !identical(scope, emptyenv())) {
        method_scopes[[length(method_scopes) + 1L]] <- list(env = scope, names = ls(scope, all.names = TRUE))
        scope <- parent.env(scope)
    }
    # $ is an internal S3 generic whose registered methods live in base.
    # Inspect binding kinds rather than calling getS3method(), which can force
    # deferred method bindings or invoke active bindings during lookup.
    table <- member_binding(asNamespace("base"), ".__S3MethodsTable__.")
    registered <- if (identical(table[[1L]], "value") && is.environment(table[[2L]])) table[[2L]] else NULL
    if (!is.null(registered)) {
        method_scopes[[length(method_scopes) + 1L]] <- list(
            env = registered, names = ls(registered, all.names = TRUE)
        )
    }
    list(scopes = method_scopes, registered = registered, methods = new.env(parent = emptyenv()))
}

member_snapshot <- function(env, max_depth = 4L, max_bindings = 5000L, s7_modes = list(),
    lookup_scope = parent.frame(), dollar_context = NULL, function_syntax = member_function_syntax) {
    seen <- list()
    remaining <- max_bindings
    if (is.null(dollar_context)) dollar_context <- member_snapshot_dollar_context(lookup_scope)
    method_scopes <- dollar_context$scopes
    registered <- dollar_context$registered
    dollar_methods <- if (is.null(dollar_context$methods)) new.env(parent = emptyenv()) else dollar_context$methods
    default_dollar <- function(current) {
        if (isS4(current) || is.null(registered)) return(FALSE)
        classes <- attr(current, "class", exact = TRUE)
        if (!is.null(classes) && (!is.character(classes) || anyNA(classes))) return(FALSE)
        if (length(classes)) classes <- c(classes, "default")
        for (class in classes) {
            method <- paste0("$.", class)
            present <- get0(method, dollar_methods, inherits = FALSE)
            if (is.null(present)) {
                present <- FALSE
                for (scope in method_scopes) {
                    if (!method %in% scope$names) next
                    record <- member_binding(scope$env, method)
                    if (!identical(record[[1L]], "value") || is.function(record[[2L]])) {
                        present <- TRUE
                        break
                    }
                }
                assign(method, present, dollar_methods)
            }
            if (present) return(FALSE)
        }
        TRUE
    }
    captures <- function(fn) {
        scope <- environment(fn)
        if (is.null(scope) || isNamespace(scope) || identical(scope, globalenv()) ||
                identical(scope, baseenv())) {
            return(list())
        }
        nms <- ls(scope, all.names = TRUE)
        if (length(nms) > 100L) {
            return(list())
        }
        values <- list()
        for (name in nms) {
            record <- member_binding(scope, name)
            x <- record[[2L]]
            if (identical(record[[1L]], "value") && !is.object(x) &&
                    is.atomic(x) && length(x) <= 100L) {
                values[name] <- list(x)
            }
        }
        values
    }
    r6_cache <- new.env(parent = emptyenv())
    r6_cache$receivers <- r6_cache$scopes <- list()
    r6_method_scopes <- function(receiver) {
        cached <- which(vapply(r6_cache$receivers, identical, logical(1L), receiver))
        if (length(cached)) return(r6_cache$scopes[[cached[[1L]]]])
        record <- member_binding(receiver, ".__enclos_env__")
        queue <- if (identical(record[[1L]], "value") && is.environment(record[[2L]])) list(record[[2L]]) else list()
        scopes <- list()
        checked <- 0L
        next_scope <- 1L
        while (next_scope <= length(queue) && length(scopes) < 32L) {
            scope <- queue[[next_scope]]
            next_scope <- next_scope + 1L
            if (any(vapply(scopes, identical, logical(1L), scope))) next
            self <- member_binding(scope, "self")
            if (!identical(self[[1L]], "value") || !identical(self[[2L]], receiver)) next
            scopes[[length(scopes) + 1L]] <- scope
            if (checked >= 5000L) next
            super <- member_binding(scope, "super")
            if (!identical(super[[1L]], "value") || !is.environment(super[[2L]])) next
            for (name in utils::head(ls(super[[2L]], all.names = TRUE), 5000L - checked)) {
                checked <- checked + 1L
                method <- member_binding(super[[2L]], name)
                if (identical(method[[1L]], "value") && typeof(method[[2L]]) == "closure") {
                    enclosing <- environment(method[[2L]])
                    if (!any(vapply(queue, identical, logical(1L), enclosing))) {
                        queue[[length(queue) + 1L]] <- enclosing
                    }
                }
            }
        }
        key <- length(r6_cache$receivers) + 1L
        r6_cache$receivers[key] <- list(receiver)
        r6_cache$scopes[key] <- list(scopes)
        scopes
    }
    visit <- function(current, depth, r6_receiver = NULL) {
        classes <- attr(current, "class", exact = TRUE)
        r6_instance <- "R6" %in% classes && !"R6ClassGenerator" %in% classes
        if (r6_instance) r6_receiver <- current
        kind <- if (r6_instance) "r6_instance" else "environment"
        old <- which(vapply(seen, identical, logical(1L), current))
        if (length(old)) {
            return(list(kind = "reference", id = old[[1L]]))
        }
        id <- length(seen) + 1L
        seen[[id]] <<- current
        if (depth > max_depth) {
            return(list(kind = kind, id = id, complete = FALSE))
        }
        nms <- ls(current, all.names = TRUE)
        if (length(nms) > remaining) {
            return(list(kind = kind, id = id, complete = FALSE))
        }
        remaining <<- remaining - length(nms)
        fields <- stats::setNames(lapply(nms, function(name) {
            record <- member_binding(current, name)
            value <- record[[2L]]
            field <- if (identical(record[[1L]], "active")) {
                list(
                    kind = "active", syntax = if (typeof(value) == "closure") function_syntax(value),
                    captures = if (typeof(value) == "closure") captures(value) else list()
                )
            } else if (identical(record[[1L]], "value")) {
                receiver <- if (typeof(value) == "closure" ||
                        (is.environment(value) && name %in% c(".__enclos_env__", "private", "super"))) r6_receiver
                describe(value, depth + 1L, receiver, name)
            } else {
                list(kind = "unknown", reason = "promise_not_forced")
            }
            if (!is.null(r6_receiver)) field$locked <- bindingIsLocked(name, current)
            field
        }), nms)
        list(
            kind = kind, id = id, complete = TRUE, classes = classes, fields = fields,
            default_dollar = default_dollar(current),
            r6_locked = if (!is.null(r6_receiver)) environmentIsLocked(current) else NULL
        )
    }
    describe <- function(value, depth, r6_receiver = NULL, binding_name = NULL) {
        if (missing(value) || identical(value, quote(expr = ))) return(list(kind = "unknown", reason = "missing_value"))
        descriptor <- member_s7_instance_descriptor(value, s7_modes)
        if (!is.null(descriptor)) {
            if (depth > max_depth) return(list(kind = "unknown", reason = "snapshot_depth"))
            slots <- list()
            for (name in names(descriptor$properties)) {
                property <- descriptor$properties[[name]]
                if (property$getter || property$setter) next
                # Reserved base attributes use underscore storage in S7 1.0.
                storage <- if (!is.null(attr(value, "_S7_class", exact = TRUE)) && name %in% c(
                    "names", "dim", "dimnames", "class", "tsp", "comment", "row.names"
                )) paste0("_", name) else name
                stored <- attr(value, storage, exact = TRUE)
                if (!is.null(stored)) slots[name] <- list(describe(stored, depth + 1L))
            }
            return(list(kind = "s7_instance", descriptor = descriptor, slots = slots))
        }
        descriptor <- member_s7_runtime_descriptor(value, modes = s7_modes)
        if (!is.null(descriptor)) return(list(kind = "s7", descriptor = descriptor))
        snapshot <- member_s4_instance_snapshot(value, describe, depth, max_depth, lookup_scope,
            reserve = function(n) {
                if (n > remaining) return(FALSE)
                remaining <<- remaining - n
                TRUE
            })
        if (!is.null(snapshot)) return(snapshot)
        if (typeof(value) == "closure") {
            scope <- environment(value)
            self <- if (!is.null(r6_receiver) && is.environment(scope)) member_binding(scope, "self")
            bound <- identical(self[[1L]], "value") && identical(self[[2L]], r6_receiver) &&
                any(vapply(r6_method_scopes(r6_receiver), identical, logical(1L), scope))
            stub_clone <- ((bound && identical(binding_name, "clone")) ||
                    (is.environment(scope) && "R6ClassGenerator" %in% attr(scope, "class", exact = TRUE))) &&
                member_r6_generated_clone(value)
            syntax <- function_syntax(if (stub_clone) member_r6_clone_stub() else value)
            contexts <- list()
            if (bound) {
                for (name in intersect(c("private", "super"), member_syntax_names(member_function_syntax(value)))) {
                    record <- member_binding(scope, name)
                    if (identical(record[[1L]], "value")) {
                        contexts[name] <- list(describe(record[[2L]], depth + 1L, r6_receiver, name))
                    }
                }
            }
            list(kind = "function", syntax = syntax, captures = captures(value),
                r6_bound = bound, r6_contexts = contexts,
                r6_classes = if (bound) attr(r6_receiver, "class", exact = TRUE),
                r6_receiver = if (bound) which(vapply(seen, identical, logical(1L), r6_receiver))[[1L]])
        } else if (is.environment(value)) {
            # Namespace registries can refer back to whole package namespaces.
            # Apply the root exclusion recursively rather than copying those
            # large scopes through every private registry reference.
            if (isNamespace(value) || identical(value, globalenv()) || identical(value, baseenv())) {
                return(list(kind = "unknown", reason = "namespace_environment"))
            }
            visit(value, depth, r6_receiver)
        } else if (!is.object(value) && is.atomic(value) && length(value) <= 100L) {
            list(kind = "literal", value = value)
        } else if (typeof(value) %in% c("list", "pairlist") && !isS4(value) && depth <= max_depth) {
            storage <- member_list_storage(value)
            if (length(storage) > remaining) return(list(kind = "unknown", reason = "snapshot_budget"))
            remaining <<- remaining - length(storage)
            list(
                kind = "list", type = typeof(value), elements = lapply(storage, describe, depth + 1L),
                classes = attr(value, "class", exact = TRUE), default_dollar = default_dollar(value)
            )
        } else {
            list(kind = "unknown", reason = "unsupported_storage")
        }
    }
    describe(env, 0L)
}

member_snapshot_shape <- function(snapshot, package = "") {
    records <- list()
    collect <- function(record) {
        if (record$kind %in% c("environment", "r6_instance")) {
            records[[as.character(record$id)]] <<- record
            for (field in record$fields) collect(field)
        } else if (identical(record$kind, "list")) {
            for (field in record$elements) collect(field)
        } else if (record$kind %in% c("s7_instance", "s4_instance")) {
            for (field in record$slots) collect(field)
        } else if (identical(record$kind, "function")) {
            for (context in record$r6_contexts) collect(context)
        }
    }
    collect(snapshot)
    # A bound R6 method refers to the private and super environments of its
    # instance. Decoding a copy into every method (or every trail subset) is
    # quadratic in width and geometric in inheritance depth, so each distinct
    # context environment is decoded once per instance (`r6_contexts`) with a
    # local canonical key, while intra-instance context cycles are cut lazily
    # along the active context trail in member_access().
    register_context <- function(state, stored, name, class) {
        raw <- paste(name, stored$id, if (is.null(class$name)) "" else class$name, sep = ":")
        key <- member_lookup(state$keys, raw)
        if (is.null(key)) {
            count <- if (is.null(state$counts[[name]])) 0L else state$counts[[name]]
            count <- count + 1L
            state$counts[[name]] <- count
            key <- paste0(name, ":", count)
            state$keys[[raw]] <- key
            state$requests[[length(state$requests) + 1L]] <- list(
                key = key, stored = stored, name = name, class = class
            )
        }
        key
    }
    decode_context <- function(stored, name, class, trail, receiver = NULL) {
        context <- decode(stored, trail, receiver)
        if (identical(stored$kind, "reference")) stored <- records[[as.character(stored$id)]]
        context$r6_class <- class
        context$r6_role <- name
        context$r6_locked <- isTRUE(stored$r6_locked)
        fields <- stored$fields
        context$r6_bindings <- stats::setNames(vapply(fields, function(field) {
            if (identical(field$kind, "active")) {
                "active"
            } else if (isTRUE(field$locked)) {
                if (isTRUE(field$r6_bound)) "method" else "locked"
            } else {
                "data"
            }
        }, character(1L)), names(fields))
        context
    }
    # `receiver` holds per-instance context-decoding state for the enclosing
    # R6 instance, passed down through its bound methods and R6 scopes.
    describe_shape <- function(record, trail = integer(), receiver = NULL) {
        if (identical(record$kind, "reference")) {
            if (record$id %in% trail) {
                return(member_value(reason = "cycle"))
            }
            return(decode(records[[as.character(record$id)]], trail, receiver))
        }
        if (identical(record$kind, "function")) {
            value <- member_value(
                type = "function", function_expr = record$syntax, closure = lapply(record$captures, member_literal)
            )
            if (isTRUE(record$r6_bound)) {
                classes <- setdiff(record$r6_classes, "R6")
                class <- list(name = if (length(classes)) classes[[1L]] else NULL, package = package)
                value$receiver_name <- "self"
                value$r6_static_bound <- TRUE
                private <- member_lookup(record$r6_contexts, "private")
                if (identical(private$kind, "reference") && identical(private$id, utils::tail(trail, 1L))) {
                    value$r6_private_self <- TRUE
                }
                value$closure$self <- NULL
                shared <- !is.null(receiver) && identical(record$r6_receiver, receiver$id)
                for (name in names(record$r6_contexts)) {
                    stored <- record$r6_contexts[[name]]
                    if (shared && stored$kind %in% c("reference", "environment", "r6_instance")) {
                        value$r6_context_ids[name] <- register_context(receiver, stored, name, class)
                    } else {
                        value$closure[name] <- list(decode_context(stored, name, class, trail))
                    }
                }
            }
            return(value)
        }
        if (identical(record$kind, "literal")) {
            return(member_literal(record$value))
        }
        if (record$kind %in% c("environment", "r6_instance")) {
            if (record$id %in% trail || !isTRUE(record$complete)) {
                return(member_value(reason = "cycle_or_budget"))
            }
            instance <- identical(record$kind, "r6_instance")
            if (instance) {
                state <- new.env(parent = emptyenv())
                state$id <- record$id
                state$trail <- c(trail, record$id)
                state$keys <- character()
                state$counts <- list()
                state$requests <- list()
                receiver <- state
            }
            fields <- record$fields
            for (i in seq_along(fields)) {
                field <- fields[[i]]
                bound <- identical(field$kind, "function") ||
                    (names(fields)[[i]] %in% c(".__enclos_env__", "private", "super") &&
                            field$kind %in% c("reference", "environment"))
                fields[i] <- list(decode(field, c(trail, record$id), if (bound) receiver))
            }
            value <- member_value(
                type = "environment", classes = record$classes,
                fields = fields,
                default_dollar = record$default_dollar
            )
            if (!instance) return(value)
            value <- member_r6_instance_shape(value, record, package)
            # Decoding a context can register further ones; each is decoded once.
            next_request <- 1L
            while (next_request <= length(receiver$requests)) {
                request <- receiver$requests[[next_request]]
                next_request <- next_request + 1L
                value$r6_contexts[request$key] <- list(decode_context(
                    request$stored, request$name, request$class, receiver$trail, receiver))
            }
            return(value)
        }
        if (identical(record$kind, "s4_instance")) return(member_s4_snapshot_shape(record, decode, trail))
        if (identical(record$kind, "s7_instance")) {
            value <- member_s7_shape(record$descriptor)
            for (name in names(record$slots)) value$slots[name] <- list(decode(record$slots[[name]], trail))
            return(value)
        }
        if (identical(record$kind, "s7")) {
            return(if (identical(record$descriptor$kind, "class")) member_s7_generator(record$descriptor) else
                    member_s7_value(type = "S7_descriptor", s7_descriptor = record$descriptor))
        }
        if (identical(record$kind, "list")) {
            values <- lapply(record$elements, decode, trail)
            fields <- if (is.null(names(values))) list() else values[!is.na(names(values)) & nzchar(names(values))]
            named <- length(values) && !is.null(names(values)) && !anyNA(names(values)) && all(nzchar(names(values)))
            return(member_value(
                type = if (is.null(record$type)) "list" else record$type, fields = fields,
                elements = if (named) NULL else values,
                classes = record$classes, default_dollar = record$default_dollar,
                named_elements = isTRUE(named),
                column_names_only = "data.frame" %in% record$classes && !isTRUE(record$default_dollar)
            ))
        }
        member_value(reason = if (identical(record$kind, "active")) "getter_not_invoked" else record$reason)
    }
    decode <- function(record, trail = integer(), receiver = NULL) {
        value <- describe_shape(record, trail, receiver)
        if (nzchar(package)) {
            if (!is.null(value$function_expr)) value$metadata <- package
            if (!is.null(value$fields) || !is.null(value$slot_types)) value$member_package <- package
        }
        # Namespace values can repeat hundreds of small structures. Optional
        # NULL fields and duplicate named elements need no serialized storage.
        value[!vapply(value, is.null, logical(1L))]
    }
    decode(snapshot)
}

member_source_input <- function(root) {
    definitions <- locations <- expressions <- list()
    for (path in package_metadata_sort(list.files(file.path(root, "R"), "\\.[Rr]$", full.names = TRUE))) {
        code <- parse(path, keep.source = FALSE)
        expressions[[length(expressions) + 1L]] <- code
        for (expr in code) {
            if (!member_head(expr, "<-") || length(expr) != 3L) next
            key <- member_key(expr[[2L]])
            if (is.null(key)) next
            definitions[key] <- list(expr[[3L]])
            locations[key] <- list(basename(path))
        }
    }
    exports <- intrinsics <- character()
    providers <- list(rlang = c(
        "list2", "arg_match0", "arg_match", "try_fetch",
        "is_character", "is_list", "is_bool", "abort"
    ), S7 = member_s7_intrinsics, methods = member_methods_intrinsics)
    for (expr in parse(file.path(root, "NAMESPACE"))) {
        if (member_head(expr, "import") || member_head(expr, "importFrom")) {
            provider <- member_name(expr[[2L]])
            allowed <- member_lookup(providers, provider)
            if (member_head(expr, "importFrom")) {
                allowed <- intersect(
                    allowed,
                    vapply(as.list(expr)[-c(1L, 2L)], member_name, character(1L))
                )
            }
            intrinsics <- union(intrinsics, setdiff(allowed, names(definitions)))
        }
        if (member_head(expr, "export")) {
            exports <- c(
                exports,
                vapply(as.list(expr)[-1L], member_name, character(1L))
            )
        }
    }
    list(
        definitions = definitions, locations = locations, expressions = expressions,
        exports = exports, intrinsics = intrinsics, package = read.dcf(file.path(root, "DESCRIPTION"))[[1L, "Package"]]
    )
}

member_namespace_lazydata <- function(ns) {
    if (!isNamespace(ns)) return(NULL)
    info <- member_binding(ns, ".__NAMESPACE__.")
    if (!identical(info[[1L]], "value") || !is.environment(info[[2L]])) return(NULL)
    record <- member_binding(info[[2L]], "lazydata")
    if (identical(record[[1L]], "value") && is.environment(record[[2L]])) record[[2L]] else NULL
}

member_namespace_export_targets <- function(ns, exports) {
    targets <- stats::setNames(exports, exports)
    if (!isNamespace(ns)) return(targets)
    info <- member_binding(ns, ".__NAMESPACE__.")
    if (!identical(info[[1L]], "value") || !is.environment(info[[2L]])) return(targets)
    record <- member_binding(info[[2L]], "exports")
    if (!identical(record[[1L]], "value") || !is.environment(record[[2L]])) return(targets)
    for (name in exports) {
        binding <- member_binding(record[[2L]], name)
        target <- if (identical(binding[[1L]], "value")) member_name(binding[[2L]]) else NULL
        if (!is.null(target)) targets[[name]] <- target
    }
    targets
}

# Lazy datasets are public through :: without being ordinary namespace
# exports. Keep their surfaces separate so a private binding with the same
# name remains private and an explicit namespace export retains precedence.
member_public_roots <- function(index, input) {
    exports <- if (is.null(input$namespace_exports)) index$exports else input$namespace_exports
    roots <- index$package_roots[intersect(exports, names(index$package_roots))]
    for (name in names(input$namespace_export_targets)) {
        target <- input$namespace_export_targets[[name]]
        value <- if (name %in% exports) member_lookup(index$package_roots, target) else NULL
        if (is.null(value)) value <- member_lookup(input$lazydata_roots, target)
        if (!is.null(value)) roots[name] <- list(value)
    }
    blocked <- if (is.null(input$namespace_lazydata_blocked)) exports else input$namespace_lazydata_blocked
    lazy <- input$lazydata_roots[setdiff(names(input$lazydata_roots), union(blocked, names(roots)))]
    c(roots, lazy)
}

member_namespace_input <- function(
    ns, package = unname(getNamespaceName(ns)),
    exports = getNamespaceExports(ns), lazydata = member_namespace_lazydata(ns),
    lazydata_only = FALSE
) {
    namespace_names <- package_metadata_sort(ls(ns, all.names = TRUE, sorted = FALSE))
    targets <- member_namespace_export_targets(ns, exports)
    blocked <- names(targets)[targets %in% namespace_names | targets != names(targets)]
    nms <- if (isTRUE(lazydata_only)) character() else namespace_names
    namespace_exports <- if (isTRUE(lazydata_only)) character() else exports
    lazy_names <- if (is.environment(lazydata)) {
        setdiff(package_metadata_sort(ls(lazydata, all.names = TRUE, sorted = FALSE)), blocked)
    } else {
        character()
    }
    if (length(nms) + length(lazy_names) > 10000L) stop("Namespace exceeds metadata budget")
    definitions <- snapshots <- registries <- links <- descriptors <- s4_classes <- s4_roots <- s4_objects <- list()
    generic_external <- character()
    s7_roots <- list()
    s7_modes <- member_s7_capabilities()
    functions <- values <- list()
    for (name in nms) {
        record <- member_binding(ns, name, materialize = TRUE)
        value <- record[[2L]]
        if (!identical(record[[1L]], "value")) next
        descriptor <- member_s7_runtime_descriptor(value, modes = s7_modes)
        if (!is.null(descriptor)) {
            s7_roots[name] <- list(if (identical(descriptor$kind, "class")) {
                member_s7_generator(descriptor)
            } else {
                member_s7_value(type = "S7_descriptor", s7_descriptor = descriptor)
            })
        } else if ("S7_object" %in% attr(value, "class", exact = TRUE)) {
            descriptor <- member_s7_instance_descriptor(value, s7_modes)
            if (!is.null(descriptor)) {
                holder <- new.env(parent = emptyenv())
                holder$object <- value
                s7_roots[name] <- list(member_snapshot_shape(member_snapshot(
                    holder, s7_modes = s7_modes, lookup_scope = ns
                ))$fields$object)
            }
        }
        if (isS4(value) && any(c("classRepresentation", "ClassUnionRepresentation") %in%
                    attr(value, "class", exact = TRUE))) {
            descriptor <- member_s4_runtime_descriptor(value)
            if (!is.null(descriptor)) s4_classes[descriptor$name] <- list(descriptor)
            next
        }
        s4_instance <- isS4(value) && !"classGeneratorFunction" %in% attr(value, "class", exact = TRUE) &&
            is.null(s7_roots[[name]])
        if (s4_instance) {
            classes <- attr(value, "class", exact = TRUE)
            if (is.character(classes) && length(classes) == 1L) {
                owner <- attr(classes, "package", exact = TRUE)
                s4_objects[name] <- list(list(name = as.character(classes), package = if (is.null(owner)) package else owner))
            }
        }
        if (s4_instance) values[name] <- list(value)
        if (typeof(value) == "closure") {
            if ("classGeneratorFunction" %in% attr(value, "class", exact = TRUE)) {
                class <- attr(value, "className", exact = TRUE)
                if (is.character(class) && length(class) == 1L) {
                    owner <- attr(class, "package", exact = TRUE)
                    s4_roots[name] <- list(member_value(type = "function", s4_generator = list(
                        name = as.character(class), package = if (is.null(owner)) package else owner
                    )))
                }
            }
            if ("S7_class" %in% attr(value, "class", exact = TRUE)) {
                type <- attr(value, "name", exact = TRUE)
                properties <- attr(value, "properties", exact = TRUE)
                if (is.character(type) && length(type) == 1L && is.list(properties)) {
                    descriptors[name] <- list(list(type = type, fields = names(properties)))
                }
            }
            definitions[name] <- list(member_function_syntax(value))
            functions[name] <- list(value)
        } else if (is.environment(value) || typeof(value) %in% c("list", "pairlist") || isS4(value)) {
            values[name] <- list(value)
        } else if (!is.object(value) && is.atomic(value) && length(value) <= 256L) {
            definitions[name] <- list(value)
        }
    }
    # Match bindings by identity, not a method-name prefix. Retain namespace
    # function IDs for documentation; anonymous registry closures get path IDs.
    function_match <- NULL
    remaining_bindings <- 50000L
    dollar_context <- member_snapshot_dollar_context(ns)
    for (name in names(values)) {
        value <- values[[name]]
        if (is.environment(value) && (isNamespace(value) || identical(value, ns) || identical(value, globalenv()) ||
                    identical(value, baseenv()))) {
            next
        }
        storage <- if (is.environment(value)) value else if (!isS4(value)) member_list_storage(value) else NULL
        size <- if (is.environment(value)) {
            length(ls(value, all.names = TRUE))
        } else if (isS4(value)) {
            length(attributes(value))
        } else {
            length(storage)
        }
        if (size > remaining_bindings) {
            if (is.environment(value)) stop("Namespace exceeds registry budget")
            snapshots[name] <- list(list(kind = "unknown", reason = "snapshot_budget"))
            registries[name] <- list(character())
            next
        }
        remaining_bindings <- remaining_bindings - size
        snapshots[name] <- list(member_snapshot(value,
                max_bindings = remaining_bindings + size, s7_modes = s7_modes, lookup_scope = ns,
                dollar_context = dollar_context))
        definitions[name] <- list(if (is.environment(value)) quote(new.env(parent = emptyenv())) else quote(list()))
        methods <- character()
        fields <- if (snapshots[[name]]$kind %in% c("environment", "r6_instance")) {
            snapshots[[name]]$fields
        } else if (identical(snapshots[[name]]$kind, "list")) {
            snapshots[[name]]$elements
        } else {
            list()
        }
        for (field in names(fields)[!is.na(names(fields)) & nzchar(names(fields))]) {
            if (identical(fields[[field]]$reason, "missing_value")) next
            record <- if (is.environment(value)) member_binding(value, field) else list("value", storage[[field]])
            fn <- record[[2L]]
            if (identical(record[[1L]], "value") && (is.environment(fn) || typeof(fn) == "list")) {
                matches <- which(vapply(values, identical, logical(1L), fn))
                if (length(matches)) links[[name]][field] <- names(values)[[matches[[1L]]]]
            }
            if (!identical(record[[1L]], "value") || typeof(fn) != "closure") next
            if (is.null(function_match)) function_match <- member_function_identity_index(functions)
            key <- function_match(fn)
            if (is.null(key)) key <- paste(name, field, sep = "$")
            stub_fn <- (is.environment(value) && "R6ClassGenerator" %in% attr(value, "class", exact = TRUE) &&
                    member_r6_generated_clone(fn, value)) ||
                (identical(snapshots[[name]]$kind, "r6_instance") && identical(field, "clone") &&
                        isTRUE(fields[[field]]$r6_bound) && member_r6_generated_clone(fn))
            fn_syntax <- member_function_syntax(if (stub_fn) member_r6_clone_stub() else fn)
            definitions[key] <- list(fn_syntax)
            # A static factory may also be addressed by its environment path.
            definitions[paste(name, field, sep = "$")] <- list(as.name(key))
            if (identical(key, paste(name, field, sep = "$"))) definitions[key] <- list(fn_syntax)
            methods[field] <- key
        }
        registries[name] <- list(methods)
    }
    # Imported intrinsic summaries are allowed only for the actual provider's
    # binding, not arbitrary functions with a matching local name.
    intrinsics <- character()
    imports <- parent.env(ns)
    providers <- list(rlang = c(
        "list2", "arg_match0", "arg_match", "try_fetch",
        "is_character", "is_list", "is_bool", "abort"
    ), S7 = member_s7_intrinsics, methods = member_methods_intrinsics)
    for (provider in if (isTRUE(lazydata_only)) character() else names(providers)) {
        if (!provider %in% loadedNamespaces()) next
        for (name in providers[[provider]]) {
            if (name %in% names(definitions)) next
            record <- member_binding(imports, name, materialize = TRUE)
            source <- member_binding(asNamespace(provider), name, materialize = TRUE)
            if (identical(record[[1L]], "value") && identical(source[[1L]], "value") &&
                    identical(record[[2L]], source[[2L]])) {
                intrinsics <- c(intrinsics, name)
            }
        }
    }
    lazy_roots <- stats::setNames(lapply(lazy_names, function(name) {
        tryCatch({
            record <- member_binding(lazydata, name, materialize = TRUE)
            snapshot <- if (identical(record[[1L]], "value")) {
                member_snapshot(record[[2L]], max_bindings = 5000L, s7_modes = s7_modes, lookup_scope = ns,
                    dollar_context = dollar_context)
            } else {
                list(kind = "unknown", reason = "deferred_dataset")
            }
            member_snapshot_shape(snapshot, package)
        }, error = function(e) member_snapshot_shape(list(kind = "unknown", reason = "dataset_metadata_error"), package))
    }), lazy_names)
    subsets <- member_subset_registry(ns, package)
    table <- member_binding(ns, ".__S3MethodsTable__.")[[2L]]
    if (is.environment(table)) {
        methods <- package_metadata_sort(ls(table, all.names = TRUE, sorted = FALSE))
        for (name in intersect(exports, names(definitions))) {
            generic <- s3_generic_descriptor(member_definition(definitions, name))
            if (is.null(generic)) next
            for (method in methods[startsWith(methods, paste0(generic$name, "."))]) {
                record <- member_binding(table, method, materialize = TRUE)
                if (!identical(record[[1L]], "value") || typeof(record[[2L]]) != "closure" ||
                        !identical(record[[2L]], functions[[method]])) {
                    generic_external <- union(generic_external, generic$name)
                }
            }
        }
    }
    list(
        definitions = definitions, locations = list(), expressions = list(),
        exports = union(namespace_exports, union(lazy_names, names(targets)[targets %in% names(lazy_roots)])),
        namespace_exports = namespace_exports, namespace_export_targets = targets,
        namespace_lazydata_blocked = blocked,
        lazydata_roots = lazy_roots, registries = registries,
        subset_registry = subsets$methods, subset_registry_complete = subsets$complete, generic_external = generic_external,
        snapshots = snapshots, links = links, descriptors = descriptors, package = package, intrinsics = intrinsics,
        s4_classes = s4_classes, s4_roots = s4_roots, s4_objects = s4_objects,
        s7_roots = s7_roots, s7_capabilities = s7_modes,
        s7_dependencies = member_s7_dependencies(s7_roots, package, s7_modes),
        s4_dependencies = member_s4_dependencies(s4_classes, lapply(c(
            lapply(s4_roots, function(value) value$s4_generator), s4_objects
        ), function(class) structure(class$name, package = class$package)))
    )
}

member_index_freeze <- function(index) {
    value <- as.list(index)
    value$cache <- NULL
    # Prepare a cheap request-time hint in the metadata worker. Check syntax
    # and already inferred roots, including runtime R6 generators.
    value$class_scope <- isTRUE(value$r6_attached) ||
        any(vapply(value$definitions, function(expr) "R6Class" %in% member_syntax_names(expr), logical(1L))) ||
        any(vapply(value$package_roots, function(root) !is.null(root$r6_class), logical(1L)))
    value$symbol_scope <- value$class_scope ||
        any(vapply(value$definitions, function(expr) any(member_syntax_names(expr) %in% member_s7_intrinsics), logical(1L))) ||
        any(vapply(value$package_roots, function(root) !is.null(root$s7_generator), logical(1L)))
    value$generation <- digest::digest(value, algo = "sha256")
    value$schema <- 1L
    if (as.numeric(utils::object.size(value)) > 32 * 1024^2) stop("Namespace exceeds snapshot budget")
    value
}

member_index_thaw <- function(snapshot) {
    if (!is.list(snapshot) || !identical(snapshot$schema, 1L)) {
        return(NULL)
    }
    index <- list2env(snapshot, parent = emptyenv())
    if (!is.null(index$return_definitions) &&
            !identical(index$return_definitions, digest::digest(index$definitions, algo = "xxhash64"))) {
        index$method_results <- list()
    }
    index$cache <- new.env(parent = emptyenv())
    index
}

member_prunable_r6_generators <- function(index, input, generators) {
    generators <- unique(as.character(generators))
    if (!length(generators)) {
        return(character())
    }
    gen_env <- new.env(hash = TRUE, parent = emptyenv())
    for (g in generators) if (!is.na(g) && nzchar(g)) gen_env[[g]] <- TRUE
    kept_gen_env <- new.env(hash = TRUE, parent = emptyenv())
    gen_queue <- character()
    mark_gen <- function(types) {
        if (!is.character(types) || !length(types)) return(invisible(NULL))
        for (g in types) {
            if (!is.na(g) && nzchar(g) && !is.null(gen_env[[g]]) && is.null(kept_gen_env[[g]])) {
                kept_gen_env[[g]] <- TRUE
                gen_queue[[length(gen_queue) + 1L]] <<- g
            }
        }
    }
    scan_s7 <- function(desc, depth = 0L) {
        if (!is.list(desc) || depth > 32L) return(invisible(NULL))
        if (is.character(desc$name) && identical(desc$kind, "base")) mark_gen(desc$name)
        if (is.list(desc$parent)) scan_s7(desc$parent, depth + 1L)
        for (cls in desc$classes) scan_s7(cls, depth + 1L)
        for (prop in desc$properties) {
            if (is.list(prop)) scan_s7(prop$class, depth + 1L)
        }
    }
    scan_shape <- function(x, depth = 0L) {
        if (!is.list(x) || is.object(x) || depth > 32L) return(invisible(NULL))
        if (is.character(x$type)) mark_gen(x$type)
        if (is.list(x$s7_descriptor)) scan_s7(x$s7_descriptor, depth + 1L)
        if (is.list(x$s7_generator)) scan_s7(x$s7_generator, depth + 1L)
        if (is.list(x$s7_property)) scan_s7(x$s7_property$class, depth + 1L)
        for (child in x$fields) scan_shape(child, depth + 1L)
        for (child in x$slots) scan_shape(child, depth + 1L)
        for (child in x$elements) scan_shape(child, depth + 1L)
        for (child in x$possible_returns) scan_shape(child, depth + 1L)
        for (child in x$closure) scan_shape(child, depth + 1L)
        for (child in x$binding_env) scan_shape(child, depth + 1L)
        for (child in x$r6_contexts) scan_shape(child, depth + 1L)
        for (layer in x$r6_layers) {
            if (is.list(layer)) {
                for (child in layer$public) scan_shape(child, depth + 1L)
                for (child in layer$private) scan_shape(child, depth + 1L)
            }
        }
        for (single in c("receiver", "receiver_value", "element_shape", "result_shape",
                "r6_private", "r6_super", "r6_self", "r6_initialize")) {
            child <- x[[single]]
            if (is.list(child)) scan_shape(child, depth + 1L)
        }
    }
    scan_property <- function(prop) {
        if (is.character(prop)) mark_gen(prop) else scan_shape(prop)
    }
    non_g_links <- setdiff(names(input$links), generators)
    mark_gen(unlist(input$links[non_g_links], use.names = FALSE))
    s3_keys <- c(as.character(names(index$definitions)), as.character(names(index$subset_registry)))
    for (g in generators) {
        if (any(paste0(c("$.", "[[.", "[."), g) %in% s3_keys)) mark_gen(g)
    }
    mark_gen(c(names(index$classes), unlist(index$classes, use.names = FALSE)))
    mark_gen(c(names(index$namespace_owners), unlist(index$namespace_owners, use.names = FALSE)))
    mark_gen(c(names(index$raw_fields), unlist(index$raw_fields, use.names = FALSE)))
    mark_gen(names(index$s4_classes))
    for (s4_desc in index$s4_classes) {
        mark_gen(c(s4_desc$name, unlist(s4_desc$slots, use.names = FALSE), unlist(s4_desc$union, use.names = FALSE)))
    }
    for (ctor in index$constructors) mark_gen(c(ctor$type, ctor$classes))
    mark_gen(unlist(index$constructor_types, use.names = FALSE))
    for (ci in index$constructor_inputs) for (v in ci) scan_shape(v)
    for (rule in index$registration_rules) mark_gen(rule$owners)
    for (owner in setdiff(names(index$properties), generators)) {
        for (prop in index$properties[[owner]]) scan_property(prop)
    }
    for (owner in setdiff(names(index$package_roots), generators)) scan_shape(index$package_roots[[owner]])
    for (root in input$lazydata_roots) scan_shape(root)
    for (res in index$method_results) scan_shape(res)
    for (glist in index$generic_returns) for (res in glist) scan_shape(res)
    gen_idx <- 1L
    while (gen_idx <= length(gen_queue)) {
        g <- gen_queue[[gen_idx]]
        gen_idx <- gen_idx + 1L
        mark_gen(input$links[[g]])
        for (prop in index$properties[[g]]) scan_property(prop)
    }
    generators[vapply(generators, function(g) is.null(kept_gen_env[[g]]), logical(1L))]
}

member_prepare_r6_roots <- function(index, input, ns) {
    generator_envs <- list()
    for (name in names(index$package_roots)) {
        record <- member_binding(ns, name)
        value <- record[[2L]]
        if (identical(record[[1L]], "value") && is.environment(value) &&
                "R6ClassGenerator" %in% attr(value, "class", exact = TRUE)) {
            index$package_roots[name] <- list(member_r6_runtime_shape(value, index))
            generator_envs[name] <- list(value)
        }
    }
    generators <- names(generator_envs)
    if (!length(generators)) return(invisible(index))
    prunable_gens <- member_prunable_r6_generators(index, input, generators)
    for (name in prunable_gens) {
        index$properties[[name]] <- NULL
        index$registries[[name]] <- NULL
        if (name %in% names(index$members)) index$members[name] <- list(character())
    }
    if (length(prunable_gens)) {
        mres_names <- as.character(names(index$method_results))
        mrec_names <- as.character(names(index$method_receivers))
        del_orig <- vapply(index$delegation, function(d) as.character(d$original), character(1L))
        protected <- unique(c(
            package_metadata_sort(ls(ns, all.names = TRUE, sorted = FALSE)),
            as.character(index$exports),
            as.character(index$native_factories),
            as.character(names(index$constructors)),
            as.character(names(index$constructor_types)),
            as.character(names(index$registration_rules)),
            mres_names,
            sub("^[^|]*\\|", "", mres_names),
            sub("^[^|]*\\|", "", mrec_names),
            unname(del_orig)
        ))
        candidates <- setdiff(as.character(names(index$definitions)), protected)
        snap_names <- as.character(names(input$snapshots))
        canonical <- new.env(hash = TRUE, parent = emptyenv())
        changed <- FALSE
        for (g in prunable_gens) {
            gen <- generator_envs[[g]]
            if (!is.environment(gen)) next
            prefix <- paste0(g, "$")
            keys <- candidates[startsWith(candidates, prefix)]
            if (!length(keys)) next
            longer <- snap_names[startsWith(snap_names, prefix) & nchar(snap_names) > nchar(g)]
            for (other in longer) keys <- keys[!startsWith(keys, paste0(other, "$"))]
            for (def_key in keys) {
                fn_expr <- index$definitions[[def_key]]
                if (is.null(fn_expr) || is.symbol(fn_expr)) next
                field <- substring(def_key, nchar(prefix) + 1L)
                bnd <- member_binding(gen, field)
                if (!identical(bnd[[1L]], "value") || !member_r6_standard_generator_fn(bnd[[2L]], field, gen)) next
                sig <- digest::digest(fn_expr, algo = "xxhash64")
                target <- canonical[[sig]]
                if (is.null(target)) {
                    canonical[[sig]] <- def_key
                } else if (identical(fn_expr, index$definitions[[target]])) {
                    index$definitions[def_key] <- list(as.name(target))
                    changed <- TRUE
                }
            }
        }
        if (changed && !is.null(index$return_definitions)) {
            index$return_definitions <- digest::digest(index$definitions, algo = "xxhash64")
        }
    }
    invisible(index)
}

member_prepare_package <- function(package, lib_paths = .libPaths(), context = NULL, identity = NULL) {
    .libPaths(lib_paths)
    ns <- asNamespace(package)
    if (is.null(identity)) identity <- package_metadata_live_identity(package, lib_paths, context)
    cache_path <- package_metadata_cache_path("members", identity)
    cached <- package_metadata_cache_read(cache_path, identity, function(value) {
        identical(value$identity, identity) && !is.null(member_index_thaw(value))
    })
    if (!is.null(cached)) return(cached)
    prepare <- function(input, preparation_error = NULL) {
        index <- member_package_index(input)
        member_prepare_r6_roots(index, input, ns)
        index$roots <- member_public_roots(index, input)
        index$r6_attached <- identical(package, "R6")
        for (name in names(index$roots)) index$namespace_roots[paste(package, name, sep = "::")] <- list(index$roots[[name]])
        if (!is.null(preparation_error)) {
            index$dataset_only <- TRUE
            index$namespace_attempted <- TRUE
            index$preparation_error <- preparation_error
        }
        index$identity <- identity
        member_index_freeze(index)
    }
    snapshot <- tryCatch(prepare(member_namespace_input(ns)), error = function(e) {
        message <- conditionMessage(e)
        lazydata <- member_namespace_lazydata(ns)
        if (!grepl("^Namespace exceeds .* budget$", message) || !is.environment(lazydata) ||
                !length(ls(lazydata, all.names = TRUE))) stop(e)
        # A large normal namespace must not hide its lightweight public data.
        prepare(member_namespace_input(ns, lazydata = lazydata, lazydata_only = TRUE), message)
    })
    package_metadata_cache_write(cache_path, identity, snapshot)
    snapshot
}

member_prepare_startup_data <- function(
    packages,
    lib_paths = .libPaths(),
    cache_dir = package_metadata_cache_root(),
    include_status = FALSE) {
    .libPaths(lib_paths)
    lib_paths <- package_metadata_startup_lib_paths(lib_paths)
    previous <- options(languageserver.package_cache_dir = cache_dir)
    on.exit(options(previous), add = TRUE)
    target_packages <- unique(as.character(packages))
    target_packages <- target_packages[!is.na(target_packages) & nzchar(target_packages)]
    loaded <- list()
    errors <- list()
    for (package in target_packages) {
        ns <- tryCatch(asNamespace(package), error = function(e) {
            errors[[package]] <<- conditionMessage(e)
            NULL
        })
        if (!is.null(ns)) loaded[[package]] <- ns
    }
    base_required <- unique(c(startup_packages, target_packages))
    base_required <- base_required[!is.na(base_required) & nzchar(base_required)]
    base_traversal <- package_metadata_transitive_packages(base_required, lib_paths)
    identities <- if (isTRUE(base_traversal$complete)) {
        package_metadata_manifest(
            unique(c(base_required, base_traversal$packages)),
            lib_paths
        )
    }
    context <- if (!is.null(identities)) list(packages = unname(identities))
    snapshots <- list()
    inspected <- character()
    for (package in names(loaded)) {
        ns <- loaded[[package]]
        outcome <- tryCatch({
            lazydata <- member_namespace_lazydata(ns)
            snapshot <- if (is.environment(lazydata) && length(ls(lazydata, all.names = TRUE))) {
                identity <- package_metadata_live_identity(package, lib_paths)
                path <- package_metadata_cache_path("startup-members", identity)
                cached <- package_metadata_cache_read(path, identity, function(value) {
                    identical(value$identity, identity) && isTRUE(value$dataset_only) && !is.null(member_index_thaw(value))
                })
                res <- if (!is.null(cached)) {
                    cached
                } else {
                    input <- member_namespace_input(ns, package, lazydata = lazydata, lazydata_only = TRUE)
                    index <- member_package_index(input)
                    index$dataset_only <- TRUE
                    index$identity <- identity
                    value <- member_index_freeze(index)
                    package_metadata_cache_write(path, identity, value)
                    value
                }
                if (!is.null(context)) {
                    res$identity$context <- context
                }
                res
            }
            list(ok = TRUE, snapshot = snapshot)
        }, error = function(e) {
            list(ok = FALSE, error = conditionMessage(e))
        })
        if (isTRUE(outcome$ok)) {
            inspected <- c(inspected, package)
            if (!is.null(outcome$snapshot)) {
                snapshots[package] <- list(outcome$snapshot)
            }
        } else {
            errors[[package]] <- outcome$error
        }
    }
    s4_s7 <- setdiff(unlist(lapply(snapshots, function(snapshot) {
        c(snapshot$s4_dependencies, snapshot$s7_dependencies)
    }), use.names = FALSE), names(identities))
    if (length(s4_s7) && !is.null(identities)) {
        extra_trav <- package_metadata_transitive_packages(s4_s7, lib_paths)
        if (!isTRUE(extra_trav$complete)) {
            identities <- NULL
        } else {
            extra_pkgs <- setdiff(unique(c(s4_s7, extra_trav$packages)), names(identities))
            if (length(extra_pkgs)) {
                extra_id <- package_metadata_manifest(extra_pkgs, lib_paths)
                if (is.null(extra_id)) {
                    identities <- NULL
                } else {
                    identities <- c(identities, extra_id)
                    for (pkg in names(snapshots)) {
                        snapshots[[pkg]]$identity$context <- list(packages = unname(identities))
                    }
                }
            }
        }
    }
    complete <- !length(errors) && all(target_packages %in% inspected) && !is.null(identities)
    attr(snapshots, "identities") <- identities
    if (isTRUE(include_status)) {
        return(list(
            .startup_members_result = TRUE,
            snapshots = snapshots,
            inspected = inspected,
            errors = errors,
            identities = identities,
            complete = complete
        ))
    }
    if (!complete) {
        attr(snapshots, "complete") <- FALSE
        attr(snapshots, "inspected") <- inspected
        attr(snapshots, "errors") <- errors
    }
    snapshots
}

member_resolve_packages <- function(pkgs, lib_paths, prepare = TRUE, namespace_packages = character(), prepare_s3 = TRUE,
    known_members = list(), cache_dir = package_metadata_cache_root()) {
    .libPaths(lib_paths)
    previous <- options(languageserver.package_cache_dir = cache_dir)
    on.exit(options(previous), add = TRUE)
    packages <- resolve_attached_packages(pkgs)
    snapshots <- list()
    if (isTRUE(prepare)) {
        context_packages <- package_metadata_sort(unique(c(packages, namespace_packages)))
        # All requested namespaces must register their methods before any
        # cached model is compared or a new dispatch snapshot is prepared.
        for (package in namespace_packages) tryCatch(asNamespace(package), error = function(e) NULL)
        member_s7_capabilities()
        if ("S7" %in% loadedNamespaces()) context_packages <- package_metadata_sort(union(context_packages, "S7"))
        context <- package_metadata_context(context_packages)
        for (package in unique(c(setdiff(packages, startup_packages), namespace_packages))) {
            identity <- tryCatch(package_metadata_live_identity(package, lib_paths, context), error = function(e) NULL)
            if (isTRUE(context$cacheable) && !is.null(identity) && identical(known_members[[package]], identity)) next
            snapshots[package] <- list(tryCatch(member_prepare_package(package, lib_paths, context, identity),
                error = function(e) {
                    list(
                        schema = 1L, roots = list(), namespace_roots = list(),
                        package = package, generation = "", error = conditionMessage(e)
                    )
                }
            ))
        }
    }
    list(
        packages = packages, members = snapshots,
        s3 = if (isTRUE(prepare_s3)) s3_prepare_packages(unique(c(packages, namespace_packages))) else NULL,
        requested = list(packages = normalize_package_request(pkgs),
            namespace_packages = normalize_package_request(unique(namespace_packages)))
    )
}

Try the languageserver package in your browser

Any scripts or data that you put into this service are public.

languageserver documentation built on Nov. 16, 2026, 9:07 a.m.