Running old toolchain (3.1.1) from command-line on OS X 10.13.4

Is it possible to run an old toolchain from the command-line? This is my attempt at running 3.1.1 from OS X 10.13.4 (Xcode 9.3)

~/work$ which swift
/Library/Developer/Toolchains/swift-3.1.1-RELEASE.xctoolchain/usr/bin/swift
~/work$ mkdir foo
~/work$ cd foo
~/work/foo$ swift package init
Creating library package: foo
Creating Package.swift
Creating .gitignore
Creating Sources/
Creating Sources/foo.swift
Creating Tests/
Creating Tests/LinuxMain.swift
Creating Tests/fooTests/
Creating Tests/fooTests/fooTests.swift
~/work/foo$ swift build
Compile Swift Module 'foo' (1 sources)
~/work/foo$ swift test
Compile Swift Module 'fooTests' (1 sources)
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/Library/Frameworks/XCTest.framework/Headers/XCTest.apinotes:31:21: error: unknown key 'SwiftObjCMembers'
  SwiftObjCMembers: true
                    ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk/System/Library/Frameworks/Foundation.framework/Headers/Foundation.apinotes:8:28: error: unknown key 'SwiftImportAsNonGeneric'
  SwiftImportAsNonGeneric: true
                           ^
<module-includes>:1:9: note: in file included from <module-includes>:1:
#import "Headers/XCTest.h"
        ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/Library/Frameworks/XCTest.framework/Headers/XCTest.h:32:9: note: in file included from /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/Library/Frameworks/XCTest.framework/Headers/XCTest.h:32:
#import <XCTest/XCTestDefines.h>
        ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/Library/Frameworks/XCTest.framework/Headers/XCTestDefines.h:32:9: error: could not build module 'Foundation'
#import <Foundation/Foundation.h>
        ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk/System/Library/Frameworks/AppKit.framework/Headers/AppKit.apinotes:820:15: error: unknown key 'EnumKind'
    EnumKind: none
              ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/Library/Frameworks/XCTest.framework/Headers/XCUIElement.h:12:9: note: while building module 'AppKit' imported from /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/Library/Frameworks/XCTest.framework/Headers/XCUIElement.h:12:
#import <AppKit/AppKit.h>
        ^
<module-includes>:1:9: note: in file included from <module-includes>:1:
#import "Headers/AppKit.h"
        ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk/System/Library/Frameworks/AppKit.framework/Headers/AppKit.h:10:9: error: could not build module 'Foundation'
#import <Foundation/Foundation.h>
        ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/Library/Frameworks/XCTest.framework/Headers/XCUIElement.h:12:9: note: while building module 'AppKit' imported from /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/Library/Frameworks/XCTest.framework/Headers/XCUIElement.h:12:
#import <AppKit/AppKit.h>
        ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk/System/Library/Frameworks/AppKit.framework/Headers/NSColor.h:50:9: note: while building module 'CoreImage' imported from /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk/System/Library/Frameworks/AppKit.framework/Headers/NSColor.h:50:
#import <CoreImage/CIColor.h>
        ^
<module-includes>:1:9: note: in file included from <module-includes>:1:
#import "Headers/CoreImage.h"
        ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk/System/Library/Frameworks/CoreImage.framework/Headers/CoreImage.h:9:9: error: could not build module 'Foundation'
#import <Foundation/Foundation.h>
        ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/Library/Frameworks/XCTest.framework/Headers/XCUIElement.h:12:9: note: while building module 'AppKit' imported from /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/Library/Frameworks/XCTest.framework/Headers/XCUIElement.h:12:
#import <AppKit/AppKit.h>
        ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk/System/Library/Frameworks/AppKit.framework/Headers/NSColor.h:50:9: note: while building module 'CoreImage' imported from /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk/System/Library/Frameworks/AppKit.framework/Headers/NSColor.h:50:
#import <CoreImage/CIColor.h>
        ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk/System/Library/Frameworks/CoreImage.framework/Headers/CIImage.h:10:9: note: while building module 'CoreVideo' imported from /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk/System/Library/Frameworks/CoreImage.framework/Headers/CIImage.h:10:
#import <CoreVideo/CoreVideo.h>
        ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk/System/Library/Frameworks/CoreVideo.framework/Headers/CVPixelBufferIOSurface.h:30:10: note: while building module 'IOSurface' imported from /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk/System/Library/Frameworks/CoreVideo.framework/Headers/CVPixelBufferIOSurface.h:30:
#include <IOSurface/IOSurface.h>
         ^
<module-includes>:4:9: note: in file included from <module-includes>:4:
#import "Headers/IOSurfaceObjC.h"
        ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk/System/Library/Frameworks/IOSurface.framework/Headers/IOSurfaceObjC.h:15:9: error: could not build module 'Foundation'
#import <Foundation/Foundation.h>
        ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/Library/Frameworks/XCTest.framework/Headers/XCUIElement.h:12:9: note: while building module 'AppKit' imported from /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/Library/Frameworks/XCTest.framework/Headers/XCUIElement.h:12:
#import <AppKit/AppKit.h>
        ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk/System/Library/Frameworks/AppKit.framework/Headers/NSColor.h:50:9: note: while building module 'CoreImage' imported from /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk/System/Library/Frameworks/AppKit.framework/Headers/NSColor.h:50:
