diff options
author | Yusuke Tanaka <yusuktan@maguro.dev> | 2020-11-28 23:18:13 +0900 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-11-28 15:18:13 +0100 |
commit | d9b41828684a5e1ed620d7324ad6c27a83687622 (patch) | |
tree | 774b49d47890715c4257299e968e19ab0154a306 /cli/main.rs | |
parent | 5588085c727271a362904362184a148bd1648712 (diff) |
fix(watcher): watcher doesn't exit when module resolution fails (#8521)
This commit makes the file watcher continue to work even if module
resolution fails at the initial attempt, allowing us to execute `run`
or `bundle` subcommand when a script has invalid syntax. In such
cases, the watcher observes a single file that is specified as an
command line argument.
Diffstat (limited to 'cli/main.rs')
-rw-r--r-- | cli/main.rs | 36 |
1 files changed, 31 insertions, 5 deletions
diff --git a/cli/main.rs b/cli/main.rs index fdbfdad77..de2e1b402 100644 --- a/cli/main.rs +++ b/cli/main.rs @@ -50,6 +50,7 @@ mod worker; use crate::file_fetcher::File; use crate::file_fetcher::FileFetcher; +use crate::file_watcher::ModuleResolutionResult; use crate::media_type::MediaType; use crate::permissions::Permissions; use crate::program_state::ProgramState; @@ -307,10 +308,11 @@ async fn bundle_command( let module_resolver = || { let flags = flags.clone(); - let source_file = source_file.clone(); + let source_file1 = source_file.clone(); + let source_file2 = source_file.clone(); async move { let module_specifier = - ModuleSpecifier::resolve_url_or_path(&source_file)?; + ModuleSpecifier::resolve_url_or_path(&source_file1)?; debug!(">>>>> bundle START"); let program_state = ProgramState::new(flags.clone())?; @@ -373,6 +375,16 @@ async fn bundle_command( Ok((paths_to_watch, module_graph)) } + .map(move |result| match result { + Ok((paths_to_watch, module_graph)) => ModuleResolutionResult::Success { + paths_to_watch, + module_info: module_graph, + }, + Err(e) => ModuleResolutionResult::Fail { + source_path: PathBuf::from(source_file2), + error: e, + }, + }) .boxed_local() }; @@ -423,7 +435,10 @@ async fn bundle_command( ) .await?; } else { - let (_, module_graph) = module_resolver().await?; + let module_graph = match module_resolver().await { + ModuleResolutionResult::Fail { error, .. } => return Err(error), + ModuleResolutionResult::Success { module_info, .. } => module_info, + }; operation(module_graph).await?; } @@ -610,10 +625,11 @@ async fn run_from_stdin(flags: Flags) -> Result<(), AnyError> { async fn run_with_watch(flags: Flags, script: String) -> Result<(), AnyError> { let module_resolver = || { - let script = script.clone(); + let script1 = script.clone(); + let script2 = script.clone(); let flags = flags.clone(); async move { - let main_module = ModuleSpecifier::resolve_url_or_path(&script)?; + let main_module = ModuleSpecifier::resolve_url_or_path(&script1)?; let program_state = ProgramState::new(flags)?; let handler = Rc::new(RefCell::new(FetchHandler::new( &program_state, @@ -641,6 +657,16 @@ async fn run_with_watch(flags: Flags, script: String) -> Result<(), AnyError> { Ok((paths_to_watch, main_module)) } + .map(move |result| match result { + Ok((paths_to_watch, module_info)) => ModuleResolutionResult::Success { + paths_to_watch, + module_info, + }, + Err(e) => ModuleResolutionResult::Fail { + source_path: PathBuf::from(script2), + error: e, + }, + }) .boxed_local() }; |