Skip to content

Commit

Permalink
fix: providing empty source code for missing compiled files (denoland…
Browse files Browse the repository at this point in the history
…#6760)

This commit adds a fallback mechanism for absent compiled source file.

Because imported type declaration files are not emitted by TS compiler
and their imports are not elided users often hit "No such file or directory"
error. With this commit in such situation an empty source file will be
provided to V8 with a warning to the user suggesting using "import type"/
"export type" syntax instead.
  • Loading branch information
bartlomieju committed Jul 17, 2020
1 parent 121eaa4 commit 6e34f6a
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 11 deletions.
27 changes: 16 additions & 11 deletions cli/global_state.rs
Expand Up @@ -9,7 +9,6 @@ use crate::module_graph::ModuleGraphFile;
use crate::module_graph::ModuleGraphLoader;
use crate::msg;
use crate::msg::MediaType;
use crate::op_error::OpError;
use crate::permissions::Permissions;
use crate::state::exit_unstable;
use crate::tsc::CompiledModule;
Expand Down Expand Up @@ -236,16 +235,22 @@ impl GlobalState {
};

let compiled_module = if was_compiled {
state1
.ts_compiler
.get_compiled_module(&out.url)
.map_err(|e| {
let msg = e.to_string();
OpError::other(format!(
"Failed to get compiled source code of {}.\nReason: {}",
out.url, msg
))
})?
match state1.ts_compiler.get_compiled_module(&out.url) {
Ok(module) => module,
Err(e) => {
let msg = format!(
"Failed to get compiled source code of \"{}\".\nReason: {}\n\
If the source file provides only type exports, prefer to use \"import type\" or \"export type\" syntax instead.",
out.url, e.to_string()
);
info!("{} {}", crate::colors::yellow("Warning"), msg);

CompiledModule {
code: "".to_string(),
name: out.url.to_string(),
}
}
}
} else {
CompiledModule {
code: String::from_utf8(out.source_code.clone())?,
Expand Down
5 changes: 5 additions & 0 deletions cli/tests/integration_tests.rs
Expand Up @@ -1972,6 +1972,11 @@ itest!(ts_decorators {
output: "ts_decorators.ts.out",
});

itest!(ts_type_only_import {
args: "run --reload ts_type_only_import.ts",
output: "ts_type_only_import.ts.out",
});

itest!(swc_syntax_error {
args: "run --reload swc_syntax_error.ts",
output: "swc_syntax_error.ts.out",
Expand Down
3 changes: 3 additions & 0 deletions cli/tests/ts_type_only_import.d.ts
@@ -0,0 +1,3 @@
export interface HelloWorld {
a: string;
}
1 change: 1 addition & 0 deletions cli/tests/ts_type_only_import.ts
@@ -0,0 +1 @@
export * from "./ts_type_only_import.d.ts";
4 changes: 4 additions & 0 deletions cli/tests/ts_type_only_import.ts.out
@@ -0,0 +1,4 @@
Check [WILDCARD]ts_type_only_import.ts
Warning Failed to get compiled source code of "[WILDCARD]ts_type_only_import.d.ts".
Reason: [WILDCARD] (os error 2)
If the source file provides only type exports, prefer to use "import type" or "export type" syntax instead.

0 comments on commit 6e34f6a

Please sign in to comment.