Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exports to <object><ref>Mod rather than <object>Mod #555

Closed
bphenriques opened this issue Aug 13, 2023 · 5 comments
Closed

Exports to <object><ref>Mod rather than <object>Mod #555

bphenriques opened this issue Aug 13, 2023 · 5 comments

Comments

@bphenriques
Copy link

bphenriques commented Aug 13, 2023

Hello,

The converter is exporting Tauri's dialog interface (https://tauri.app/v1/api/js/dialog) to dialog20ff401cMod rather than just dialogMod. In which situations does this occur and how can I avoid it?

Context

I am unable to find the culprit. I thought it was related to a dependency bump or the addition of stFlavour := Flavour.ScalajsReact but it might not be it. I was able to bump and build the frontend of the following sample project[1] using the latest versions (tested using sbt buildFrontend):

  • sbt: 1.9.2
  • converter: 1.0.0-beta42
  • scalajs: 1.13.2
  • scala version: 3.3.0

Saying might not be it because I applied the same changes to a separate project using a different stOutputPackage and now I am unable to compile. Is that related?

I am pretty sure that I am missing something and after several hours my mind is playing tricks on me. I am hopeful that there is simple explanation.

Notes

Reproducing the issue is confusing as it seems that I need to clean-up files between each conversion (see [2])? I followed the cache path mentioned in the docs.

