Repository: denoland/deno
File: cli/lsp/tsc.rs (Rust)

Code before:
  if scopes_with_node_specifier.contains(&None) {
    result
      .unscoped
      .insert("asset:///node_types.d.ts".to_string());
  }
  for (scope, script_names) in &mut result.by_scope {
    if scopes_with_node_specifier.contains(&Some(scope.clone())) {
      script_names.insert("asset:///node_types.d.ts".to_string());
    }
  }

  // inject these next because they're global
  for (scope, script_names) in &mut result.by_scope {
    for (_, specifiers) in state
      .state_snapshot
      .resolver
      .graph_imports_by_referrer(scope)
    {
      for specifier in specifiers {
        let Ok(resolved) = state
          .state_snapshot
          .resolver
          .as_cli_resolver(Some(scope))
          .resolve(
            specifier.as_str(),
            scope,
            deno_graph::Position::zeroed(),
            ResolutionMode::Import,
            node_resolver::NodeResolutionKind::Types,
          )
          .inspect_err(|e| {
            lsp_log!("failed to resolve compilerOptions.types specifier {specifier}: {e}");
          })
        else {
          continue;
        };

        if let Ok(req_ref) =
          deno_semver::npm::NpmPackageReqReference::from_specifier(&resolved)
        {
          let Some((resolved, _)) =
            state.state_snapshot.resolver.npm_to_file_url(
              &req_ref,
              scope,
              ResolutionMode::Import,
              Some(scope),
            )
          else {
            lsp_log!("failed to resolve {req_ref} to file URL");
            continue;
          };

Review comment (suggestion):
It might make more sense to use the referrer from `graph_imports_by_referrer()` here

Code after:
  if scopes_with_node_specifier.contains(&None) {
    result
      .unscoped
      .insert("asset:///node_types.d.ts".to_string());
  }
  for (scope, script_names) in &mut result.by_scope {
    if scopes_with_node_specifier.contains(&Some(scope.clone())) {
      script_names.insert("asset:///node_types.d.ts".to_string());
    }
  }

  // inject these next because they're global
  for (scope, script_names) in &mut result.by_scope {
    for (_, specifiers) in state
      .state_snapshot
      .resolver
      .graph_imports_by_referrer(scope)
    {
      for specifier in specifiers {
        if let Ok(req_ref) =
          deno_semver::npm::NpmPackageReqReference::from_specifier(specifier)
        {
          let Some((resolved, _)) =
            state.state_snapshot.resolver.npm_to_file_url(
              &req_ref,
              scope,
              ResolutionMode::Import,
              Some(scope),
            )
          else {
            lsp_log!("failed to resolve {req_ref} to file URL");
            continue;
          };
          script_names.insert(resolved.to_string());
        } else {
          script_names.insert(specifier.to_string());
        }
      }
    }
  }

  // finally include the documents
  let docs = state
    .state_snapshot
    .documents
    .documents(DocumentsFilter::AllDiagnosable);
  for doc in &docs {
    let specifier = doc.specifier();
    let is_open = doc.is_open();
    if is_open
      || (specifier.scheme() == "file"