tauri-apps / plugins-workspace

All of the official Tauri plugins in one place!
https://tauri.app
Apache License 2.0
884 stars 245 forks source link

After upgrade to v2: invalid args `rid` for command `get`: command get missing required key rid #1877

Open gknapp opened 4 days ago

gknapp commented 4 days ago

I'm seeing many of these console errors in the webview2 inspector after updating tauri and tauri-plugin-store from 2.0.0-rc to 2.0.0 final. The error points to the plugin-store, or my call point: store.get("my-key"). No code changed, just Cargo and npm package upgrades to @tauri-apps v2 release.

My code:

import { Store } from "@tauri-apps/plugin-store"

const Setting = {
  editorOpen: "editor-visible",
  windowSplit: "window-split"
}

class WindowSettings {
  #defaults
  #store

  constructor(defaults, store = new Store(".settings.dat")) {
    this.#defaults = defaults
    this.#store = store
  }

  #saveSetting(key, value) {
    this.#store.set(key, value).then(() => this.#store.save())
  }

  /**
   * Return bounded split value in pixels
   * @returns number
   */
  getSplit = () =>
    this.#store.get(Setting.windowSplit).then(split =>
      typeof split !== "number"
        ? this.#defaults.split.min
        : Math.min(
            Math.max(split, this.#defaults.split.min),
            this.#defaults.split.max
          )
    )

  setSplit = px => {
    this.#saveSetting(Setting.windowSplit, px)
  }
}

image

I've tried changing my key names and store file (.dat), neither helped.

FabianLars commented 4 days ago

Can you post the output of the tauri info command please?

mrguiman commented 4 days ago

This method of declaring store no longer works: https://v2.tauri.app/plugin/store/.

I've followed the migration but I'm now stuck because the create_store command apparently expects a u64 rather than a boolean

Unhandled Promise Rejection: invalid args `autoSave` for command `create_store`: invalid type: boolean `true`, expected u64

which has already been mentionned here and can be circumvented by using a number instead of a boolean when declaring the autoSave option

Some issues with stable it seems

gknapp commented 3 days ago

Further to yesterday, I'm now seeing this error on Store construction:

invalid argsridfor commandget: invalid type: string ".settings.dat", expected u32

tauri info:

[✔] Environment
    - OS: Windows 10.0.22631 x86_64 (X64)
    ✔ WebView2: 129.0.2792.79
    ✔ MSVC: Visual Studio Build Tools 2022
    ✔ rustc: 1.78.0 (9b00956e5 2024-04-29)
    ✔ cargo: 1.78.0 (54d8815d0 2024-03-26)
    ✔ rustup: 1.27.1 (54dd3d00f 2024-04-24)
    ✔ Rust toolchain: stable-x86_64-pc-windows-msvc (default)
    - node: 20.10.0
    - npm: 10.2.3

[-] Packages
    - tauri 🦀: 2.0.0
    - tauri-build 🦀: 2.0.0
    - wry 🦀: 0.44.1
    - tao 🦀: 0.30.2
    - @tauri-apps/api : 2.0.1
    - @tauri-apps/cli : 2.0.0-rc.18 (outdated, latest: 2.0.1)

[-] Plugins
    - tauri-plugin-http 🦀: 2.0.0
    - @tauri-apps/plugin-http : 2.0.0
    - tauri-plugin-dialog 🦀: 2.0.0
    - @tauri-apps/plugin-dialog : 2.0.0
    - tauri-plugin-fs 🦀: 2.0.0
    - @tauri-apps/plugin-fs : 2.0.0
    - tauri-plugin-store 🦀: 2.0.0
    - @tauri-apps/plugin-store : 2.0.0
    - tauri-plugin-shell 🦀: 2.0.0
    - @tauri-apps/plugin-shell : 2.0.0
    - tauri-plugin-window-state 🦀: 2.0.0
    - @tauri-apps/plugin-window-state : 2.0.0

[-] App
    - build-type: bundle
    - CSP: default-src 'self' data: blob:; script-src 'self' 'unsafe-inline' http://localhost:*; style-src 'self' 'unsafe-inline'; connect-src 'self' ipc: http://ipc.localhost http://localhost:*; img-src 'self' data: http: https:
    - frontendDist: ../dist
    - devUrl: http://localhost:3055/
    - framework: React
    - bundler: Rollup