Plus, without looking at the source-code, it is unclear where the 20ff401c comes from (doesn't match a git SHA).

Thank you!


[1]

diff --git a/build.sbt b/build.sbt
index fc3c74d..ff56d07 100644
--- a/build.sbt
+++ b/build.sbt
@@ -8,10 +8,10 @@ lazy val tauri = project
   .settings(
     organization := "com.indoorvivants.tauri",
     moduleName := "api",
-    scalaVersion := "3.1.2",
+    scalaVersion := "3.3.0",
     libraryDependencies += "com.raquo" %%% "laminar" % "0.14.2",
     externalNpm := {
-      sys.process.Process("yarn", baseDirectory.value).!
+      //sys.process.Process("yarn", baseDirectory.value).!
       baseDirectory.value
     },
     stOutputPackage := "com.indoorvivants.tauri",
@@ -21,6 +21,7 @@ lazy val tauri = project
       config
         .withModuleKind(ModuleKind.ESModule)
     },
+    stFlavour := Flavour.ScalajsReact,
     scalaJSUseMainModuleInitializer := true,
     Compile / packageSrc / mappings ++= {
       val base = (Compile / sourceManaged).value
diff --git a/project/build.properties b/project/build.properties
index c8fcab5..875b706 100644
--- a/project/build.properties
+++ b/project/build.properties
@@ -1 +1 @@
-sbt.version=1.6.2
+sbt.version=1.9.2
diff --git a/project/plugins.sbt b/project/plugins.sbt
index 65ed377..55d07fa 100644
--- a/project/plugins.sbt
+++ b/project/plugins.sbt
@@ -1,5 +1,5 @@
 addSbtPlugin("com.geirsson" % "sbt-ci-release" % "1.5.7")
 addSbtPlugin("com.indoorvivants" % "sbt-commandmatrix" % "0.0.5")
-addSbtPlugin("org.scalablytyped.converter" % "sbt-converter" % "1.0.0-beta37")
-addSbtPlugin("org.scala-js" % "sbt-scalajs" % "1.10.0")
+addSbtPlugin("org.scalablytyped.converter" % "sbt-converter" % "1.0.0-beta42")
+addSbtPlugin("org.scala-js" % "sbt-scalajs" % "1.13.2")

diff --git a/src-tauri/tauri.conf.json b/src-tauri/tauri.conf.json
index cfe710c..4130eaf 100644
--- a/src-tauri/tauri.conf.json
+++ b/src-tauri/tauri.conf.json
@@ -1,8 +1,8 @@
 {
   "$schema": "../node_modules/@tauri-apps/cli/schema.json",
   "build": {
-    "beforeBuildCommand": "npm run build",
-    "beforeDevCommand": "npm run dev",
+    "beforeBuildCommand": "yarn run build",
+    "beforeDevCommand": "yarn run dev",
     "devPath": "http://localhost:3000",
     "distDir": "../dist",
     "withGlobalTauri": true
diff --git a/src/main/scala/Main.scala b/src/main/scala/Main.scala
index 30cc998..fe1ce8a 100644
--- a/src/main/scala/Main.scala
+++ b/src/main/scala/Main.scala
@@ -2,8 +2,6 @@ import com.indoorvivants.tauri.tauriAppsApi.*
 import scalajs.js.Thenable.Implicits.*
 import concurrent.ExecutionContext.Implicits.global

-import tauriAppsApiMod as tauri
-import tauriAppsApiMod as tauri
 import org.scalajs.dom
 import org.scalablytyped.runtime.StringDictionary

[2]

rm -rf ~/.ivy2/local/org.scalablytyped.com.indoorvivants.tauri ~/.cache/scalablytyped project/target project/project target/ ~/.ivy2/local/org.scalablytyped/
@oyvindberg
Copy link
Collaborator

javascript people are still javascript people I see.

those names are from typescript - you can use those in typescript and it'll compile!

I added the package locally, and index.d.ts looks like this:

export { a as app } from './app-5190a154.js';
export { c as cli } from './cli-373e13ed.js';
export { c as clipboard } from './clipboard-79413165.js';
export { d as dialog } from './dialog-20ff401c.js';
export { e as event, w as window } from './event-41a9edf5.js';
export { f as fs } from './fs-6ad2a328.js';
export { g as globalShortcut } from './globalShortcut-003b7421.js';
export { h as http } from './http-43c39402.js';
export { n as notification } from './notification-6cd45c32.js';
export { p as path } from './path-c062430b.js';
export { p as process } from './process-63838be1.js';
export { s as shell } from './shell-efff51a2.js';
import { i as invoke$1 } from './tauri-605fa63e.js';
export { t as tauri } from './tauri-605fa63e.js';
export { u as updater } from './updater-f9814f36.js';
export { o as os } from './os-650909c3.js';

ST follows code referred from index.d.ts (or files specified in typings.json, package.json etc) in a (semi-flawed) attempt at not including everything in the npm package. dialog.d.ts is not mentioned there, and hence not included in the conversion.

The reason why this has to be done is that many popular packages package up to a dozen versions of the same library in the same gzip file. and importing a dozen similar-but-has-different-module-names hierarchies of code makes compile time and automatic naming suffer a lot. Choosing which files to import is actually quite hard.

as such, this is probably the same thing as #463, #488. You see there is a PR, so this is going to be solved eventually. I'm working on another project now, it'll be later in autumn that I have time to return to ST :)

in the meantime I guess manually change typescript or the generated code and distribute it as a jar may be a good path forward

@oyvindberg
Copy link
Collaborator

you could also provide a rename in your project of course, something like

object mytauri {
  val nicename = uglyname
}

and import from there instead

@oyvindberg
Copy link
Collaborator

Reproducing the issue is confusing as it seems that I need to clean-up files between each conversion (see [2])? I followed the cache path mentioned in the docs.

this really shouldn't be needed. the digest part of the file name ensures that the generated source code is the same, and the only difference from there and out is what scalac does.

@bphenriques
Copy link
Author

Thank you for the swift response!

ST follows code referred from index.d.ts (or files specified in typings.json, package.json etc) in a (semi-flawed) attempt at not including everything in the npm package. dialog.d.ts is not mentioned there, and hence not included in the conversion.

The reason why this has to be done is that many popular packages package up to a dozen versions of the same library in the same gzip file. and importing a dozen similar-but-has-different-module-names hierarchies of code makes compile time and automatic naming suffer a lot. Choosing which files to import is actually quite hard.

as such, this is probably the same thing as #463, #488. You see there is a PR, so this is going to be solved eventually. I'm working on another project now, it'll be later in autumn that I have time to return to ST :)

in the meantime I guess manually change typescript or the generated code and distribute it as a jar may be a good path forward

Thanks! That helps understanding the issue a bit better! I did not consider checking the contents of node_modules!

If the converter relies solely on the JS files under node_modules then the issue is isolated to the contents of node_modules.

Managed to reproduce the inconsistent node_modules/@tauri-apps/api/index.d.ts and it is confusing:

  1. Test 1: Fresh clone then run yarn install leads to [2]
  2. Test 2: Fresh clone, bumped Tauris to 1.4.0 then run yarn install. Leads to [1].
  3. Test 3: Test 2 followed by rollback to 1.0.0. It stays stuck in [1]. Even after rm -rf node_modules yarn.lock && yarn cache clean. This is the behaviour that baffles me which led to a long day of debugging. I should be able to rollback easily.

I do not mean to drag you to this rabbit-hole, I apreciate the help!

Regarding the workaround, it seems that it is almost working:

  1. Updated the dialog call to dialog20ff401cMod.a
  2. Updated the windowMod.appWindow call to event41a9edf5Mod.d (was super weird having to dig around the JS code and the renames inside)

The code is compiling but now files ./dialog-20ff401c.js and ./event-41a9edf5.js do not exist (the d.js counterparts exist). When I try to run the web app, I encounter the following errors:

node_modules/vite/dist/node/chunks/dep-6e2fe41e.js:38597:7: ERROR: [plugin: vite:dep-scan] Missing "./dialog-20ff401c" export in "@tauri-apps/api" package
node_modules/vite/dist/node/chunks/dep-6e2fe41e.js:38597:7: ERROR: [plugin: vite:dep-scan] Missing "./event-41a9edf5" export in "@tauri-apps/api" package

At this point, I am too concerned making manual changes to the JS files given the nature of the error. Do you have a suggestion?

this really shouldn't be needed. the digest part of the file name ensures that the generated source code is the same, and the only difference from there and out is what scalac does.

I understand but can't explain. Not worthwhile revisiting this topic given that I am still in discovery mode. I suppose that, if it does not rely on the content of the files, I need to manually recompile if I manually modify the files (really trying to avoid that).


[1]
index.d.ts:

export { a as app } from './app-5190a154.js';
export { c as cli } from './cli-373e13ed.js';
export { c as clipboard } from './clipboard-79413165.js';
export { d as dialog } from './dialog-20ff401c.js';
export { e as event, w as window } from './event-41a9edf5.js';
export { f as fs } from './fs-6ad2a328.js';
export { g as globalShortcut } from './globalShortcut-003b7421.js';
export { h as http } from './http-43c39402.js';
export { n as notification } from './notification-6cd45c32.js';
export { p as path } from './path-c062430b.js';
export { p as process } from './process-63838be1.js';
export { s as shell } from './shell-efff51a2.js';
import { i as invoke$1 } from './tauri-605fa63e.js';
export { t as tauri } from './tauri-605fa63e.js';
export { u as updater } from './updater-f9814f36.js';
export { o as os } from './os-650909c3.js';

/**
 * The Tauri API allows you to interface with the backend layer.
 *
 * This module exposes all other modules as an object where the key is the module name, and the value is the module exports.
 * @example
 * ```typescript
 * import { app, dialog, event, fs, globalShortcut } from '@tauri-apps/api'
 * ```
 * @module
 */

/** @ignore */
declare const invoke: typeof invoke$1;

export { invoke };

[2]
index.d.ts:

/**
 * The Tauri API allows you to interface with the backend layer.
 *
 * This module exposes all other modules as an object where the key is the module name, and the value is the module exports.
 * @example
 * ```typescript
 * import { app, dialog, event, fs, globalShortcut } from '@tauri-apps/api'
 * ```
 * @module
 */
export * from './bundle';

What in turn bundle.d.js contains

import 'regenerator-runtime/runtime';
import * as app from './app';
import * as cli from './cli';
import * as clipboard from './clipboard';
import * as dialog from './dialog';
import * as event from './event';
import * as fs from './fs';
import * as globalShortcut from './globalShortcut';
import * as http from './http';
import * as notification from './notification';
import * as path from './path';
import * as process from './process';
import * as shell from './shell';
import * as tauri from './tauri';
import * as updater from './updater';
import * as window from './window';
import * as os from './os';
/** @ignore */
declare const invoke: typeof tauri.invoke;
export { app, cli, clipboard, dialog, event, fs, globalShortcut, http, notification, path, process, shell, tauri, updater, window, os, invoke };

[3]

✘ [ERROR] [plugin vite:dep-scan] Missing "./dialog-20ff401c" export in "@tauri-apps/api" package
...
Build failed with 1 error:
node_modules/vite/dist/node/chunks/dep-6e2fe41e.js:38597:7: ERROR: [plugin: vite:dep-scan] Missing "./dialog-20ff401c" export in "@tauri-apps/api" package
        Info Watching /home/bphenriques/workspace/tauris-test-desktop/src-tauri for changes...
    Finished dev [unoptimized + debuginfo] target(s) in 0.80s
6:56:50 AM [vite] Internal server error: Missing "./dialog-20ff401c" export in "@tauri-apps/api" package
  Plugin: vite:import-analysis
  File: /home/bphenriques/workspace/tauris-test-desktop/main.js
      at bail (/home/bphenriques/workspace/tauris-test-desktop/node_modules/vite/dist/node/chunks/dep-6e2fe41e.js:38597:8)
      at resolve (/home/bphenriques/workspace/tauris-test-desktop/node_modules/vite/dist/node/chunks/dep-6e2fe41e.js:38674:10)
      at resolveExports (/home/bphenriques/workspace/tauris-test-desktop/node_modules/vite/dist/node/chunks/dep-6e2fe41e.js:40982:12)
      at resolveDeepImport (/home/bphenriques/workspace/tauris-test-desktop/node_modules/vite/dist/node/chunks/dep-6e2fe41e.js:41000:31)
      at tryNodeResolve (/home/bphenriques/workspace/tauris-test-desktop/node_modules/vite/dist/node/chunks/dep-6e2fe41e.js:40773:20)
      at Context.resolveId (/home/bphenriques/workspace/tauris-test-desktop/node_modules/vite/dist/node/chunks/dep-6e2fe41e.js:40581:28)
      at async Object.resolveId (/home/bphenriques/workspace/tauris-test-desktop/node_modules/vite/dist/node/chunks/dep-6e2fe41e.js:39254:32)
      at async TransformContext.resolve (/home/bphenriques/workspace/tauris-test-desktop/node_modules/vite/dist/node/chunks/dep-6e2fe41e.js:39028:23)
      at async normalizeUrl (/home/bphenriques/workspace/tauris-test-desktop/node_modules/vite/dist/node/chunks/dep-6e2fe41e.js:58354:34)
      at async TransformContext.transform (/home/bphenriques/workspace/tauris-test-desktop/node_modules/vite/dist/node/chunks/dep-6e2fe41e.js:58509:57)
6:56:50 AM [vite] Internal server error: Missing "./dialog-20ff401c" export in "@tauri-apps/api" package
  Plugin: vite:import-analysis
  File: /home/bphenriques/workspace/tauris-test-desktop/main.js
      at bail (/home/bphenriques/workspace/tauris-test-desktop/node_modules/vite/dist/node/chunks/dep-6e2fe41e.js:38597:8)
      at resolve (/home/bphenriques/workspace/tauris-test-desktop/node_modules/vite/dist/node/chunks/dep-6e2fe41e.js:38674:10)
      at resolveExports (/home/bphenriques/workspace/tauris-test-desktop/node_modules/vite/dist/node/chunks/dep-6e2fe41e.js:40982:12)
      at resolveDeepImport (/home/bphenriques/workspace/tauris-test-desktop/node_modules/vite/dist/node/chunks/dep-6e2fe41e.js:41000:31)
      at tryNodeResolve (/home/bphenriques/workspace/tauris-test-desktop/node_modules/vite/dist/node/chunks/dep-6e2fe41e.js:40773:20)
      at Context.resolveId (/home/bphenriques/workspace/tauris-test-desktop/node_modules/vite/dist/node/chunks/dep-6e2fe41e.js:40581:28)
      at async Object.resolveId (/home/bphenriques/workspace/tauris-test-desktop/node_modules/vite/dist/node/chunks/dep-6e2fe41e.js:39254:32)
      at async TransformContext.resolve (/home/bphenriques/workspace/tauris-test-desktop/node_modules/vite/dist/node/chunks/dep-6e2fe41e.js:39028:23)
      at async normalizeUrl (/home/bphenriques/workspace/tauris-test-desktop/node_modules/vite/dist/node/chunks/dep-6e2fe41e.js:58354:34)
      at async TransformContext.transform (/home/bphenriques/workspace/tauris-test-desktop/node_modules/vite/dist/node/chunks/dep-6e2fe41e.js:58509:57)

@bphenriques
Copy link
Author

Closing as likely a duplicate of #463 and #488

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants