plugins-workspace icon indicating copy to clipboard operation
plugins-workspace copied to clipboard

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

Open gknapp opened this issue 1 year ago • 9 comments

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.

gknapp avatar Oct 03 '24 13:10 gknapp

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

FabianLars avatar Oct 03 '24 13:10 FabianLars

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

glumn avatar Oct 03 '24 14:10 glumn

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

invalid args ridfor 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

gknapp avatar Oct 04 '24 14:10 gknapp

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

antkit avatar Oct 05 '24 11:10 antkit

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

will5933 avatar Oct 06 '24 06:10 will5933

@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/

glumn avatar Oct 07 '24 09:10 glumn

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

beeb avatar Oct 07 '24 09:10 beeb

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');

will5933 avatar Oct 07 '24 13:10 will5933

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

gknapp avatar Oct 07 '24 16:10 gknapp

Oh oops I opened https://github.com/tauri-apps/plugins-workspace/issues/1931 before seeing this. But it seems that createStore is now the blessed api? So we should update the readme here to match that?

But for autoSave, it seems like the rust is using it as a u64 because it's using it to define debounce. But those changes aren't reflected in the JS, and I'm not sure which we should want to do:

~~a. change types/docs to match this behavior in js (autoSave is now a boolean, describe it to be used for debounce)~~ ~~b. transform the autoSave boolean in js, and properly pass an int to bridge~~ ~~c. change the rust to treat them as two different properties~~

edit:

It looks like more changes are in the pipeline already, and I see this change being reflected in those changes, so probably this will be more clear after this pr.

* Auto save on modification with debounce duration in milliseconds, it's 100ms by default, pass in `false` to disable it
    */
   autoSave?: boolean | number

bpevs avatar Oct 14 '24 03:10 bpevs

I also encountered this issue, should we update this version?

imddc avatar Oct 14 '24 12:10 imddc

I am currently experiencing the same problem. I have implemented the code as documented below, but it fails when I run load().

Rust

let config_path = PathBuf::from("config.bin");
let store =  app.store( config_path );
if store.load().is_err() {
  store.set("general".to_string(), json!(INITIAL_CONFIG));
  store.save().unwrap();
}

TypeScript

import { type Store, load as loadStore } from "@tauri-apps/plugin-store";
/// --------
const store = useRef<Store | null>( null );
store.current = await loadStore( CONFIG_File );

TAURI info

[✔] Environment
    - OS: Windows 10.0.22631 x86_64 (X64)
    ✔ WebView2: 129.0.2792.89
    ✔ MSVC:
        - Visual Studio Build Tools 2022
        - Visual Studio Community 2022
    ✔ rustc: 1.82.0 (f6e511eec 2024-10-15)
    ✔ cargo: 1.82.0 (8f40fc59f 2024-08-21)
    ✔ rustup: 1.27.1 (54dd3d00f 2024-04-24)
    ✔ Rust toolchain: stable-x86_64-pc-windows-msvc (default)
    - node: 20.12.1
    - pnpm: 9.12.2
    - npm: 10.5.0

[-] Packages
    - tauri 🦀: 2.0.6
    - tauri-build 🦀: 2.0.2
    - wry 🦀: 0.46.1
    - tao 🦀: 0.30.3
    - @tauri-apps/api : 2.0.3
    - @tauri-apps/cli : 2.0.5

[-] Plugins
    - tauri-plugin-dialog 🦀: 2.0.3
    - @tauri-apps/plugin-dialog : 2.0.1
    - tauri-plugin-window-state 🦀: 2.0.1
    - @tauri-apps/plugin-window-state : not installed!
    - tauri-plugin-shell 🦀: 2.0.2
    - @tauri-apps/plugin-shell : 2.0.1
    - tauri-plugin-stronghold 🦀: 2.0.1
    - @tauri-apps/plugin-stronghold : 2.0.0
    - tauri-plugin-http 🦀: 2.0.3
    - @tauri-apps/plugin-http : 2.0.1
    - tauri-plugin-sql 🦀: 2.0.1
    - @tauri-apps/plugin-sql : 2.0.0
    - tauri-plugin-fs 🦀: 2.0.3
    - @tauri-apps/plugin-fs : 2.0.1
    - tauri-plugin-store 🦀: git+https://github.com/tauri-apps/plugins-workspace?branch=v2#7e1c17a635f10d69e10ea9f35081a68c71276203 (2.0.1)
    - @tauri-apps/plugin-store : 2.1.0
    - tauri-plugin-deep-link 🦀: 2.0.1
    - @tauri-apps/plugin-deep-link : not installed!
    - tauri-plugin-process 🦀: 2.0.1
    - @tauri-apps/plugin-process : 2.0.0
    - tauri-plugin-single-instance 🦀: 2.0.1
    - @tauri-apps/plugin-single-instance : not installed!
    - tauri-plugin-global-shortcut 🦀: 2.0.1
    - @tauri-apps/plugin-global-shortcut : 2.0.0

[-] App
    - build-type: bundle
    - CSP: connect-src 'self' https://google.github.io/magika/model/ https://www.youtube.com/ https://googleads.g.doubleclick.net/ ipc: http://ipc.localhost; worker-src 'self' blob:; frame-src 'self' https://www.youtube.com/embed/; style-src 'self' 'unsafe-hashes' 'sha256-QLk7hiiq3b6XVhLz2U+7CDM2vHCse7K+ELeFYemxS1w='; style-src-elem 'self' 'nonce-idn354fskf25dt' 'sha256-47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU=' 'sha256-ejxaNqI9HE4qAX7B2hAfln6pnS37ktMxKSd3kZCKrvU=' 'sha256-nzTgYzXYDNe6BAHiiI7NNlfK8n/auuOAhh2t92YvuXo='; script-src 'self'; default-src 'self'; img-src * blob: blob: data:
    - frontendDist: ../dist
    - devUrl: http://localhost:1420/
    - framework: React
    - bundler: Vite

ryusei-48 avatar Oct 22 '24 06:10 ryusei-48

Your rust side store plugin's version seems to be before the most recent rework, do you mind upgrading it to the latest and try again?

Legend-Master avatar Oct 22 '24 07:10 Legend-Master

Thank you very much. I set the version of the store plugin to fine center and the problem is solved.

ryusei-48 avatar Oct 22 '24 08:10 ryusei-48

Thank you very much.  I set the version of the store plugin to fine center and the problem is solved. hello which version ?2.1.0?

643364103 avatar Nov 23 '24 06:11 643364103

Yes, 2.1.0, both js package and rust crate

Legend-Master avatar Nov 23 '24 06:11 Legend-Master