summaryrefslogtreecommitdiff
path: root/cli/main.rs
diff options
context:
space:
mode:
authorBartek IwaƄczuk <biwanczuk@gmail.com>2022-06-08 12:07:25 +0200
committerGitHub <noreply@github.com>2022-06-08 12:07:25 +0200
commit2769d602506af1953312b28580506fca3fcbe030 (patch)
treef9582375300cc0e4e6d5631b71fe6a5a1e687723 /cli/main.rs
parentff5def9ed5725f853f0a2cd33662211ce3418317 (diff)
fix: watch dynamic imports in --watch (#14775)
Fix dynamic imports being watched in the watcher when using `--watch`.
Diffstat (limited to 'cli/main.rs')
-rw-r--r--cli/main.rs111
1 files changed, 14 insertions, 97 deletions
diff --git a/cli/main.rs b/cli/main.rs
index b6c7e97ff..f9f92359c 100644
--- a/cli/main.rs
+++ b/cli/main.rs
@@ -995,100 +995,6 @@ async fn run_from_stdin(flags: Flags) -> Result<i32, AnyError> {
// TODO(bartlomieju): this function is not handling `exit_code` set by the runtime
// code properly.
async fn run_with_watch(flags: Flags, script: String) -> Result<i32, AnyError> {
- let flags = Arc::new(flags);
- let resolver = |_| {
- let script1 = script.clone();
- let script2 = script.clone();
- let flags = flags.clone();
- let watch_flag = flags.watch.clone();
- async move {
- let main_module = resolve_url_or_path(&script1)?;
- let ps = ProcState::build(flags).await?;
- let mut cache = cache::FetchCacher::new(
- ps.dir.gen_cache.clone(),
- ps.file_fetcher.clone(),
- Permissions::allow_all(),
- Permissions::allow_all(),
- );
- let maybe_locker = lockfile::as_maybe_locker(ps.lockfile.clone());
- let maybe_imports = if let Some(config_file) = &ps.maybe_config_file {
- config_file.to_maybe_imports()?
- } else {
- None
- };
- let maybe_import_map_resolver =
- ps.maybe_import_map.clone().map(ImportMapResolver::new);
- let maybe_jsx_resolver = ps.maybe_config_file.as_ref().and_then(|cf| {
- cf.to_maybe_jsx_import_source_module()
- .map(|im| JsxResolver::new(im, maybe_import_map_resolver.clone()))
- });
- let maybe_resolver = if maybe_jsx_resolver.is_some() {
- maybe_jsx_resolver.as_ref().map(|jr| jr.as_resolver())
- } else {
- maybe_import_map_resolver
- .as_ref()
- .map(|im| im.as_resolver())
- };
- let graph = deno_graph::create_graph(
- vec![(main_module.clone(), deno_graph::ModuleKind::Esm)],
- false,
- maybe_imports,
- &mut cache,
- maybe_resolver,
- maybe_locker,
- None,
- None,
- )
- .await;
- let check_js = ps
- .maybe_config_file
- .as_ref()
- .map(|cf| cf.get_check_js())
- .unwrap_or(false);
- graph_valid(
- &graph,
- ps.flags.type_check_mode != flags::TypeCheckMode::None,
- check_js,
- )?;
-
- // Find all local files in graph
- let mut paths_to_watch: Vec<PathBuf> = graph
- .specifiers()
- .iter()
- .filter_map(|(_, r)| {
- r.as_ref().ok().and_then(|(s, _, _)| s.to_file_path().ok())
- })
- .collect();
-
- // Add the extra files listed in the watch flag
- if let Some(watch_paths) = watch_flag {
- paths_to_watch.extend(watch_paths);
- }
-
- if let Ok(Some(import_map_path)) =
- config_file::resolve_import_map_specifier(
- ps.flags.import_map_path.as_deref(),
- ps.maybe_config_file.as_ref(),
- )
- .map(|ms| ms.and_then(|ref s| s.to_file_path().ok()))
- {
- paths_to_watch.push(import_map_path);
- }
-
- Ok((paths_to_watch, main_module, ps))
- }
- .map(move |result| match result {
- Ok((paths_to_watch, module_info, ps)) => ResolutionResult::Restart {
- paths_to_watch,
- result: Ok((ps, module_info)),
- },
- Err(e) => ResolutionResult::Restart {
- paths_to_watch: vec![PathBuf::from(script2)],
- result: Err(e),
- },
- })
- };
-
/// The FileWatcherModuleExecutor provides module execution with safe dispatching of life-cycle events by tracking the
/// state of any pending events and emitting accordingly on drop in the case of a future
/// cancellation.
@@ -1144,10 +1050,19 @@ async fn run_with_watch(flags: Flags, script: String) -> Result<i32, AnyError> {
}
}
- let operation = |(ps, main_module): (ProcState, ModuleSpecifier)| {
+ let flags = Arc::new(flags);
+ let main_module = resolve_url_or_path(&script)?;
+ let (sender, receiver) = tokio::sync::mpsc::unbounded_channel();
+
+ let operation = |(sender, main_module): (
+ tokio::sync::mpsc::UnboundedSender<Vec<PathBuf>>,
+ ModuleSpecifier,
+ )| {
let flags = flags.clone();
let permissions = Permissions::from_options(&flags.permissions_options());
async move {
+ let ps = ProcState::build_for_file_watcher(flags.clone(), sender.clone())
+ .await?;
// We make use an module executor guard to ensure that unload is always fired when an
// operation is called.
let mut executor = FileWatcherModuleExecutor::new(
@@ -1167,15 +1082,17 @@ async fn run_with_watch(flags: Flags, script: String) -> Result<i32, AnyError> {
}
};
- file_watcher::watch_func(
- resolver,
+ file_watcher::watch_func2(
+ receiver,
operation,
+ (sender, main_module),
file_watcher::PrintConfig {
job_name: "Process".to_string(),
clear_screen: !flags.no_clear_screen,
},
)
.await?;
+
Ok(0)
}