Repository: denoland/deno
Pull request: fix(check): support text and bytes imports with --unstable-tsgo

Fixes https://github.com/denoland/deno/issues/31034.

Changed files:
filename: cli/tsc/go.rs
patch: @@ -60,6 +60,7 @@ fn synthetic_config(
   {
     obj.insert("jsx".to_string(), json!("react-jsx"));
   }
+  obj.insert("allowArbitraryExtensions".to_string(), json!(true));
   let config = serde_json::to_string(&json!({
     "compilerOptions": config,
     "files": root_names,
@@ -334,6 +335,22 @@ fn get_package_json_scope_if_applicable(
   Ok(jsons!(None::<String>)?)
 }
 
+fn append_raw_import_fragment(specifier: &mut String, raw_kind: &str) {
+  let fragment_index = specifier.find('#');
+  match fragment_index {
+    Some(index) => {
+      if specifier[index..].contains(&format!("denoRawImport={}.ts", raw_kind))
+      {
+        return;
+      }
+      specifier.push_str(&format!("&denoRawImport={}.ts", raw_kind));
+    }
+    None => {
+      specifier.push_str(&format!("#denoRawImport={}.ts", raw_kind));
+    }
+  }
+}
+
 struct HandlerState {
   config_path: String,
   synthetic_config: String,
@@ -407,7 +424,17 @@ impl deno_typescript_go_client_rust::CallbackHandler for Handler {
       }
       "resolveModuleName" => {
         let payload = deser::<ResolveModuleNamePayload>(payload)?;
-        let (out_name, extension) = resolve_name(&mut state, payload)?;
+        let import_attribute_type = payload.import_attribute_type.clone();
+        let (mut out_name, mut extension) = resolve_name(&mut state, payload)?;
+        if let Some(import_attribute_type) = &import_attribute_type
+          && matches!(import_attribute_type.as_str(), "text" | "bytes")
+        {
+          append_raw_import_fragment(
+            &mut out_name,
+            import_attribute_type.as_str(),
+          );
+          extension = Some("ts");
+        }
 
         Ok(jsons!({
           "resolvedFileName": out_name,
@@ -432,6 +459,7 @@ impl deno_typescript_go_client_rust::CallbackHandler for Handler {
           module_name: payload.type_reference_directive_name,
           containing_file: payload.containing_file,
           resolution_mode: payload.resolution_mode,
+          import_attribute_type: None,
         };
         let (out_name, extension) = resolve_name(&mut state, payload)?;
         log::debug!(
@@ -507,6 +535,7 @@ fn resolve_name(
   payload: ResolveModuleNamePayload,
 ) -> Result<(String, Option<&'static str>), deno_typescript_go_client_rust::Error>
 {
+  log::debug!("resolve_name({payload:?})");
   let graph = &handler.graph;
   let maybe_npm = handler.maybe_npm.as_ref();
   let referrer = if let Some(remapped_specifier) =
@@ -627,6 +656,7 @@ fn load_inner(
     state,
   )?;
   let Some(result) = result else {
+    log::debug!("load_inner {load_specifier} -> None");
     return Ok(None);
   };
   let is_cjs = result.is_cjs;
@@ -635,6 +665,7 @@ fn load_inner(
   let module_kind = get_resolution_mode(is_cjs, media_type);
   let script_kind = super::as_ts_script_kind(media_type);
   log::debug!("load_inner {load_specifier} -> {:?}", module_kind);
+  log::trace!("loaded contents ({load_specifier}) -> {:?}", result.data);
   state
     .module_kind_map
     .insert(load_specifier.to_string(), module_kind);

filename: cli/tsc/go/tsgo_version.rs
patch: @@ -23,15 +23,15 @@ impl Hashes {
   }
 }
 
-pub const VERSION: &str = "0.1.6";
+pub const VERSION: &str = "0.1.8";
 pub const DOWNLOAD_BASE_URL: &str =
-  "https://github.com/denoland/typescript-go/releases/download/v0.1.6";
+  "https://github.com/denoland/typescript-go/releases/download/v0.1.8";
 pub const HASHES: Hashes = Hashes {
-  windows_x64: "sha256:04f85a9a64807437471cd45ed569b1ee9910dbe9751c1a5085028ae5eb09db56",
-  macos_x64: "sha256:84619ab4a6ac3dc1c78a62c209ea853cf077871fe086657bc861e268b9c0412c",
-  macos_arm64: "sha256:cef3d3f60abe9f2947f2e30f8075d860f9bf176a5545c651eabfcaa9e791e0f9",
-  linux_x64: "sha256:ea0ae7f3782a8372a03ec2c1f1bbb415e0c10a43ce917b0564084f896e0df127",
-  linux_arm64: "sha256:67b4f4d9982ff5c5c14105b37c48b582e3dc806c8a504f1a1b5416e29de68198",
+  windows_x64: "sha256:3e5c7102c387d1b85795e0baf402e93a1c3bd82d75b5ad90089ea72f0525bc22",
+  macos_x64: "sha256:5f31471910b585dfbffdd370ffd0d0dc9f408e1b7482e293a84246d7c8bf980b",
+  macos_arm64: "sha256:73d88c842a5276d721f38c6b749a923417f1c42f237fafe06ed4c50d772dda71",
+  linux_x64: "sha256:e83798203401f8fd31f2539ef23c1bc76a9bf02cfe6902dc854310ed8d994454",
+  linux_arm64: "sha256:f82e11a474bb9e69e0397c614ab30a08eb09c93c69bb3de19364ef660a851ede",
 };
 
 const _: () = {

filename: libs/typescript_go_client/src/types.rs
patch: @@ -48,6 +48,7 @@ pub struct ResolveModuleNamePayload {
   pub module_name: String,
   pub containing_file: String,
   pub resolution_mode: ResolutionMode,
+  pub import_attribute_type: Option<String>,
   // redirected_reference: Handle<Project>,
 }
 

filename: tests/specs/check/bytes_and_text_imports/__test__.jsonc
patch: @@ -3,14 +3,9 @@
   "output": "main.out",
   "exitCode": 1,
   "variants": {
-    // TODO(nathanwhit): not implemented yet, needs tsgo changes
-    //  see appendRawImportFragment in cli/tsc/97_ts_host.js and the code that calls it.
-    //  it will need tsgo changes because the import kind is not easily available on the tsgo side
-    //  during resolution.
-    //
-    // "tsgo": {
-    //   "use_tsgo": "1"
-    // },
+    "tsgo": {
+      "use_tsgo": "1"
+    },
     "tsc": {
       "use_tsgo": ""
     }