antkit commented 3 days ago

I can't load data after upgrading from v2.0.0-rc to v2.0.1 . The console prints errors as gknapp said.

will5933 commented 2 days ago

the same problem😭

Uncaught invalid args `rid` for command `get`: invalid type: string "settings.bin", expected u32
[✔] Environment
    - OS: Windows 10.0.22631 x86_64 (X64)
    ✔ WebView2: 129.0.2792.79
    ✔ MSVC: Visual Studio Community 2022
    ✔ rustc: 1.81.0 (eeb90cda1 2024-09-04)
    ✔ cargo: 1.81.0 (2dbb1af80 2024-08-20)
    ✔ rustup: 1.27.1 (54dd3d00f 2024-04-24)
    ✔ Rust toolchain: stable-x86_64-pc-windows-msvc (environment override by RUSTUP_TOOLCHAIN)

[-] Packages
    - tauri 🦀: 2.0.1
    - tauri-build 🦀: 2.0.1
    - wry 🦀: 0.44.1
    - tao 🦀: 0.30.3
    - tauri-cli 🦀: 2.0.1

[-] Plugins
    - tauri-plugin-store 🦀: 2.0.1
    - tauri-plugin-fs 🦀: 2.0.1
    - tauri-plugin-autostart 🦀: 2.0.1
    - tauri-plugin-dialog 🦀: 2.0.1
    - tauri-plugin-single-instance 🦀: 2.0.1
    - tauri-plugin-shell 🦀: 2.0.1

[-] App
    - build-type: bundle
    - CSP: unset
    - frontendDist: ../src
mrguiman commented 1 day ago

@will5933 @antkit @gknapp

You guys need to change the way you declare the store after updating to plugin-store v2 -> https://v2.tauri.app/plugin/store/

beeb commented 1 day ago

You guys need to change the way you declare the store after updating to plugin-store v2 -> https://v2.tauri.app/plugin/store/

The bug still happens for me with this code snippet

will5933 commented 22 hours ago

Thanks, it works.

from

const clipboardStore = new window.__TAURI_PLUGIN_STORE__.Store('clipboard.bin');

to

const { createStore } = window.__TAURI__.store;
// ...
const clipboardStore = await createStore('clipboard.bin');
gknapp commented 19 hours ago

I've updated my code to follow the V2 docs, this generated a new error passing the { autoSave: true } option object:

Uncaught (in promise) invalid args autoSave for command create_store: invalid type: boolean true, expected u64

I omitted that option and just call store.save() after setting a value. Update: { autoSave: 1 } is accepted, no console errors.

I updated my capabilities for the plugin too (to use store:default) and rewrote the constructor of my class. I no longer construct the store if it's not passed in. I moved store construction to a hook, as createStore() is async.

const Setting = {
  windowSplit: "window:split"
}

class WindowSettings {
  #defaults
  #store

  constructor(defaults, store) {
    this.#defaults = defaults
    this.#store = store
  }

  #saveSetting(key, value) {
    this.#store.set(key, value).then(() => this.#store.save())
  }

  getSplit = () =>
    this.#store.get(Setting.windowSplit).then(split =>
      typeof split !== "number"
        ? this.#defaults.split.min
        : Math.min(
            Math.max(split, this.#defaults.split.min),
            this.#defaults.split.max
          )
    )

  setSplit = px => {
    this.#saveSetting(Setting.windowSplit, px)
  }
   ...
}

I use a hook for store instantiation and updated my app to show a loading component if the store isn't set / ready.

import React from "react"
import { createStore } from "@tauri-apps/plugin-store"
import Settings from "../settings"

function useSettings(defaults, storeFile = "settings.bin") {
  const [settings, setSettings] = React.useState(null)

  React.useEffect(() => {
    const initSettings = async () => {
      const store = await createStore(storeFile)
      setSettings(new Settings(defaults, store))
    }
    initSettings()
  }, [])

  return settings
}

export default useSettings

Conditional render in App:

  ...
  const defaultSettings = React.useMemo(() => {
    return {
      split: {
        max: leftSideMax + resizeBarWidth,
        min: minWidth
      }
    }
  }, [minWidth, leftSideMax, resizeBarWidth])
  const settings = useWindowSettings(defaultSettings)
  ...
  if (!settings) {
    return (<Loading />)
  }
  ...
  // render app UI as usual, settings available