Repository: denoland/deno
File: libs/resolver/npm/mod.rs (Rust)

Code before:

                type_req.map(|(r, _)| r).cloned()
              } else {
                None
              }
            } else {
              Some(
                PackageReq::from_str(&format!(
                  "{}@*",
                  types_package_name(&req.name)
                ))
                .unwrap(),
              )
            };
          if let Some(req) = maybe_definitely_typed_req
            && let Ok(resolved) = self.resolve_req_with_sub_path(
              &req,
              sub_path,
              referrer,
              resolution_mode,
              resolution_kind,
            )
          {
            return Ok(resolved);
          }
          // failed to find types, just fall back to normal resolution
          if let Ok(resolved) =
            self.node_resolver.resolve_package_subpath_from_deno_module(
              &package_folder,
              sub_path,
              Some(referrer),
              resolution_mode,
              NodeResolutionKind::Execution,
            )
          {
            return Ok(resolved);
          }
        }
        if matches!(self.npm_resolver, NpmResolver::Byonm(_)) {
          let package_json_path = package_folder.join("package.json");
          if !self.sys.fs_exists_no_err(&package_json_path) {
            return Err(
              MissingPackageNodeModulesFolderError {
                package_json_path,
                inner: err,
              }
              .into(),
            );
          }
        }
        Err(err.into())

Review comment (bug):
Although it won't happen, it might be worthwhile to add a debug assertion that the current NodeResolutionKind is Types in case the code changes somehow.

Code after:

                type_req.map(|(r, _)| r).cloned()
              } else {
                None
              }
            } else {
              Some(
                PackageReq::from_str(&format!(
                  "{}@*",
                  types_package_name(&req.name)
                ))
                .unwrap(),
              )
            };
          if let Some(req) = maybe_definitely_typed_req
            && let Ok(resolved) = self.resolve_req_with_sub_path(
              &req,
              sub_path,
              referrer,
              resolution_mode,
              resolution_kind,
            )
          {
            return Ok(resolved);
          }
          // failed to find types, just fall back to normal resolution
          debug_assert_eq!(resolution_kind, NodeResolutionKind::Types);
          if let Ok(resolved) =
            self.node_resolver.resolve_package_subpath_from_deno_module(
              &package_folder,
              sub_path,
              Some(referrer),
              resolution_mode,
              NodeResolutionKind::Execution,
            )
          {
            return Ok(resolved);
          }
        }
        if matches!(self.npm_resolver, NpmResolver::Byonm(_)) {
          let package_json_path = package_folder.join("package.json");
          if !self.sys.fs_exists_no_err(&package_json_path) {
            return Err(
              MissingPackageNodeModulesFolderError {
                package_json_path,
                inner: err,
              }
              .into(),
            );
          }
        }