Skip to content

[bug] Relative paths in version field of tauri.config.json is not correctly parsed by generate_context!() since fa028ebf #4723

Closed

Description

@shniubobo

Describe the bug

The version field should accept paths to package.json. This works with the default directory layout as is generated by create-tauri-app, but may fail in cli.js>=1.0.3 when the layout is changed.

Reproduction

  1. Clone this repository: https://github.com/shniubobo/tauri-cwd-bug

  2. cd into it, and

pnpm i -r
pnpm tauri dev

It fails with

error: `package > version` must be a semver string
 --> src\tauri\src\main.rs:8:10
  |
8 |     .run(tauri::generate_context!())
  |          ^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = note: this error originates in the macro `tauri::generate_context` (in Nightly builds, run with -Z macro-backtrace for more info)

error: could not compile `app` due to previous error
  1. Make these changes:
--- a/src/frontend/package.json
+++ b/src/frontend/package.json
@@ -8,6 +8,6 @@
     "@tauri-apps/api": "^1.0.2"
   },
   "devDependencies": {
-    "@tauri-apps/cli": "1.0.3"
+    "@tauri-apps/cli": "1.0.4"
   }
 }

And run:

pnpm up -r 
pnpm tauri dev

It still fails.

  1. Make these changes:
--- a/src/frontend/package.json
+++ b/src/frontend/package.json
@@ -8,6 +8,6 @@
     "@tauri-apps/api": "^1.0.2"
   },
   "devDependencies": {
-    "@tauri-apps/cli": "1.0.3"
+    "@tauri-apps/cli": "1.0.2"
   }
 }

And run:

pnpm up -r 
pnpm tauri dev

It now succeeds.

Expected behavior

Step two and three should succeed.

Platform and versions

Environment
  › OS: Windows 10.0.19044 X64
  › Webview2: 103.0.1264.62
  › MSVC:
      - Visual Studio Community 2022
  › Node.js: 18.6.0
  › npm: 8.13.2
  › pnpm: 7.5.2
  › yarn: 1.22.19
  › rustup: 1.25.1
  › rustc: 1.62.0
  › cargo: 1.62.0
  › Rust toolchain: stable-x86_64-pc-windows-msvc

Packages
WARNING: no lock files found, defaulting to npm
  › @tauri-apps/cli [NPM]: 1.0.3 (outdated, latest: 1.0.4)
  › @tauri-apps/api [NPM]: Not installed!
  › tauri [RUST]: 1.0.4,
  › tauri-build [RUST]: 1.0.4,
  › tao [RUST]: 0.12.1,
  › wry [RUST]: 0.19.0,

App

App directory structure
  ├─ .git
  ├─ node_modules
  ├─ src
  └─ target

Stack trace

No response

Additional context

Please note that the bug affects generate_context!() only. The build script does not fail, which means it can correctly parse the relative path. As an aside, if a non-existing path were provided, the build script would fail:

--- a/src/tauri/tauri.conf.json
+++ b/src/tauri/tauri.conf.json
@@ -8,7 +8,7 @@
   },
   "package": {
     "productName": "tauri-cwd-bug",
-    "version": "../frontend/package.json"
+    "version": "../does-not-exist/package.json"
   },
   "tauri": {
     "allowlist": {
$ cargo build
[...]
error: failed to run custom build command for `app v0.1.0 ([...]\src\tauri)`

Caused by:
  process didn't exit successfully: `[...]\target\debug\build\app-1cf83bec3ab9949d\build-script-build` (exit code: 1)
  --- stdout
  cargo:rerun-if-env-changed=TAURI_CONFIG
  cargo:rerun-if-changed=tauri.conf.json
  `package > version` must be a semver string
warning: build failed, waiting for other jobs to finish...

The bug is introduced by commit fa028eb.

Before this commit, the config is parsed only by cli.js. It always sets the working directory to where tauri.config.json is (see below), so it can successfully parse the relative path.

set_current_dir(&tauri_path).with_context(|| "failed to change current working directory")?;

After this commit, however, the config is parsed partly by cli.js, and partly by tauri_codegen::get_config. The working directory is not set before the latter is called by generate_context, and therefore the relative path cannot be parsed.

I am working on a fix now, and I will create a PR afterwards.

Activity

  1. added a commit that references this issue on Jul 20, 2022
    9cd6cf2
  2. added a commit that references this issue on Dec 9, 2024
    accbc5e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions