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

CLI app discovery broken under certain circumstances (utmctl symlink) #6628

Open
lishaduck opened this issue Aug 27, 2024 · 8 comments
Open

Comments

@lishaduck
Copy link

lishaduck commented Aug 27, 2024

Describe the issue
I don't have admin permissions, but I do have homebrew, so I installed UTM via homebrew with --appdir=~/Applications/. utmctl, however, errors as follows:

2024-08-15 20:28:29.203 utmctl[28511:15501686] Can't find app with file path /Applications/UTM.app
Error: Application not found.

I filed an issue over at Homebrew/homebrew-cask#182568, and I was directed to file an issue here.

Configuration

  • UTM Version: 4.5.3 (I'm about to upgrade, I'll update it if it fixes it)
  • macOS Version: 14.6.1
  • Mac Chip: M1

Crash log
n/a

Debug log
n/a

Upload VM
n/a


I haven't written Swift in ages, but I'm happy to try to fix it.

@osy
Copy link
Contributor

osy commented Aug 27, 2024

How are you calling utmctl?

@lishaduck
Copy link
Author

How are you calling utmctl?

Oh, silly me 😅

$ utmctl list
2024-08-27 11:05:23.455 utmctl[37164:17429559] Can't find app with file path /Applications/UTM.app
Error: Application not found.

@lishaduck lishaduck changed the title CLI Symlink Resolution broken CLI app discovery broken under certain circumstances (utmctl symlink) Aug 27, 2024
@osy
Copy link
Contributor

osy commented Aug 27, 2024

Can you run ls -l $(which utmctl) and paste the output?

@lishaduck
Copy link
Author

lishaduck commented Aug 27, 2024

lrwxr-xr-x  1 username  admin  58 Aug 27 11:00 /opt/homebrew/bin/utmctl -> /Users/username/Applications/UTM.app/Contents/MacOS/utmctl

@osy
Copy link
Contributor

osy commented Aug 27, 2024

I've been playing around with this and it seems like it's actually not possible for a sandboxed CLI app to know what's its own real path is.

    /// Find the path to UTM.app
    private var utmAppUrl: URL {
        if let executableURL = Bundle.main.executableURL?.resolvingSymlinksInPath() {
            let utmURL = executableURL.deletingLastPathComponent().deletingLastPathComponent().deletingLastPathComponent()
            if utmURL.lastPathComponent == "UTM.app" {
                return utmURL
            }
        }
        return URL(fileURLWithPath: "/Applications/UTM.app")
    }

This returns the default path if it cannot determine the correct path. However, when you symlink, Bundle.main.executableURL returns nil and Bundle.main.bundleURL returns nil as well. CommandLine.arguments.first returns a sandboxed path /Users/username/Library/Containers/com.utmapp.utmctl/Data/utmctl. I don't know any other way to do it.

The workaround is if you add /Users/username/Applications/UTM.app/Contents/MacOS to your PATH variable. For example by creating a new entry in /etc/paths.d/

@lishaduck
Copy link
Author

I've been playing around with this and it seems like it's actually not possible for a sandboxed CLI app to know what's its own real path is.

That's too bad, but thanks for investigating!

CommandLine.arguments.first returns a sandboxed path /Users/username/Library/Containers/com.utmapp.utmctl/Data/utmctl.

I never knew what ~/Library/Containers contained, and now I do, so at least I learned something :)

The workaround is if you add /Users/username/Applications/UTM.app/Contents/MacOS to your PATH variable.

Yeah, I figured that'd probably end up being the solution, though I'd rather it not be (my .zshrc file is complicated enough as it is, but at least I know where to fix it)
I assumed that if vscode could do it, it couldn't be too hard, but vscode isn't sandboxed. For that matter, why is UTM sandboxed? It's not distributed via the mas.

@osy
Copy link
Contributor

osy commented Aug 27, 2024

It is indeed distributed on MAS. And also because of the added security (if there's a QEMU bug, it won't automatically be able to rw your entire drive).

@lishaduck
Copy link
Author

It is indeed distributed on MAS.

Oh, yeah 🤦‍♂️

And also because of the added security (if there's a QEMU bug, it won't automatically be able to rw your entire drive).

I'm glad to hear that 😅

@github-staff github-staff deleted a comment from yiweifengyan Oct 23, 2024
@github-staff github-staff deleted a comment from yiweifengyan Oct 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants