{"id":49853,"date":"2025-10-30T19:21:35","date_gmt":"2025-10-30T23:21:35","guid":{"rendered":"https:\/\/mjtsai.com\/blog\/?p=49853"},"modified":"2025-12-01T10:04:16","modified_gmt":"2025-12-01T15:04:16","slug":"swift-6-2-subprocess","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2025\/10\/30\/swift-6-2-subprocess\/","title":{"rendered":"Swift 6.2: Subprocess"},"content":{"rendered":"<p><a href=\"https:\/\/www.swift.org\/blog\/swift-6.2-released\/\">Holly Borla<\/a>:<\/p>\n<blockquote cite=\"https:\/\/www.swift.org\/blog\/swift-6.2-released\/\"><p>Swift 6.2 introduces a new <code>Subprocess<\/code> package that offers a streamlined, concurrency&#x2011;friendly API for launching and managing external processes.<\/p>\n<\/blockquote>\n\n<p><a href=\"https:\/\/github.com\/swiftlang\/swift-foundation\/blob\/main\/Proposals\/0007-swift-subprocess.md\">SF-0007<\/a>:<\/p>\n<blockquote cite=\"https:\/\/github.com\/swiftlang\/swift-foundation\/blob\/main\/Proposals\/0007-swift-subprocess.md\"><p>The existing Foundation API for spawning a process, <code>NSTask<\/code>, originated in Objective-C. It was subsequently renamed to <code>Process<\/code> in Swift. As the language has continued to evolve, <code>Process<\/code> has not kept up. It lacks support for <code>async\/await<\/code>, makes extensive use of completion handlers, and uses Objective-C exceptions to indicate developer error. This proposal introduces a new package called <code>Subprocess<\/code>, which addresses the ergonomic shortcomings of <code>Process<\/code> and enhances the experience of using Swift for scripting and other areas such as server-side development.<\/p><\/blockquote>\n\n<p>It&rsquo;s currently available as <a href=\"https:\/\/github.com\/swiftlang\/swift-subprocess\">a package<\/a>, rather than being built into the OS, so the API may not be fully stable, but you can use it on older OS versions.<\/p>\n\n<p>One of the issues with <code>NSTask<\/code>\/<code>Process<\/code> is that the simple API works for small amounts of input\/output but (unbeknownst to many, even some <a href=\"https:\/\/mastodon.social\/@heckj\/114257538235870239\">experts<\/a>) hangs once you exceed the OS&rsquo;s buffer size. There are ways around this, but they are awkward (though you can hide them in a  helper class). As <a href=\"https:\/\/christiantietze.de\/posts\/2025\/03\/process-pipe-64kib-limit\/\">Christian Tietze<\/a> (<a href=\"https:\/\/mastodon.social\/@ctietze\/114259714400478229\">Mastodon<\/a>) writes:<\/p>\n<blockquote cite=\"https:\/\/christiantietze.de\/posts\/2025\/03\/process-pipe-64kib-limit\/\"><p><a href=\"https:\/\/mastodon.social\/deck\/@helge\/114228790121854120\">Helge He&szlig; pointed out<\/a> that naive usage of <code>Pipe<\/code> in child <code>Process<\/code>es can break your program if you pipe too much data.<\/p><p>[&#8230;]<\/p><p>If the data is larger than the pipe buffer, you need to drain the corresponding <code>FileHandle<\/code> with repeated read calls. (Or provide data larger than 64KiB with repeated write calls, respectively.)<\/p><p>If you try to send\/receive the whole buffer in one go, from a user&rsquo;s perspective, your program will freeze, and the read call never return. As a CLI app, it&rsquo;ll never terminate.<\/p><\/blockquote>\n\n<p><code>Subprocess<\/code> seems to be designed to transparently handle this. You can specify the output size limit (and the encoding, if you want a string output), and it will collect the pieces of output as they arrive.<\/p>\n\n<p>I&rsquo;ve been using Swift to write scripts, and scripts often need to run shell commands. Unfortunately, <code>Subprocess<\/code> is currently cumbersome to use for scripting, as <a href=\"https:\/\/blog.jacobstechtavern.com\/p\/swift-subprocess\">Jacob Bartlett<\/a> writes:<\/p>\n<blockquote cite=\"https:\/\/blog.jacobstechtavern.com\/p\/swift-subprocess\"><p>These simple scripts have ludicrous amounts of overhead for what are trivial one-liner operations in the bash shell. Bear with me, because later we&rsquo;ll look at where Swift subprocess can actually work well, in a more complex workflow.<\/p><p>The additional overhead of requiring a full SwiftPM project, compared to a Bash script, makes it incredibly cumbersome for simple workloads. Also, it&rsquo;s still not an actual script, so it&rsquo;ll always require a compilation (and potentially dependency resolution) overhead whenever your code changes.<\/p><p>On the other hand, the syntax, type safety, and composability of Swift code work pretty nicely when you have <strong>complex automation<\/strong> <strong>workflows<\/strong> to orchestrate, build, and run on demand.<\/p><\/blockquote>\n\n<p>Previously:<\/p>\n<ul>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2025\/10\/29\/swift-6-2\/\">Swift 6.2<\/a><\/li>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2011\/01\/24\/bmscript\/\">BMScript<\/a><\/li>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2003\/05\/18\/mac_os_x_advanced_develop\/\">Mac OS X Advanced Development Techniques<\/a><\/li>\n<\/ul>\n\n<p id=\"swift-6-2-subprocess-update-2025-12-01\">Update (<a href=\"#swift-6-2-subprocess-update-2025-12-01\">2025-12-01<\/a>): <a href=\"https:\/\/troz.net\/post\/2025\/process-subprocess\/\">Sarah Reichelt<\/a>:<\/p>\n<blockquote cite=\"https:\/\/troz.net\/post\/2025\/process-subprocess\/\">\n<p>There are immediate benefits to using Subprocess. I didn't have to specify the full path to the <code>whoami<\/code> command, and I didn't have to set up a pipe and a file handle to see the result.<\/p>\n<p>[&#8230;]<\/p>\n<p>That's when I found out that <code>arguments<\/code> must be an array of <code>Arguments<\/code>, not <code>Strings<\/code>. <\/p>\n<\/blockquote>\n<p>It&rsquo;s not an <code>Array<\/code> of <code>Arguments<\/code>; <a href=\"https:\/\/github.com\/swiftlang\/swift-subprocess\/blob\/ab2072d457d1464b17166cc02125fc9fc0791e1a\/Sources\/Subprocess\/Configuration.swift#L325\">Arguments<\/a> itself is an array-like type. The comment says it&rsquo;s &ldquo;a collection,&rdquo; but it does not actually seem to be a <code>Collection<\/code>. Anyway, this is annoying because I&rsquo;m often constructing arguments lists in code rather than using literals. I wonder if there wasn&rsquo;t a better way to provide the <code>executablePathOverride<\/code> functionality than by making all the users who don&rsquo;t need that use a whole new type.<\/p>\n<blockquote cite=\"https:\/\/troz.net\/post\/2025\/process-subprocess\/\">\n<p>This worked, but I was still getting the complete output at the end, and not seeing each line as it arrived. To help in my research, I selected <strong>Build Documentation<\/strong> from the <strong>Product<\/strong> menu which gave me the docs for <code>Subprocess<\/code> in Xcode's Developer Documentation. There are a lot of custom types, but only one function - <code>run<\/code> - which has 14 different versions. Seven of these variants expect an <code>Executable<\/code>, which is what I've been using when I specify a <code>.name<\/code>. The others expect a <code>Configuration<\/code> which is a way of combining an executable and its arguments plus other configuration details, into a single object.<\/p>\n<p>I also discovered that the versions that streamed data had a <code>preferredBufferSize<\/code> setting.<\/p>\n<p>[&#8230;]<\/p>\n<p>I set the <code>preferredBufferSize<\/code> to 32 and watched the lines come in one by one. It would be great if there was an option to buffer until a line feed, but this is workable.<\/p>\n<\/blockquote>","protected":false},"excerpt":{"rendered":"<p>Holly Borla: Swift 6.2 introduces a new Subprocess package that offers a streamlined, concurrency&#x2011;friendly API for launching and managing external processes. SF-0007: The existing Foundation API for spawning a process, NSTask, originated in Objective-C. It was subsequently renamed to Process in Swift. As the language has continued to evolve, Process has not kept up. It [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"apple_news_api_created_at":"2025-10-30T23:21:38Z","apple_news_api_id":"15bc1fcd-984f-4820-88eb-50f260e2cc64","apple_news_api_modified_at":"2025-12-01T15:04:21Z","apple_news_api_revision":"AAAAAAAAAAAAAAAAAAAAAA==","apple_news_api_share_url":"https:\/\/apple.news\/AFbwfzZhPSCCI61DyYOLMZA","apple_news_coverimage":0,"apple_news_coverimage_caption":"","apple_news_is_hidden":false,"apple_news_is_paid":false,"apple_news_is_preview":false,"apple_news_is_sponsored":false,"apple_news_maturity_rating":"","apple_news_metadata":"\"\"","apple_news_pullquote":"","apple_news_pullquote_position":"","apple_news_slug":"","apple_news_sections":"\"\"","apple_news_suppress_video_url":false,"apple_news_use_image_component":false,"footnotes":""},"categories":[4],"tags":[69,31,2741,30,2742,74,71,2200,2854,901],"class_list":["post-49853","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-cocoa","tag-ios","tag-ios-26","tag-mac","tag-macos-tahoe-26","tag-opensource","tag-programming","tag-swift-concurrency","tag-swift-foundation","tag-swift-programming-language"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/49853","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/comments?post=49853"}],"version-history":[{"count":2,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/49853\/revisions"}],"predecessor-version":[{"id":50260,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/49853\/revisions\/50260"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=49853"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=49853"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=49853"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}