tauri-apps / tauri

Build smaller, faster, and more secure desktop applications with a web frontend.
https://tauri.app
Apache License 2.0
78.1k stars 2.31k forks source link

[bug] Resources for nsis installer when cross compiled are added to installer.nsi script with current OS separators #9656

Closed jemmyw closed 2 weeks ago

jemmyw commented 2 weeks ago

Describe the bug

I found that when cross compiling / bundling from Mac for Windows, the resources I added that were not in the root directory ended up in the root directory after install on Windows, even though the subdirectories had been created.

The reason for this is that in the installer.nsi file it does this:

  {{#each resources}}
    File /a "/oname={{this.[1]}}" "{{@key}}"
  {{/each}}

Where this.[1] is a PathBuf generated by generate_resource_data in nsis.rs. When bundling on Windows this will render with a \ and when bundling on *nix it will bundle with a /. Nsis always expects the path separators to be a \ and it appears to just ignore /

I've fixed it for myself by adding a handlebars helper:

  handlebars.register_helper(
    "win-path",
    Box::new(
      |h: &handlebars::Helper<'_, '_>,
       _: &Handlebars<'_>,
       _: &handlebars::Context,
       _: &mut handlebars::RenderContext<'_, '_>,
       out: &mut dyn handlebars::Output|
       -> handlebars::HelperResult {
        let param = h.param(0).unwrap();
        let value = param.value().render();
        let path = value.replace(MAIN_SEPARATOR_STR, "\\");
        out.write(&path)?;
        Ok(())
      },
    ),
  );

and changing the installer.nsi line:

    File /a "/oname={{win-path this.[1]}}" "{{@key}}"

which might not be the optimal fix but it proves the problem.

Reproduction

Add a resource in a subdirectory like:

      "resources": [
        "db/structure.sql"
      ],

in tauri.conf.json and then bundle on windows as per https://tauri.app/v1/guides/building/cross-platform

Expected behavior

Resources should end up in the same location regardless of platform the installer is built on

Full tauri info output

[✔] Environment
    - OS: Mac OS 14.4.1 X64
    ✔ Xcode Command Line Tools: installed
    ✔ rustc: 1.77.2 (25ef9e3d8 2024-04-09)
    ✔ cargo: 1.77.2 (e52e36006 2024-03-26)
    ✔ rustup: 1.27.0 (bbb9276d2 2024-03-08)
    ✔ Rust toolchain: stable-aarch64-apple-darwin (default)
    - node: 20.8.1
    - pnpm: 9.0.4
    - npm: 10.1.0

[-] Packages
    - tauri [RUST]: 1.6.1
    - tauri-build [RUST]: 1.5.1
    - wry [RUST]: 0.24.7
    - tao [RUST]: 0.16.7
    - @tauri-apps/api : not installed!
    - @tauri-apps/cli [NPM]: 1.5.11 (outdated, latest: 1.5.12)


### Stack trace

_No response_

### Additional context

_No response_
FabianLars commented 2 weeks ago

according to tauri info your cli is 1.5.11, can you update that to 1.5.12 and try again? This should have been fixed as part of https://github.com/tauri-apps/tauri/pull/9281 which was part of 1.5.12 assuming of course i didn't miss anything...

jemmyw commented 2 weeks ago

Thanks, that looks like the fix I need!