#import <CoreImage/CIColor.h>
        ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk/System/Library/Frameworks/CoreImage.framework/Headers/CIImage.h:10:9: note: while building module 'CoreVideo' imported from /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk/System/Library/Frameworks/CoreImage.framework/Headers/CIImage.h:10:
#import <CoreVideo/CoreVideo.h>
        ^
<module-includes>:1:9: note: in file included from <module-includes>:1:
#import "Headers/CoreVideo.h"
        ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk/System/Library/Frameworks/CoreVideo.framework/Headers/CoreVideo.h:29:10: note: in file included from /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk/System/Library/Frameworks/CoreVideo.framework/Headers/CoreVideo.h:29:
#include <CoreVideo/CVPixelBuffer.h>
         ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk/System/Library/Frameworks/CoreVideo.framework/Headers/CVPixelBuffer.h:461:10: note: in file included from /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk/System/Library/Frameworks/CoreVideo.framework/Headers/CVPixelBuffer.h:461:
#include <CoreVideo/CVPixelBufferIOSurface.h>
         ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk/System/Library/Frameworks/CoreVideo.framework/Headers/CVPixelBufferIOSurface.h:30:10: error: could not build module 'IOSurface'
#include <IOSurface/IOSurface.h>
         ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/Library/Frameworks/XCTest.framework/Headers/XCUIElement.h:12:9: note: while building module 'AppKit' imported from /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/Library/Frameworks/XCTest.framework/Headers/XCUIElement.h:12:
#import <AppKit/AppKit.h>
        ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk/System/Library/Frameworks/AppKit.framework/Headers/NSColor.h:50:9: note: while building module 'CoreImage' imported from /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk/System/Library/Frameworks/AppKit.framework/Headers/NSColor.h:50:
#import <CoreImage/CIColor.h>
        ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk/System/Library/Frameworks/CoreImage.framework/Headers/CIRenderDestination.h:18:9: note: while building module 'Metal' imported from /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk/System/Library/Frameworks/CoreImage.framework/Headers/CIRenderDestination.h:18:
#import <Metal/MTLPixelFormat.h>
        ^
<module-includes>:1:9: note: in file included from <module-includes>:1:
#import "Headers/Metal.h"
        ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk/System/Library/Frameworks/Metal.framework/Headers/Metal.h:9:9: note: in file included from /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk/System/Library/Frameworks/Metal.framework/Headers/Metal.h:9:
#import <Metal/MTLTypes.h>
        ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk/System/Library/Frameworks/Metal.framework/Headers/MTLTypes.h:8:9: error: could not build module 'Foundation'
#import <Foundation/Foundation.h>
        ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/Library/Frameworks/XCTest.framework/Headers/XCUIElement.h:12:9: note: while building module 'AppKit' imported from /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/Library/Frameworks/XCTest.framework/Headers/XCUIElement.h:12:
#import <AppKit/AppKit.h>
        ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk/System/Library/Frameworks/AppKit.framework/Headers/NSOpenGLLayer.h:10:9: note: while building module 'QuartzCore' imported from /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk/System/Library/Frameworks/AppKit.framework/Headers/NSOpenGLLayer.h:10:
#import <QuartzCore/CAOpenGLLayer.h>
        ^
<module-includes>:1:9: note: in file included from <module-includes>:1:
#import "Headers/QuartzCore.h"
        ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk/System/Library/Frameworks/QuartzCore.framework/Headers/QuartzCore.h:9:10: note: in file included from /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk/System/Library/Frameworks/QuartzCore.framework/Headers/QuartzCore.h:9:
#include <QuartzCore/CoreImage.h>
         ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk/System/Library/Frameworks/QuartzCore.framework/Headers/CoreImage.h:1:10: error: could not build module 'CoreImage'
#include <CoreImage/CoreImage.h>
         ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/Library/Frameworks/XCTest.framework/Headers/XCUIElement.h:12:9: note: while building module 'AppKit' imported from /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/Library/Frameworks/XCTest.framework/Headers/XCUIElement.h:12:
#import <AppKit/AppKit.h>
        ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk/System/Library/Frameworks/AppKit.framework/Headers/NSPredicateEditorRowTemplate.h:13:9: note: while building module 'CoreData' imported from /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk/System/Library/Frameworks/AppKit.framework/Headers/NSPredicateEditorRowTemplate.h:13:
#import <CoreData/NSAttributeDescription.h>
        ^
<module-includes>:1:9: note: in file included from <module-includes>:1:
#import "Headers/CoreData.h"
        ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk/System/Library/Frameworks/CoreData.framework/Headers/CoreData.h:8:9: error: could not build module 'Foundation'
#import <Foundation/Foundation.h>
        ^
<unknown>:0: error: could not build Objective-C module 'XCTest'
<unknown>:0: error: build had 1 command failures
error: exit(1): /Library/Developer/Toolchains/swift-3.1.1-RELEASE.xctoolchain/usr/bin/swift-build-tool -f /Users/tabor.kelly/work/foo/.build/debug.yaml test
~/work/foo$

swiftenv is supposed to allow you to do this relatively simply, though I haven’t had much use of it myself yet.

If you use an old toolchain, you’ll have to have an old macOS SDK around too (or avoid using any features that depend on the SDK, including Foundation and Dispatch).