Skip to content

Commit

Permalink
feat(publish): support sloppy imports and bare node built-ins (#22588)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucacasonato authored Feb 27, 2024
1 parent 47c2a63 commit 8d5c231
Show file tree
Hide file tree
Showing 17 changed files with 316 additions and 130 deletions.
2 changes: 1 addition & 1 deletion cli/factory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ use crate::resolver::SloppyImportsResolver;
use crate::standalone::DenoCompileBinaryWriter;
use crate::tools::check::TypeChecker;
use crate::tools::coverage::CoverageCollector;
use crate::tools::registry::deno_json_deps;
use crate::tools::run::hmr::HmrRunner;
use crate::util::file_watcher::WatcherCommunicator;
use crate::util::fs::canonicalize_path_maybe_not_exists;
use crate::util::import_map::deno_json_deps;
use crate::util::progress_bar::ProgressBar;
use crate::util::progress_bar::ProgressBarStyle;
use crate::worker::CliMainWorkerFactory;
Expand Down
32 changes: 16 additions & 16 deletions cli/tools/registry/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use deno_core::error::AnyError;
use deno_graph::FastCheckDiagnostic;
use lsp_types::Url;

use crate::util::import_map::ImportMapUnfurlDiagnostic;
use super::unfurl::SpecifierUnfurlerDiagnostic;

#[derive(Clone, Default)]
pub struct PublishDiagnosticsCollector {
Expand Down Expand Up @@ -74,7 +74,7 @@ impl PublishDiagnosticsCollector {

pub enum PublishDiagnostic {
FastCheck(FastCheckDiagnostic),
ImportMapUnfurl(ImportMapUnfurlDiagnostic),
SpecifierUnfurl(SpecifierUnfurlerDiagnostic),
InvalidPath {
path: PathBuf,
message: String,
Expand Down Expand Up @@ -102,7 +102,7 @@ impl Diagnostic for PublishDiagnostic {
..
}) => DiagnosticLevel::Warning,
FastCheck(_) => DiagnosticLevel::Error,
ImportMapUnfurl(_) => DiagnosticLevel::Warning,
SpecifierUnfurl(_) => DiagnosticLevel::Warning,
InvalidPath { .. } => DiagnosticLevel::Error,
DuplicatePath { .. } => DiagnosticLevel::Error,
UnsupportedFileType { .. } => DiagnosticLevel::Warning,
Expand All @@ -114,7 +114,7 @@ impl Diagnostic for PublishDiagnostic {
use PublishDiagnostic::*;
match &self {
FastCheck(diagnostic) => diagnostic.code(),
ImportMapUnfurl(diagnostic) => Cow::Borrowed(diagnostic.code()),
SpecifierUnfurl(diagnostic) => Cow::Borrowed(diagnostic.code()),
InvalidPath { .. } => Cow::Borrowed("invalid-path"),
DuplicatePath { .. } => Cow::Borrowed("case-insensitive-duplicate-path"),
UnsupportedFileType { .. } => Cow::Borrowed("unsupported-file-type"),
Expand All @@ -126,7 +126,7 @@ impl Diagnostic for PublishDiagnostic {
use PublishDiagnostic::*;
match &self {
FastCheck(diagnostic) => diagnostic.message(),
ImportMapUnfurl(diagnostic) => Cow::Borrowed(diagnostic.message()),
SpecifierUnfurl(diagnostic) => Cow::Borrowed(diagnostic.message()),
InvalidPath { message, .. } => Cow::Borrowed(message.as_str()),
DuplicatePath { .. } => {
Cow::Borrowed("package path is a case insensitive duplicate of another path in the package")
Expand All @@ -142,8 +142,8 @@ impl Diagnostic for PublishDiagnostic {
use PublishDiagnostic::*;
match &self {
FastCheck(diagnostic) => diagnostic.location(),
ImportMapUnfurl(diagnostic) => match diagnostic {
ImportMapUnfurlDiagnostic::UnanalyzableDynamicImport {
SpecifierUnfurl(diagnostic) => match diagnostic {
SpecifierUnfurlerDiagnostic::UnanalyzableDynamicImport {
specifier,
text_info,
range,
Expand Down Expand Up @@ -180,8 +180,8 @@ impl Diagnostic for PublishDiagnostic {
fn snippet(&self) -> Option<DiagnosticSnippet<'_>> {
match &self {
PublishDiagnostic::FastCheck(diagnostic) => diagnostic.snippet(),
PublishDiagnostic::ImportMapUnfurl(diagnostic) => match diagnostic {
ImportMapUnfurlDiagnostic::UnanalyzableDynamicImport {
PublishDiagnostic::SpecifierUnfurl(diagnostic) => match diagnostic {
SpecifierUnfurlerDiagnostic::UnanalyzableDynamicImport {
text_info,
range,
..
Expand Down Expand Up @@ -227,7 +227,7 @@ impl Diagnostic for PublishDiagnostic {
fn hint(&self) -> Option<Cow<'_, str>> {
match &self {
PublishDiagnostic::FastCheck(diagnostic) => diagnostic.hint(),
PublishDiagnostic::ImportMapUnfurl(_) => None,
PublishDiagnostic::SpecifierUnfurl(_) => None,
PublishDiagnostic::InvalidPath { .. } => Some(
Cow::Borrowed("rename or remove the file, or add it to 'publish.exclude' in the config file"),
),
Expand All @@ -250,11 +250,11 @@ impl Diagnostic for PublishDiagnostic {
PublishDiagnostic::FastCheck(diagnostic) => {
diagnostic.info()
}
PublishDiagnostic::ImportMapUnfurl(diagnostic) => match diagnostic {
ImportMapUnfurlDiagnostic::UnanalyzableDynamicImport { .. } => Cow::Borrowed(&[
Cow::Borrowed("after publishing this package, imports from the local import map do not work"),
PublishDiagnostic::SpecifierUnfurl(diagnostic) => match diagnostic {
SpecifierUnfurlerDiagnostic::UnanalyzableDynamicImport { .. } => Cow::Borrowed(&[
Cow::Borrowed("after publishing this package, imports from the local import map / package.json do not work"),
Cow::Borrowed("dynamic imports that can not be analyzed at publish time will not be rewritten automatically"),
Cow::Borrowed("make sure the dynamic import is resolvable at runtime without an import map")
Cow::Borrowed("make sure the dynamic import is resolvable at runtime without an import map / package.json")
]),
},
PublishDiagnostic::InvalidPath { .. } => Cow::Borrowed(&[
Expand All @@ -278,8 +278,8 @@ impl Diagnostic for PublishDiagnostic {
fn docs_url(&self) -> Option<Cow<'_, str>> {
match &self {
PublishDiagnostic::FastCheck(diagnostic) => diagnostic.docs_url(),
PublishDiagnostic::ImportMapUnfurl(diagnostic) => match diagnostic {
ImportMapUnfurlDiagnostic::UnanalyzableDynamicImport { .. } => None,
PublishDiagnostic::SpecifierUnfurl(diagnostic) => match diagnostic {
SpecifierUnfurlerDiagnostic::UnanalyzableDynamicImport { .. } => None,
},
PublishDiagnostic::InvalidPath { .. } => {
Some(Cow::Borrowed("https://jsr.io/go/invalid-path"))
Expand Down
23 changes: 21 additions & 2 deletions cli/tools/registry/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@ use crate::factory::CliFactory;
use crate::graph_util::ModuleGraphCreator;
use crate::http_util::HttpClient;
use crate::resolver::MappedSpecifierResolver;
use crate::resolver::SloppyImportsResolver;
use crate::tools::check::CheckOptions;
use crate::tools::lint::no_slow_types;
use crate::tools::registry::diagnostics::PublishDiagnostic;
use crate::tools::registry::diagnostics::PublishDiagnosticsCollector;
use crate::tools::registry::graph::collect_invalid_external_imports;
use crate::util::display::human_size;
use crate::util::import_map::ImportMapUnfurler;

mod api;
mod auth;
Expand All @@ -50,10 +50,13 @@ mod graph;
mod paths;
mod publish_order;
mod tar;
mod unfurl;

use auth::get_auth_method;
use auth::AuthMethod;
use publish_order::PublishOrderGraph;
pub use unfurl::deno_json_deps;
use unfurl::SpecifierUnfurler;

use super::check::TypeChecker;

Expand Down Expand Up @@ -81,12 +84,15 @@ impl PreparedPublishPackage {
static SUGGESTED_ENTRYPOINTS: [&str; 4] =
["mod.ts", "mod.js", "index.ts", "index.js"];

#[allow(clippy::too_many_arguments)]
async fn prepare_publish(
package_name: &str,
deno_json: &ConfigFile,
source_cache: Arc<ParsedSourceCache>,
graph: Arc<deno_graph::ModuleGraph>,
mapped_resolver: Arc<MappedSpecifierResolver>,
sloppy_imports_resolver: Option<SloppyImportsResolver>,
bare_node_builtins: bool,
diagnostics_collector: &PublishDiagnosticsCollector,
) -> Result<Rc<PreparedPublishPackage>, AnyError> {
let config_path = deno_json.specifier.to_file_path().unwrap();
Expand Down Expand Up @@ -132,7 +138,11 @@ async fn prepare_publish(

let diagnostics_collector = diagnostics_collector.clone();
let tarball = deno_core::unsync::spawn_blocking(move || {
let unfurler = ImportMapUnfurler::new(&mapped_resolver);
let unfurler = SpecifierUnfurler::new(
&mapped_resolver,
sloppy_imports_resolver.as_ref(),
bare_node_builtins,
);
tar::create_gzipped_tarball(
&dir_path,
LazyGraphSourceParser::new(&source_cache, &graph),
Expand Down Expand Up @@ -661,7 +671,9 @@ async fn prepare_packages_for_publishing(
let module_graph_creator = cli_factory.module_graph_creator().await?.as_ref();
let source_cache = cli_factory.parsed_source_cache();
let type_checker = cli_factory.type_checker().await?;
let fs = cli_factory.fs();
let cli_options = cli_factory.cli_options();
let bare_node_builtins = cli_options.unstable_bare_node_builtins();

if members.len() > 1 {
println!("Publishing a workspace...");
Expand All @@ -686,6 +698,11 @@ async fn prepare_packages_for_publishing(
.into_iter()
.map(|member| {
let mapped_resolver = mapped_resolver.clone();
let sloppy_imports_resolver = if cli_options.unstable_sloppy_imports() {
Some(SloppyImportsResolver::new(fs.clone()))
} else {
None
};
let graph = graph.clone();
async move {
let package = prepare_publish(
Expand All @@ -694,6 +711,8 @@ async fn prepare_packages_for_publishing(
source_cache.clone(),
graph,
mapped_resolver,
sloppy_imports_resolver,
bare_node_builtins,
diagnostics_collector,
)
.await
Expand Down
8 changes: 4 additions & 4 deletions cli/tools/registry/tar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ use tar::Header;

use crate::cache::LazyGraphSourceParser;
use crate::tools::registry::paths::PackagePath;
use crate::util::import_map::ImportMapUnfurler;

use super::diagnostics::PublishDiagnostic;
use super::diagnostics::PublishDiagnosticsCollector;
use super::unfurl::SpecifierUnfurler;

#[derive(Debug, Clone, PartialEq)]
pub struct PublishableTarballFile {
Expand All @@ -40,7 +40,7 @@ pub fn create_gzipped_tarball(
dir: &Path,
source_parser: LazyGraphSourceParser,
diagnostics_collector: &PublishDiagnosticsCollector,
unfurler: &ImportMapUnfurler,
unfurler: &SpecifierUnfurler,
file_patterns: Option<FilePatterns>,
) -> Result<PublishableTarball, AnyError> {
let mut tar = TarGzArchive::new();
Expand Down Expand Up @@ -192,7 +192,7 @@ pub fn create_gzipped_tarball(
fn resolve_content_maybe_unfurling(
path: &Path,
specifier: &Url,
unfurler: &ImportMapUnfurler,
unfurler: &SpecifierUnfurler,
source_parser: LazyGraphSourceParser,
diagnostics_collector: &PublishDiagnosticsCollector,
) -> Result<Vec<u8>, AnyError> {
Expand Down Expand Up @@ -241,7 +241,7 @@ fn resolve_content_maybe_unfurling(

log::debug!("Unfurling {}", specifier);
let mut reporter = |diagnostic| {
diagnostics_collector.push(PublishDiagnostic::ImportMapUnfurl(diagnostic));
diagnostics_collector.push(PublishDiagnostic::SpecifierUnfurl(diagnostic));
};
let content = unfurler.unfurl(specifier, &parsed_source, &mut reporter);
Ok(content.into_bytes())
Expand Down
Loading

0 comments on commit 8d5c231

Please sign in to comment.