Shared directories configuration not running on macOS virtual machine

When using the sample code included on the Running macOS in a virtual machine on Apple silicon. I am adding the following changes to the swift files:

Added to 'MacOSVirtualMachineConfigurationHelper' file:

static func createAutomountSingleDirectoryShareDeviceConfiguration() -> VZVirtioFileSystemDeviceConfiguration {
        let sharedDirectory = VZSharedDirectory(url: directoryURL, readOnly: false)
        let singleDirectoryShare = VZSingleDirectoryShare(directory: sharedDirectory)


        // Assign the automount tag to this share. macOS shares automounted directories automatically under /Volumes in the guest.
        let sharingConfiguration = VZVirtioFileSystemDeviceConfiguration(tag: VZVirtioFileSystemDeviceConfiguration.macOSGuestAutomountTag)
        sharingConfiguration.share = singleDirectoryShare


        return sharingConfiguration
    }

Added to 'path' file:

let directoryURL = URL(fileURLWithPath: NSHomeDirectory() + "Projects")

Added to the 'AppDelegate' file:

virtualMachineConfiguration.directorySharingDevices = [MacOSVirtualMachineConfigurationHelper.createAutomountSingleDirectoryShareDeviceConfiguration()]

When the above is added and the sample app is run, the following error is shown:

macOSVirtualMachineSampleApp/AppDelegate.swift:95: Fatal error: Virtual machine failed to start with Error Domain=VZErrorDomain Code=2 "A directory sharing device configuration is invalid." UserInfo={NSLocalizedFailure=Invalid virtual machine configuration., NSLocalizedFailureReason=A directory sharing device configuration is invalid., NSUnderlyingError=0x600000c343c0 {Error Domain=NSPOSIXErrorDomain Code=2 "No such file or directory"}}

On the host device the directory that is being shared is ~/Projects and it does exist.

What do I need to change to create the shared directory and have it work?

Is there a sample code project for the same configuration that was shown in the demo?

Answered by DTS Engineer in 785046022
let directoryURL = URL(fileURLWithPath: NSHomeDirectory() + "Projects")

NSHomeDirectory() doesn’t including a trailing slash (/), so this code doesn’t do what you expect.

A better way to create a home directory relative URL is:

let directoryURL = FileManager.default
    .homeDirectoryForCurrentUser
    .appending(path: "Projects", directoryHint: .isDirectory)

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Accepted Answer
let directoryURL = URL(fileURLWithPath: NSHomeDirectory() + "Projects")

NSHomeDirectory() doesn’t including a trailing slash (/), so this code doesn’t do what you expect.

A better way to create a home directory relative URL is:

let directoryURL = FileManager.default
    .homeDirectoryForCurrentUser
    .appending(path: "Projects", directoryHint: .isDirectory)

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Quinn, thank you for the help there, that fixed it. I am just starting to learn Xcode and Swift, this solution works. Thank you again. :)

Shared directories configuration not running on macOS virtual machine
 
 
Q