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] Can not use system base directory variables in shell cmd #5480

Closed
ianho7 opened this issue Oct 25, 2022 · 7 comments
Closed

[bug] Can not use system base directory variables in shell cmd #5480

ianho7 opened this issue Oct 25, 2022 · 7 comments
Labels
scope: core Core packages of Tauri type: bug

Comments

@ianho7
Copy link

ianho7 commented Oct 25, 2022

Describe the bug

I want to use shell command api to run a executable file, here is my code:
`

const resourceDirPath = await resourceDir()
console.log(resourceDirPath = ${resourceDirPath})
const command = new Command('download', ['https://www.bilibili.com/video/BV1rP4y1U75S'])
command.on('close', (data) => {
  console.log(`command finished with code ${data.code} and signal ${data.signal}`)
})

command.on('error', (error) => console.error(`command error: "${error}"`))
command.stdout.on('data', (line) => {
  console.log(`command stdout: "${line}"`)
  if (line) {
    setLog(`${log}\n${line}`)
  }
})
command.stderr.on('data', (line) => console.log(`command stderr: "${line}"`))

const child = await command.spawn()
console.log('pid:', child.pid)

And here is my tauri.conf.json
"shell": { "all": true, "execute": true, "sidecar": true, "open": true, "scope": [{ "name": "download", "cmd": "$RESOURCE/sdk/you-get-darwin", "args": [{ "validator": "\\S+" }] }] }

This is result:
image

I check the resource directory(resourceDirPath = /Users/ianho/Documents/project/private/tauri-react-template/src-tauri/target/debug/
Index.tsx:61) and the executable file is exists.
image

Could you please tell my what's wrong with my code or the correct way to use tauri api, thank a lot!

Reproduction

No response

Expected behavior

No response

Platform and versions

Environment
› OS: Mac OS 12.3.1 X64
› Node.js: 16.13.0
› npm: 8.1.0
› pnpm: 7.13.5
› yarn: 1.22.10
› rustup: 1.25.1
› rustc: 1.64.0
› cargo: 1.64.0
› Rust toolchain: stable-x86_64-apple-darwin

Packages
› @tauri-apps/cli [NPM]: 1.1.1
› @tauri-apps/api [NPM]: 1.1.0
› tauri [RUST]: 1.0.5,
› tauri-build [RUST]: 1.0.4,
› tao [RUST]: 0.12.2,
› wry [RUST]: 0.19.0,

App
› build-type: bundle
› CSP: unset
› distDir: ../dist
› devPath: http://localhost:3000/
› framework: React

App directory structure
├─ dist
├─ node_modules
├─ src-tauri
├─ .git
└─ src

Stack trace

No response

Additional context

No response

@ianho7 ianho7 added status: needs triage This issue needs to triage, applied to new issues type: bug labels Oct 25, 2022
@FabianLars
Copy link
Sponsor Member

FabianLars commented Oct 25, 2022

Not 100% sure but i think the $xxx vars won't be resolved for the cmd property, so in this case it tries to open <cwd>/$RESOURCE/sdk/you-get-darwin. Nevermind, i'm wrong. Too early for me lol. Maybe it's related to this bug #5196 - a fix for this will be part of tauri 1.2

Since this is effectively what you're doing manually here, did you take a look at the sidecar feature? https://tauri.app/v1/guides/building/sidecar

@ianho7
Copy link
Author

ianho7 commented Oct 25, 2022

Not 100% sure but i think the $xxx vars won't be resolved for the cmd property, so in this case it tries to open <cwd>/$RESOURCE/sdk/you-get-darwin. Nevermind, i'm wrong. Too early for me lol. Maybe it's related to this bug #5196 - a fix for this will be part of tauri 1.2

Since this is effectively what you're doing manually here, did you take a look at the sidecar feature? https://tauri.app/v1/guides/building/sidecar

Thanks for response! sidebar feature is very useful!

@ianho7 ianho7 closed this as completed Oct 25, 2022
@JMMarchant
Copy link

JMMarchant commented Oct 28, 2022

I'm similarly running into this issue though can't use the sidecar due to the interplay between multiple binary files which all need to be colocated with each other).

Although it doesn't print the file path in the error message, I'm fairly certain you're almost right @FabianLars and the issue is that it's messing up the path in cmd and instead putting it in a literal $RESOURCE directory (tested this by printing out the resourceDir() path and then placing a simple "Hello World!" binary at resourceDir()/"$RESOURCE"/<rest of cmd path from scope> where it suddenly works).

i.e. for this example it would be at src-tauri/target/debug/$RESOURCE/sdk/you-get-darwin

@JMMarchant
Copy link

Have just tried it with $HOME in place of $RESOURCE and can confirm it's an issue with the $-vars not being substituted (despite the docs saying they are); instead of picking something up from my home directory it wasn't until I put it in the debug folder under a "$HOME" directory that it worked.

@FabianLars FabianLars reopened this Oct 28, 2022
@amrbashir amrbashir added scope: core Core packages of Tauri status: needs triage This issue needs to triage, applied to new issues and removed status: needs triage This issue needs to triage, applied to new issues labels Oct 29, 2022
@lucasfernog
Copy link
Member

We can surely improve the shell configuration to allow resolving the command path with the predefined variables, but be careful to not confuse the arbitrary shell command execution with a sidecar.
A sidecar is automatically read from the resources directory, so you can specify { name: "sdk/you-get-darwin", sidecar: true } instead of { name: 'download', cmd: '$RESOURCE/sdk/you-get-darwin }` as long as you append the target triple to the sidecar file name and you'll automatic sidecar path resolution and it'll be easier to make it cross platform.

@amrbashir amrbashir removed the status: needs triage This issue needs to triage, applied to new issues label Oct 29, 2022
@JMMarchant
Copy link

A sidecar is automatically read from the resources directory, so you can specify { name: "sdk/you-get-darwin", sidecar: true } instead of { name: 'download', cmd: '$RESOURCE/sdk/you-get-darwin }` as long as you append the target triple to the sidecar file name and you'll automatic sidecar path resolution and it'll be easier to make it cross platform.

Is it guaranteed that a sidecar always gets packaged into the resources directory? My "main" sidecar binary has some dynamically linked libraries that it needs to be colocated with so need to have those end up in the same place.

@lucasfernog
Copy link
Member

Not really @JMMarchant that's one of the limitations of a sidecar. We'll try to include this enhancement on v1.2.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
scope: core Core packages of Tauri type: bug
Projects
None yet
Development

No branches or pull requests

5 participants