Repository: denoland/deno
File: cli/tools/info.rs (Rust)

Code before:
use crate::graph_util::graph_exit_integrity_errors;
use crate::npm::CliNpmResolver;
use crate::npm::ManagedCliNpmResolver;
use crate::util::checksum;

const JSON_SCHEMA_VERSION: u8 = 1;

pub async fn info(
  flags: Arc<Flags>,
  info_flags: InfoFlags,
) -> Result<(), AnyError> {
  let factory = CliFactory::from_flags(flags);
  let cli_options = factory.cli_options()?;
  if let Some(specifier) = info_flags.file {
    let module_graph_builder = factory.module_graph_builder().await?;
    let module_graph_creator = factory.module_graph_creator().await?;
    let npm_resolver = factory.npm_resolver().await?;
    let maybe_lockfile = cli_options.maybe_lockfile();
    let npmrc = cli_options.npmrc();

    let cwd_url =
      url::Url::from_directory_path(cli_options.initial_cwd()).unwrap();

    let deno_resolver = factory.deno_resolver().await?;
    let maybe_import_specifier =
      if specifier.starts_with("npm:") && cli_options.use_byonm() {
        // Don't resolve this to a file path, just pass the `npm:` specifier along
        // (it will error out later on)
        None
      } else {
        deno_resolver
          .resolve(
            &specifier,
            &cwd_url,
            deno_package_json::NodeModuleKind::Esm,
            node_resolver::NodeResolutionMode::Execution,
          )
          .ok()
          .map(|r| r.url)
      };

    let specifier = match maybe_import_specifier {
      Some(specifier) => specifier,
      None => resolve_url_or_path(&specifier, cli_options.initial_cwd())?,
    };

    let mut loader = module_graph_builder.create_graph_loader();
    loader.enable_loading_cache_info(); // for displaying the cache information
    let graph = module_graph_creator
      .create_graph_with_loader(GraphKind::All, vec![specifier], &mut loader)
      .await?;

Review comment (suggestion):
without this we would resolve `npm:foo` to the actual node_modules file in BYONM mode. wasn't sure if that was desired or not, so added this check to retain the current behavior

Code after:
use crate::graph_util::graph_exit_integrity_errors;
use crate::npm::CliNpmResolver;
use crate::npm::ManagedCliNpmResolver;
use crate::util::checksum;

const JSON_SCHEMA_VERSION: u8 = 1;

pub async fn info(
  flags: Arc<Flags>,
  info_flags: InfoFlags,
) -> Result<(), AnyError> {
  let factory = CliFactory::from_flags(flags);
  let cli_options = factory.cli_options()?;
  if let Some(specifier) = info_flags.file {
    let module_graph_builder = factory.module_graph_builder().await?;
    let module_graph_creator = factory.module_graph_creator().await?;
    let npm_resolver = factory.npm_resolver().await?;
    let maybe_lockfile = cli_options.maybe_lockfile();
    let resolver = factory.workspace_resolver().await?.clone();
    let npmrc = cli_options.npmrc();
    let node_resolver = factory.node_resolver().await?;

    let cwd_url =
      url::Url::from_directory_path(cli_options.initial_cwd()).unwrap();

    let maybe_import_specifier = if let Ok(resolved) =
      resolver.resolve(&specifier, &cwd_url)
    {
      match resolved {
        deno_config::workspace::MappedResolution::Normal {
          specifier, ..
        }
        | deno_config::workspace::MappedResolution::ImportMap {
          specifier,
          ..
        }
        | deno_config::workspace::MappedResolution::WorkspaceJsrPackage {
          specifier,
          ..
        } => Some(specifier),
        deno_config::workspace::MappedResolution::WorkspaceNpmPackage {
          target_pkg_json,
          sub_path,
          ..
        } => Some(node_resolver.resolve_package_subpath_from_deno_module(
          target_pkg_json.clone().dir_path(),
          sub_path.as_deref(),
          Some(&cwd_url),
          node_resolver::ResolutionMode::Import,
          node_resolver::NodeResolutionKind::Execution,
        )?),