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

[bug] Tauri v2 asset_resolver is empty for dev #10385

Closed
mildred opened this issue Jul 25, 2024 · 1 comment
Closed

[bug] Tauri v2 asset_resolver is empty for dev #10385

mildred opened this issue Jul 25, 2024 · 1 comment
Labels
status: needs triage This issue needs to triage, applied to new issues type: bug

Comments

@mildred
Copy link

mildred commented Jul 25, 2024

Describe the bug

For Tauri v2, in rust code, and only when running in dev, the app.asset_resolver object have no assets (iter over them yields nothing). However in production, the application assets are correctly listed.

In particular, the localhost plugin does not work in dev

Reproduction

$ npm create tauri-app@latest -- --beta

> npx
> create-tauri-app --beta

✔ Project name · tauri_app_test
✔ Choose which language to use for your frontend · TypeScript / JavaScript - (pnpm, yarn, npm, bun)
✔ Choose your package manager · npm
✔ Choose your UI template · Vanilla
✔ Choose your UI flavor · TypeScript
✔ Would you like to setup the project for mobile as well? · no

Template created! To get started run:
  cd tauri_app_test
  npm install
  npm run tauri dev

$ cd tauri_app_test
$ npm install
$ sed -i s/com.tauri.dev/test-app.example.org/ src-tauri/tauri.conf.json

create assets:

$ mkdir -p public
$ touch public/public-asset.js src/src-asset.js

update index.html to include both assets:

    <script src="/src/src-asset.js" defer></script>
    <script src="/public-asset.js" defer></script>

in src/main.rs:

// Prevents additional console window on Windows in release, DO NOT REMOVE!!
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]

// Learn more about Tauri commands at https://tauri.app/v1/guides/features/command
#[tauri::command]
fn greet(name: &str) -> String {
    format!("Hello, {}! You've been greeted from Rust!", name)
}

fn main() {
    tauri::Builder::default()
        .plugin(tauri_plugin_shell::init())
        .invoke_handler(tauri::generate_handler![greet])
        .setup(|app| {
            let asset_resolver = app.asset_resolver();
            println!("asset list start");
            for asset in asset_resolver.iter() {
                println!("asset: {}", asset.0);
            }
            println!("asset list stop");
            Ok(())
        })
        .run(tauri::generate_context!())
        .expect("error while running tauri application");
}

Running npm run tauri dev:

asset list start
asset list stop

In the DOM inspector, network tab, both assets are loaded fine though

Running npm run tauri build followed by running the built executable ./src-tauri/target/release/tauri_app_test:

asset list start
asset: /assets/index-0M1iEA7z.js
asset: /index.html
asset: /public-asset.js
asset: /assets/index-XlHJ6GfY.css
asset list stop

src-asset.js is not there, but if we configure rollup to load this asset it will appear, in vite.config.js, set:

import { defineConfig } from "vite";

// https://vitejs.dev/config/
export default defineConfig(async () => ({

  // Vite options tailored for Tauri development and only applied in `tauri dev` or `tauri build`
  //
  // 1. prevent vite from obscuring rust errors
  clearScreen: false,
  // 2. tauri expects a fixed port, fail if that port is not available
  server: {
    port: 1420,
    strictPort: true,
    watch: {
      // 3. tell vite to ignore watching `src-tauri`
      ignored: ["**/src-tauri/**"],
    },
  },

  build: {
    rollupOptions: {
      input: {
        index: './index.html',
        src_asset: 'src/src-asset.js',
      }
    }
  }
}));

Now, running the built app, it shows the asset:

asset list start
asset: /public-asset.js
asset: /assets/index-XlHJ6GfY.css
asset: /index.html
asset: /assets/src_asset-BSKcq9xs.js
asset: /assets/index-0M1iEA7z.js
asset list stop

Expected behavior

same assets shown for dev and for build

Full tauri info output

[✔] Environment
    - OS: Fedora 40 X64
    ✔ webkit2gtk-4.1: 2.44.2
    ✔ rsvg2: 2.57.1
    ✔ rustc: 1.79.0 (129f3b996 2024-06-10)
    ✔ cargo: 1.79.0 (ffa9cf99a 2024-06-03)
    ✔ rustup: 1.27.1 (54dd3d00f 2024-04-24)
    ✔ Rust toolchain: stable-x86_64-unknown-linux-gnu (default)
    - node: 22.4.1
    - yarn: 1.22.22
    - npm: 10.8.1

[-] Packages
    - tauri [RUST]: 2.0.0-beta.24
    - tauri-build [RUST]: 2.0.0-beta.19
    - wry [RUST]: 0.41.0
    - tao [RUST]: 0.28.1
    - @tauri-apps/api [NPM]: 2.0.0-beta.15
    - @tauri-apps/cli [NPM]: 2.0.0-beta.22

[-] App
    - build-type: bundle
    - CSP: unset
    - frontendDist: ../dist
    - devUrl: http://localhost:1420/
    - framework: React
    - bundler: Rollup

Stack trace

No response

Additional context

I am running a HTTP webserver in rust, triggered from the javascript side, and aimed at serving files for the system browser in order to print them. I need those assets to be served to this web server like the localhost plugin. This is to workaround the lack of print options in Tauri (and WebkitGtk). The app I am working on is : https://github.com/mildred/web_docs

@mildred mildred added status: needs triage This issue needs to triage, applied to new issues type: bug labels Jul 25, 2024
@FabianLars
Copy link
Member

Thanks for the extensive report! I'd say this counts as a duplicate of #8411 though, but PRs for that are already open.

@FabianLars FabianLars closed this as not planned Won't fix, can't repro, duplicate, stale Jul 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: needs triage This issue needs to triage, applied to new issues type: bug
Projects
None yet
Development

No branches or pull requests

2 participants