Skip to content

Commit ba98f44

Browse files
authored
Improve handling of working directory for Bazel (#838)
1 parent d54bd93 commit ba98f44

File tree

12 files changed

+29
-30
lines changed

12 files changed

+29
-30
lines changed

.mise/tasks/benchmark

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ cmd=""
88
if [ "$usage_bazel" = "true" ]; then
99
echo "INFO: Using Bazel"
1010
bazel build //:periphery
11-
cmd='bazel-bin/Sources/Frontend scan --config /var/tmp/periphery_bazel/periphery.yml --generic-project-config bazel-bin/external/+generated+periphery_generated/rule/project_config.json'
11+
cmd='bazel-bin/Sources/Frontend_opt scan --config /var/tmp/periphery_bazel/periphery.yml --generic-project-config bazel-bin/external/+generated+periphery_generated/rule/project_config.json'
1212
else
1313
mise r build --arch arm64
1414
cmd='./.build/release/periphery scan --quiet --skip-build'

BUILD.bazel

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,7 @@ package_group(
1010
packages = ["//..."],
1111
)
1212

13-
sh_binary(
13+
alias(
1414
name = "periphery",
15-
srcs = ["//bazel/internal:run.sh"],
16-
data = [
17-
"//Sources:Frontend_opt",
18-
],
15+
actual = "//Sources:Frontend_opt",
1916
)

MODULE.bazel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ bazel_dep(name = "apple_support", version = "1.17.1")
1313
# Swift dependencies
1414
bazel_dep(name = "aexml", version = "4.7.0")
1515
bazel_dep(name = "swift_argument_parser", version = "1.5.0")
16-
bazel_dep(name = "swift-filename-matcher", version = "0.1.2")
16+
bazel_dep(name = "swift-filename-matcher", version = "2.0.0")
1717
bazel_dep(name = "swift-indexstore", version = "0.3.0")
1818
bazel_dep(name = "swift-syntax", version = "600.0.1")
1919
bazel_dep(name = "swift-system", version = "1.4.0")

MODULE.bazel.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Package.resolved

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Package.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ var dependencies: [Package.Dependency] = [
88
.package(url: "https://github.com/apple/swift-argument-parser", from: "1.0.0"),
99
.package(url: "https://github.com/kateinoigakukun/swift-indexstore", from: "0.3.0"),
1010
.package(url: "https://github.com/apple/swift-syntax", from: "600.0.1"),
11-
.package(url: "https://github.com/ileitch/swift-filename-matcher", from: "0.0.0"),
11+
.package(url: "https://github.com/ileitch/swift-filename-matcher", from: "2.0.0"),
1212
]
1313

1414
#if os(macOS)

Sources/Extensions/FilenameMatcher+Extension.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,6 @@ public extension FilenameMatcher {
1919
let normalizedBase = traversedBase.hasSuffix("/") ? traversedBase : "\(traversedBase)/"
2020
let shouldPrependPwd = !["/", "*"].contains { relativePattern.hasPrefix($0) }
2121
let pattern = shouldPrependPwd ? "\(normalizedBase)\(traversedPattern)" : traversedPattern
22-
self.init(pattern: pattern, caseSensitive: caseSensitive)
22+
self.init(pattern: pattern, options: caseSensitive ? [.caseSensitive] : [])
2323
}
2424
}

Sources/Frontend/Commands/ScanCommand.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ struct ScanCommand: FrontendCommand {
1818
@Flag(help: "Enable guided setup")
1919
var setup: Bool = defaultConfiguration.guidedSetup
2020

21+
@Option(help: "Path to the root directory of your project")
22+
var projectRoot: FilePath = projectRootDefault
23+
2124
@Option(help: "Path to configuration file. By default Periphery will look for .periphery.yml in the current directory")
2225
var config: FilePath?
2326

@@ -138,6 +141,10 @@ struct ScanCommand: FrontendCommand {
138141
private static let defaultConfiguration = Configuration()
139142

140143
func run() throws {
144+
if !FileManager.default.changeCurrentDirectoryPath(projectRoot.string) {
145+
throw PeripheryError.changeCurrentDirectoryFailed(projectRoot)
146+
}
147+
141148
let configuration = Configuration()
142149

143150
if !setup {
@@ -245,6 +252,14 @@ struct ScanCommand: FrontendCommand {
245252
throw PeripheryError.foundIssues(count: filteredResults.count)
246253
}
247254
}
255+
256+
// MARK: - Private
257+
258+
private static var projectRootDefault: FilePath {
259+
let bazelWorkspace = ProcessInfo.processInfo.environment["BUILD_WORKSPACE_DIRECTORY"]
260+
let root = bazelWorkspace ?? FileManager.default.currentDirectoryPath
261+
return FilePath(root)
262+
}
248263
}
249264

250265
extension OutputFormat: ExpressibleByArgument {}

Sources/Shared/PeripheryError.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public enum PeripheryError: Error, LocalizedError, CustomStringConvertible {
1818
case swiftVersionUnsupportedError(version: String, minimumVersion: String)
1919
case jsonDeserializationError(error: Error, json: String)
2020
case indexStoreNotFound(derivedDataPath: String)
21+
case changeCurrentDirectoryFailed(FilePath)
2122

2223
public var errorDescription: String? {
2324
switch self {
@@ -55,6 +56,8 @@ public enum PeripheryError: Error, LocalizedError, CustomStringConvertible {
5556
return "JSON deserialization failed: \(describe(error))\nJSON:\n\(json)"
5657
case let .indexStoreNotFound(derivedDataPath):
5758
return "Failed to find index datastore at path: \(derivedDataPath)"
59+
case let .changeCurrentDirectoryFailed(path):
60+
return "Failed to change current directory to: \(path)"
5861
}
5962
}
6063

bazel/internal/BUILD.bazel

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +0,0 @@
1-
exports_files([
2-
"run.sh",
3-
])

0 commit comments

Comments
 (0)