JohnSundell / ShellOut

Easily run shell commands from a Swift script or command line tool
MIT License
870 stars 83 forks source link

ShellOut "swift package update" not working in some scenarios #36

Open thecb4 opened 6 years ago

thecb4 commented 6 years ago

I'm working on using Swift in some pre-commit scripts. For some reason when I attempted to run various versions of "swift package update" as a script for git pre-commit I would get a failure.

// swift script in ./.git/hooks/pre-commit
shellOut(to: "swift package update")

produces the following error:

xcrun --sdk macosx --show-sdk-path
xcrun --sdk macosx --show-sdk-platform-path
xcrun --find clang
sandbox-exec -p '(version 1)
(deny default)
(import "system.sb")
(allow file-read*)
(allow process*)
(allow sysctl*)
(allow file-write*
    (regex #"^/private/var/tmp/org\.llvm\.clang.*")
    (regex #"^/var/folders/h8/2l1nf2ss5qqfblyby0tnl7sm0000gn/T/org\.llvm\.clang.*")
    (regex #"^/private/var/folders/h8/2l1nf2ss5qqfblyby0tnl7sm0000gn/T/org\.llvm\.clang.*")
    (regex #"^/private/var/folders/h8/2l1nf2ss5qqfblyby0tnl7sm0000gn/C/org\.llvm\.clang.*")
)
' /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swiftc --driver-mode=swift -L /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/pm/4 -lPackageDescription -suppress-warnings -swift-version 4 -I /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/pm/4 -target x86_64-apple-macosx10.10 -sdk /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk ~redacted~/b4/cache/SwiftPreCommitHooks/Package.swift -fileno 5
sandbox-exec -p '(version 1)
(deny default)
(import "system.sb")
(allow file-read*)
(allow process*)
(allow sysctl*)
(allow file-write*
    (regex #"^/private/var/tmp/org\.llvm\.clang.*")
    (regex #"^/var/folders/h8/2l1nf2ss5qqfblyby0tnl7sm0000gn/T/org\.llvm\.clang.*")
    (regex #"^/private/var/folders/h8/2l1nf2ss5qqfblyby0tnl7sm0000gn/T/org\.llvm\.clang.*")
    (regex #"^/private/var/folders/h8/2l1nf2ss5qqfblyby0tnl7sm0000gn/C/org\.llvm\.clang.*")
)
' /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swiftc --driver-mode=swift -L /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/pm/4 -lPackageDescription -suppress-warnings -swift-version 4 -I /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/pm/4 -target x86_64-apple-macosx10.10 -sdk /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk /var/folders/h8/2l1nf2ss5qqfblyby0tnl7sm0000gn/T/TemporaryFile.TJUETQ.swift -fileno 6
git -C /Users/cavellebenjamin/Development/b4/cache/SwiftPreCommitHooks/.build/repositories/PathKit.git--1865447967743163058 rev-parse --is-bare-repositorygit -C ~redacted~/b4/cache/SwiftPreCommitHooks/.build/repositories/SwiftCLI.git-1996021284977509885 rev-parse --is-bare-repositorygit -C ~redacted~/b4/cache/SwiftPreCommitHooks/.build/repositories/ShellOut.git--4208804319273938067 rev-parse --is-bare-repository

git -C /Users/cavellebenjamin/Development/b4/cache/SwiftPreCommitHooks/.build/repositories/ShellOut.git--4208804319273938067 remote update -p
git -C /Users/cavellebenjamin/Development/b4/cache/SwiftPreCommitHooks/.build/repositories/SwiftCLI.git-1996021284977509885 remote update -p
Updating https://github.com/JohnSundell/ShellOut.git
Updating https://github.com/jakeheis/SwiftCLI.git
Updating https://github.com/kylef/PathKit.git
git -C ~redacted~/b4/cache/SwiftPreCommitHooks/.build/repositories/PathKit.git--1865447967743163058 remote update -p
error: terminated(128): git -C ~redacted~/b4/cache/SwiftPreCommitHooks/.build/repositories/PathKit.git--1865447967743163058 remote update -p output:

Appears to correct itself if I add Environment variables to the script that changes the PATH variable.

let updated = try bash( 
    command: .swiftPackageUpdate(),
    at: "\(FileManager.default.homeDirectoryForCurrentUser.path)/Development/b4/cache/SwiftPreCommitHooks",
    env: ["PATH":"/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin"]
"PATH":"/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin"]
  )

thoughts on a pull request? or am I overthinking the situation and should use shellOut as is with additional arguments?