Skip to content

Commit

Permalink
[#3485] Add missing rootDir adjustment for included / excluded paths
Browse files Browse the repository at this point in the history
  • Loading branch information
fredpi authored and jpsim committed Feb 23, 2021
1 parent c4200f4 commit 3b81536
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,15 @@ internal extension Configuration {
dict: firstConfigurationData.configurationDict,
enableAllRules: enableAllRules
)
firstConfiguration.fileGraph = FileGraph(rootDirectory: firstConfigurationData.rootDirectory)

// Set the config's rootDirectory to rootDirectory (+ adjust included / excluded paths that relate to it).
// firstConfigurationData.rootDirectory may be different from rootDirectory,
// e. g. when ../file.yml is passed as the first config
firstConfiguration.fileGraph = FileGraph(rootDirectory: rootDirectory)
firstConfiguration.makeIncludedAndExcludedPaths(
relativeTo: rootDirectory,
previousBasePath: firstConfigurationData.rootDirectory
)

// Build succeeding configurations
return try configurationData.reduce(firstConfiguration) {
Expand Down
25 changes: 19 additions & 6 deletions Source/SwiftLintFramework/Models/Configuration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ public struct Configuration {

// MARK: Public Instance
/// The paths that should be included when linting
public let includedPaths: [String]
public private(set) var includedPaths: [String]

/// The paths that should be excluded when linting
public let excludedPaths: [String]
public private(set) var excludedPaths: [String]

/// The style to use when indenting Swift source code.
public let indentation: IndentationStyle
Expand Down Expand Up @@ -46,7 +46,8 @@ public struct Configuration {

/// The root directory is the directory that included & excluded paths relate to.
/// By default, the root directory is the current working directory,
/// but in some merging algorithms it is used differently.
/// but during some merging algorithms it may be used differently.
/// The rootDirectory also serves as the stopping point when searching for nested configs along the file hierarchy.
public var rootDirectory: String { fileGraph.rootDirectory }

/// The rules mode used for this configuration.
Expand Down Expand Up @@ -190,11 +191,11 @@ public struct Configuration {
let configurationFiles = configurationFiles.isEmpty ? [Configuration.defaultFileName] : configurationFiles
defer { basedOnCustomConfigurationFiles = hasCustomConfigurationFiles }

let rootDirectory = FileManager.default.currentDirectoryPath.bridge().absolutePathStandardized()
let currentWorkingDirectory = FileManager.default.currentDirectoryPath.bridge().absolutePathStandardized()
let rulesMode: RulesMode = enableAllRules ? .allEnabled : .default(disabled: [], optIn: [])

// Try obtaining cached config
let cacheIdentifier = "\(rootDirectory) - \(configurationFiles)"
let cacheIdentifier = "\(currentWorkingDirectory) - \(configurationFiles)"
if let cachedConfig = Configuration.getCached(forIdentifier: cacheIdentifier) {
self.init(copying: cachedConfig)
return
Expand All @@ -204,7 +205,7 @@ public struct Configuration {
do {
var fileGraph = FileGraph(
commandLineChildConfigs: configurationFiles,
rootDirectory: rootDirectory,
rootDirectory: currentWorkingDirectory,
ignoreParentAndChildConfigs: ignoreParentAndChildConfigs
)
let resultingConfiguration = try fileGraph.resultingConfiguration(enableAllRules: enableAllRules)
Expand Down Expand Up @@ -237,8 +238,20 @@ public struct Configuration {
}
}
}

// MARK: - Methods: Internal
mutating func makeIncludedAndExcludedPaths(relativeTo newBasePath: String, previousBasePath: String) {
includedPaths = includedPaths.map {
$0.bridge().absolutePathRepresentation(rootDirectory: previousBasePath).path(relativeTo: newBasePath)
}

excludedPaths = excludedPaths.map {
$0.bridge().absolutePathRepresentation(rootDirectory: previousBasePath).path(relativeTo: newBasePath)
}
}
}

// MARK: - FileGraphInitializationResult
private enum FileGraphInitializationResult {
case initialImplicitFileNotFound
case error(message: String)
Expand Down

0 comments on commit 3b81536

Please sign in to comment.