glint: Import of .gts emits broken declarations

As reported on Discord, there seems to be a bug when having an import with an explicit .gts extension and running glint --declaration.

Reproduction:

  • go to https://stackblitz.com/edit/github-yl7xy1
  • cd packages/accordion
  • pnpm build
  • in packages/accordion/declarations/components/accordion.d.ts you can see an import of item.gts, which does not resolve, as there is only an item.d.ts, which TS/Glint does not see as the declaration that matches this import apparently: image

The emitted declarations can be fixed by patching manually, either

  • by changing the import to be extension-less
  • or by renaming item.d.ts to item.gts.d.ts

So I think glint --declaration should be doing either of these.

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 3
  • Comments: 18 (16 by maintainers)

Commits related to this issue

Most upvoted comments

FYI as a byproduct of volar-izing glint, the declaration files emitted for basename.gts are now basename.gts.d.ts. This is in line with how Vue and others do it, and should serve as a solution to this issue, though it will likely be a breaking change for certain apps. We’re moving forward with this as a solution, and depending on a number of factors, we may decide to introduce a config or mitigating solution at some point in the future if this change is too much.

I’ll let someone else decide if this is worth closing, but at this point the change to .gts.d.ts has already been merged.

Would be better to fix the root cause of course, but I don’t need to tell you that, and I also have no time and skills to get this fixed upstream 😕

But for end users, the plan is to land this as part of https://github.com/embroider-build/embroider/pull/1798, right?

Not sure if useful for others, but I’m using this simple workaround for now:

Patch writeFile to search/replace .gts to .ts
  diff --git a/lib/cli/perform-check.js b/lib/cli/perform-check.js
index 6bef94d92a5026f78073c8c35b6f8578df8e4173..632c5f3d2b98837b973c12dc28ff01b3346df8f1 100644
--- a/lib/cli/perform-check.js
+++ b/lib/cli/perform-check.js
@@ -47,6 +47,19 @@ function createCompilerHost(ts, options, transformManager) {
     host.fileExists = transformManager.fileExists;
     host.readFile = transformManager.readTransformedFile;
     host.readDirectory = transformManager.readDirectory;
+
+    // Postprocess .d.ts files to temporarily patch '.gts' to '.ts'
+    // https://github.com/typed-ember/glint/issues/628
+    const tsWriteFile = host.writeFile;
+    const matchGtsImport = /\.gts';/gm;
+    host.writeFile = (fileName, data, writeByteOrderMark, onError) => {
+        const isDts = fileName.endsWith('.d.ts');
+        if (isDts && matchGtsImport.test(data)) {
+            data = data.replace(matchGtsImport, ".ts';");
+        }
+        tsWriteFile(fileName, data, writeByteOrderMark, onError);
+    };
+
     return host;
 }
 function loadTsconfig(ts, transformManager, configPath, optionsToExtend) {
diff --git a/lib/cli/perform-watch.js b/lib/cli/perform-watch.js
index bdcd34a27c719dd2ab734cbb6ece51be27e33a43..9eef18740a7d8eecff33e51b8af3adde6a825284 100644
--- a/lib/cli/perform-watch.js
+++ b/lib/cli/perform-watch.js
@@ -9,6 +9,19 @@ export function performWatch(glintConfig, optionsToExtend) {
     let host = ts.createWatchCompilerHost(glintConfig.configPath, optionsToExtend, sysForCompilerHost(ts, transformManager), patchProgramBuilder(ts, transformManager, ts.createSemanticDiagnosticsBuilderProgram), (diagnostic) => console.error(formatDiagnostic(diagnostic)));
     // @ts-ignore: This hook was added in TS5, and is safely irrelevant in earlier versions. Once we drop support for 4.x, we can also remove this @ts-ignore comment.
     host.resolveModuleNameLiterals = transformManager.resolveModuleNameLiterals;
+
+    // Postprocess .d.ts files to temporarily patch '.gts' to '.ts'
+    // https://github.com/typed-ember/glint/issues/628
+    const tsWriteFile = host.writeFile;
+    const matchGtsImport = /\.gts';/gm;
+    host.writeFile = (fileName, data, writeByteOrderMark, onError) => {
+        const isDts = fileName.endsWith('.d.ts');
+        if (isDts && matchGtsImport.test(data)) {
+            data = data.replace(matchGtsImport, ".ts';");
+        }
+        tsWriteFile(fileName, data, writeByteOrderMark, onError);
+    };
+
     ts.createWatchProgram(host);
 }