LinusU / node-appdmg

💾 Generate your app dmgs
MIT License
1.68k stars 152 forks source link

Error: Command failed: hdiutil detach #142

Closed gmcdev closed 1 year ago

gmcdev commented 7 years ago

I have been using this package, via electron-builder for almost a year with no problems. Recently, though, I started getting this error when it tries to unmount the temporary disk image.

Tracked it down to https://github.com/LinusU/node-appdmg/blob/master/lib/hdiutil.js#L64, exitCode is now just code, so the retry after one second was not happening.

However, even after I fixed that, it was still failing intermittently, so I put it on a setInterval to make 5 attempts on 2sec intervals, or until it succeeds.

Seems to be working.

LinusU commented 5 years ago

Probably need some kind of backoff with a retry-strategy here...

aymather commented 4 years ago

Came here from #143 I'd like to propose the fix I mentioned there.

Both fixes are definitely workarounds and probably not a long term solution. But I'd just like to get the ball rolling on this topic because this package is still breaking on my computer with this error.

Inside lib/hdiutil.js

Solution 1: Set a longer timeout to free up the resource (check out this forum for more information)

exports.detach = function (path, cb) {
  const args = ['detach', path]

  setTimeout(() => {
    util.sh('hdiutil', args, function (err) {
      if (err && err.exitCode === 16 && /Resource busy/.test(err.stderr)) {
        setTimeout(function () {
          util.sh('hdiutil', args, (err) => cb(err))
        }, 1000)
      } else {
        cb(err)
      }
    })
  }, 5000)
}

Solution 2: Forcibly detach the image (I tested this and it did not noticeably affect my app)

exports.detach = function (path, cb) {
  const args = ['detach', path, '-force']

    util.sh('hdiutil', args, function (err) {
      if (err && err.exitCode === 16 && /Resource busy/.test(err.stderr)) {
        setTimeout(function () {
          util.sh('hdiutil', args, (err) => cb(err))
        }, 1000)
      } else {
        cb(err)
      }
    })
}
johanpoirier commented 4 years ago

Any update on this? I have the same issue since I upgraded MacOS X to Catalina.

jason-crawford-xio commented 1 year ago

Update: I'm withdrawing this suggestion. It assumed the unmounting was just for the sake of cleaning up at the end of the build, but upon further investigating, it appears that the creation of the dmg will not work if the drive is still mounted.

I'm experiencing this problem as well. The culprit in my case is a security scanner that is snooping the mount and preventing it from unmounting for a few minutes beyond the end of the failed build.

Ideally

An alternative might be leave a command for the developer to invoke later to clean up once the scan is complete. And of course a warning message to suggest invoking it.

LinusU commented 1 year ago

This should be fixed by #214 released as 🚢 0.6.5 / 2023-02-01

othyn commented 1 year ago

@LinusU hate to be the barer of bad news, but I don't think that fixed it.

I've made a bug report in a consuming library, https://github.com/sindresorhus/create-dmg which uses "appdmg": "^0.6.6", and I'm still getting this error which appears to originate from this library.

This only occured when moving from a macOS 12 to a macOS 13 runner for GitHub Actions, see:

https://github.com/othyn/macos-auto-clicker/actions/workflows/cicd.yml

You can see all the failed builds, with only one of them being randomly successful for both staging and main. I've not been able to decipher why that may be the case, as the log output is limited. I've diff'd the logs and nothing out of the ordinary is different. There doesn't appear to be any repeatable behavior between runs that I can pick appart as to why they'd fail. Yet the builds will randomly fail with:

[21:10:07]: $ ../node_modules/.bin/create-dmg ../build/AutoClicker.app --overwrite ../build
[21:10:08]: â–¸ - Creating DMG
[21:10:14]: ▸ ✖ Building the DMG failed. Error: Command failed: hdiutil detach /Volumes/AutoClicker
[21:10:14]: â–¸ hdiutil: detach failed - No such file or directory
[21:10:14]: Exit status of command '../node_modules/.bin/create-dmg ../build/AutoClicker.app --overwrite ../build' was 1 instead of 0.
- Creating DMG
✖ Building the DMG failed. Error: Command failed: hdiutil detach /Volumes/AutoClicker
hdiutil: detach failed - No such file or directory

The context of the action is running within Fastlane, which is just a build chain task runner for Xcode. sindresorhus/create-dmg is an opinionated wrapper around this library setting a load of defaults, with all errors being surfaced from this library.

Finding information online about this is pretty impossible, especially for debugging.

Any ideas?

elneruda commented 1 year ago

Hello @othyn

I'm currently encountering the same issue running a very close configuration

Error: Command failed: hdiutil detach /Volumes/MyApp
hdiutil: detach failed - No such file or directory

I'm very interested on evolution of this issue

yuvalkarmi commented 7 months ago

@LinusU I'm also experiencing the same issue, and it's happening completely randomly on github actions. about 1/5 builds randomly fails. Using 0.6.6.

Edit: I got it! Turns out the error is exactly what it says it is: in some cases, the volume has already been unmounted, so check before attempting to unmount. See here:

https://github.com/LinusU/node-appdmg/issues/229#issuecomment-1905123880

othyn commented 7 months ago

@yuvalkarmi awesome that a fix has been submitted, still just need to wait for that to be merged though and then a dependency bump on this repo to use it.

@elneruda I gave up doing DMG builds a long time ago as there just wasn't a reliable option. I just zipped the app instead:

  # https://docs.fastlane.tools/actions/zip
  output_archive_path = zip(
    path: "build/AutoClicker.app",
    output_path: "build/AutoClicker.zip",
    verbose: true
  )