Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Find configuration based on CWD when formatting from stdin #685

Merged
merged 1 commit into from
Feb 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Sources/swift-format/Frontend/ConfigurationLoader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ struct ConfigurationLoader {
/// The cache of previously loaded configurations.
private var cache = [String: Configuration]()

/// Returns the configuration found by searching in the directory (and ancestor directories)
/// containing the given `.swift` source file.
/// Returns the configuration found by walking up the file tree from `url`.
/// This function works for both files and directories.
///
/// If no configuration file was found during the search, this method returns nil.
///
/// - Throws: If a configuration file was found but an error occurred loading it.
mutating func configuration(forSwiftFileAt url: URL) throws -> Configuration? {
mutating func configuration(forPath url: URL) throws -> Configuration? {
guard let configurationFileURL = Configuration.url(forConfigurationFileApplyingTo: url)
else {
return nil
Expand Down
23 changes: 19 additions & 4 deletions Sources/swift-format/Frontend/Frontend.swift
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ class Frontend {
// then try to load the configuration by inferring it based on the source file path.
if let swiftFileURL = swiftFileURL {
do {
if let configuration = try configurationLoader.configuration(forSwiftFileAt: swiftFileURL) {
if let configuration = try configurationLoader.configuration(forPath: swiftFileURL) {
self.checkForUnrecognizedRules(in: configuration)
return configuration
}
Expand All @@ -223,11 +223,26 @@ class Frontend {
"Unable to read configuration for \(swiftFileURL.path): \(error.localizedDescription)")
return nil
}
} else {
// If reading from stdin and no explicit configuration file was given,
// walk up the file tree from the cwd to find a config.

let cwd = URL(fileURLWithPath: FileManager.default.currentDirectoryPath)
// Definitely a Swift file. Definitely not a directory. Shhhhhh.
do {
if let configuration = try configurationLoader.configuration(forPath: cwd) {
self.checkForUnrecognizedRules(in: configuration)
return configuration
}
} catch {
diagnosticsEngine.emitError(
"Unable to read configuration for \(cwd): \(error.localizedDescription)")
return nil
}
}

// If neither path was given (for example, formatting standard input with no assumed filename)
// or if there was no configuration found by inferring it from the source file path, return the
// default configuration.
// An explicit configuration has not been given, and one cannot be found.
// Return the default configuration.
return Configuration()
}

Expand Down