Skip to content

Commit

Permalink
Add SWIFTLINT_DISABLE_SOURCEKIT environment variable (realm#2473)
Browse files Browse the repository at this point in the history
This can be used for avoid "Test::Unit::AssertionFailedError" error in `libxpc.dylib` on calling `sourcekitd_send_request_sync` in sandbox environment.
  • Loading branch information
norio-nomura authored and jpsim committed Nov 22, 2018
1 parent db2fbad commit ea171fb
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Source/SwiftLintFramework/Extensions/File+Cache.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import SourceKittenFramework

private var responseCache = Cache({ file -> [String: SourceKitRepresentable]? in
do {
return try Request.editorOpen(file: file).send()
return try Request.editorOpen(file: file).sendIfNotDisabled()
} catch let error as Request.Error {
queuedPrintError(error.description)
return nil
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import Foundation
import SourceKittenFramework

extension Request {
static let disableSourceKit = ProcessInfo.processInfo.environment["SWIFTLINT_DISABLE_SOURCEKIT"] != nil

func sendIfNotDisabled() throws -> [String: SourceKitRepresentable] {
guard !Request.disableSourceKit else {
throw Request.Error.connectionInterrupted("SourceKit is disabled by `SWIFTLINT_DISABLE_SOURCEKIT`.")
}
return try send()
}
}
3 changes: 2 additions & 1 deletion Source/SwiftLintFramework/Models/SwiftVersion.swift
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ public extension SwiftVersion {
func isString(token: SyntaxToken) -> Bool {
return token.type == SyntaxKind.string.rawValue
}
if let decl = file.structure.kinds().first(where: { $0.kind == SwiftDeclarationKind.varGlobal.rawValue }),
if !Request.disableSourceKit,
let decl = file.structure.kinds().first(where: { $0.kind == SwiftDeclarationKind.varGlobal.rawValue }),
let token = file.syntaxMap.tokens(inByteRange: decl.byteRange).first(where: isString ) {
return .init(rawValue: file.contents.substring(from: token.offset + 1, length: token.length - 2))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ private extension File {
}
let cursorInfoRequest = Request.cursorInfo(file: path!, offset: Int64(token.offset),
arguments: compilerArguments)
guard let cursorInfo = try? cursorInfoRequest.send() else {
guard let cursorInfo = try? cursorInfoRequest.sendIfNotDisabled() else {
queuedPrintError("Could not get cursor info")
continue
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,14 @@ public struct UnusedPrivateDeclarationRule: AutomaticTestableRule, Configuration

private extension File {
func allCursorInfo(compilerArguments: [String]) -> [[String: SourceKitRepresentable]] {
guard let path = path, let editorOpen = try? Request.editorOpen(file: self).send() else {
guard let path = path, let editorOpen = try? Request.editorOpen(file: self).sendIfNotDisabled() else {
return []
}

return syntaxMap.tokens.compactMap { token in
let offset = Int64(token.offset)
var cursorInfo = try? Request.cursorInfo(file: path, offset: offset,
arguments: compilerArguments).send()
arguments: compilerArguments).sendIfNotDisabled()
if let acl = File.aclAtOffset(offset, substructureElement: editorOpen) {
cursorInfo?["key.accessibility"] = acl
}
Expand Down
4 changes: 2 additions & 2 deletions Source/SwiftLintFramework/Rules/Style/ExplicitSelfRule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ private extension File {
return try byteOffsets.compactMap { offset in
if contents.bridge().substringWithByteRange(start: offset - 1, length: 1)! == "." { return nil }
var cursorInfo = try Request.cursorInfo(file: self.path!, offset: Int64(offset),
arguments: compilerArguments).send()
arguments: compilerArguments).sendIfNotDisabled()
cursorInfo["swiftlint.offset"] = Int64(offset)
return cursorInfo
}
Expand Down Expand Up @@ -196,7 +196,7 @@ private extension NSString {

private func binaryOffsets(file: File, compilerArguments: [String]) throws -> [Int] {
let absoluteFile = file.path!.bridge().absolutePathRepresentation()
let index = try Request.index(file: absoluteFile, arguments: compilerArguments).send()
let index = try Request.index(file: absoluteFile, arguments: compilerArguments).sendIfNotDisabled()
let binaryOffsets = file.contents.bridge().recursiveByteOffsets(index)
return binaryOffsets.sorted()
}
4 changes: 4 additions & 0 deletions SwiftLint.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@
67EB4DFA1E4CC111004E9ACD /* CyclomaticComplexityConfiguration.swift in Sources */ = {isa = PBXBuildFile; fileRef = 67EB4DF81E4CC101004E9ACD /* CyclomaticComplexityConfiguration.swift */; };
67EB4DFC1E4CD7F5004E9ACD /* CyclomaticComplexityRuleTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 67EB4DFB1E4CD7F5004E9ACD /* CyclomaticComplexityRuleTests.swift */; };
69F88BF71BDA38A6005E7CAE /* OpeningBraceRule.swift in Sources */ = {isa = PBXBuildFile; fileRef = 692B1EB11BD7E00F00EAABFF /* OpeningBraceRule.swift */; };
6C1D763221A4E69600DEF783 /* Request+DisableSourceKit.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6C1D763121A4E69600DEF783 /* Request+DisableSourceKit.swift */; };
6C7045441C6ADA450003F15A /* SourceKitCrashTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6C7045431C6ADA450003F15A /* SourceKitCrashTests.swift */; };
6CB514E91C760C6900FA02C4 /* Structure+SwiftLint.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6CB514E81C760C6900FA02C4 /* Structure+SwiftLint.swift */; };
6CB8A80C1D11A7E10052816E /* Commandant.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = E8BA7E101B07A3EC003E02D0 /* Commandant.framework */; };
Expand Down Expand Up @@ -537,6 +538,7 @@
692B1EB11BD7E00F00EAABFF /* OpeningBraceRule.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = OpeningBraceRule.swift; sourceTree = "<group>"; };
692B60AB1BD8F2E700C7AA22 /* StatementPositionRule.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = StatementPositionRule.swift; sourceTree = "<group>"; };
695BE9CE1BDFD92B0071E985 /* CommaRule.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CommaRule.swift; sourceTree = "<group>"; };
6C1D763121A4E69600DEF783 /* Request+DisableSourceKit.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Request+DisableSourceKit.swift"; sourceTree = "<group>"; };
6C27B5FC2079D33F00353E17 /* Mac-XCTest.xcconfig */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xcconfig; path = "Mac-XCTest.xcconfig"; sourceTree = "<group>"; };
6C7045431C6ADA450003F15A /* SourceKitCrashTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SourceKitCrashTests.swift; sourceTree = "<group>"; };
6CB514E81C760C6900FA02C4 /* Structure+SwiftLint.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "Structure+SwiftLint.swift"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1475,6 +1477,7 @@
3BA79C9A1C4767910057E705 /* NSRange+SwiftLint.swift */,
3BB47D841C51D80000AE6A10 /* NSRegularExpression+SwiftLint.swift */,
E81619521BFC162C00946723 /* QueuedPrint.swift */,
6C1D763121A4E69600DEF783 /* Request+DisableSourceKit.swift */,
E88DEA721B0984C400A66CB0 /* String+SwiftLint.swift */,
B39353F28BCCA39247B316BD /* String+XML.swift */,
6CB514E81C760C6900FA02C4 /* Structure+SwiftLint.swift */,
Expand Down Expand Up @@ -1874,6 +1877,7 @@
62DADC481FFF0423002B6319 /* PrefixedTopLevelConstantRule.swift in Sources */,
D4130D991E16CC1300242361 /* TypeNameRuleExamples.swift in Sources */,
24E17F721B14BB3F008195BE /* File+Cache.swift in Sources */,
6C1D763221A4E69600DEF783 /* Request+DisableSourceKit.swift in Sources */,
47ACC8981E7DC74E0088EEB2 /* ImplicitlyUnwrappedOptionalConfiguration.swift in Sources */,
787CDE39208E7D41005F3D2F /* SwitchCaseAlignmentConfiguration.swift in Sources */,
009E09281DFEE4C200B588A7 /* ProhibitedSuperRule.swift in Sources */,
Expand Down

0 comments on commit ea171fb

Please sign in to comment.