From 2b307bec90ba6b8a3182f7489b99f9df2dcca259 Mon Sep 17 00:00:00 2001 From: Alex Hoppen Date: Thu, 10 Apr 2025 08:40:59 -0700 Subject: [PATCH 1/3] Use workflow in github-workflows to create auto merge PR Use workflow added by https://github.com/swiftlang/github-workflows/pull/112 to create auto merge PR that merges `main` into `release/6.2`. --- .github/workflows/automerge.yml | 47 +++------------------------------ 1 file changed, 4 insertions(+), 43 deletions(-) diff --git a/.github/workflows/automerge.yml b/.github/workflows/automerge.yml index 086a7c6e6..0ad537375 100644 --- a/.github/workflows/automerge.yml +++ b/.github/workflows/automerge.yml @@ -1,53 +1,14 @@ name: Create PR to merge main into release branch - # In the first period after branching the release branch, we typically want to include all changes from `main` also in the release branch. This workflow automatically creates a PR every Monday to merge main into the release branch. # Later in the release cycle we should stop this practice to avoid landing risky changes by disabling this workflow. To do so, disable the workflow as described in https://docs.github.com/en/actions/managing-workflow-runs-and-deployments/managing-workflow-runs/disabling-and-enabling-a-workflow - on: schedule: - - cron: '0 0 * * MON' + - cron: '0 9 * * MON' workflow_dispatch: - jobs: create_merge_pr: name: Create PR to merge main into release branch - runs-on: ubuntu-latest + uses: swiftlang/github-workflows/.github/workflows/create_automerge_pr.yml@main + with: + base_branch: release/6.2 if: (github.event_name == 'schedule' && github.repository == 'swiftlang/swift-format') || (github.event_name != 'schedule') # Ensure that we don't run this on a schedule in a fork - steps: - - name: Set up variables - id: variables - run: | - echo "release_branch=release/6.2" >> "$GITHUB_OUTPUT" - echo "pr_branch=automerge/merge-main-$(date +%Y-%m-%d)" >> "$GITHUB_OUTPUT" - - name: Checkout repository - uses: actions/checkout@v4 - with: - fetch-depth: 0 - - name: Create merge commit - id: create_merge_commit - run: | - # Without this, we can't perform git operations in GitHub actions. - git config --global --add safe.directory "$(realpath .)" - git config --local user.name 'swift-ci' - git config --local user.email 'swift-ci@users.noreply.github.com' - - git checkout ${{ steps.variables.outputs.release_branch }} - git merge main - - if [[ "$(git rev-parse HEAD)" = "$(git rev-parse main)" ]]; then - echo "has_merged_commits=true" >> "$GITHUB_OUTPUT" - else - echo "has_merged_commits=false" >> "$GITHUB_OUTPUT" - fi - - name: Push branch and create PR - id: push_branch - if: ${{ steps.create_merge_commit.outputs.has_merged_commits }} - env: - GH_TOKEN: ${{ github.token }} - run: | - git checkout -b "${{ steps.variables.outputs.pr_branch }}" - git push --set-upstream origin "${{ steps.variables.outputs.pr_branch }}" - - gh pr create -B "${{ steps.variables.outputs.release_branch }}" -H "${{ steps.variables.outputs.pr_branch }}" \ - --title 'Merge `main` into `${{ steps.variables.outputs.release_branch }}`' \ - --body 'This PR was automatically opened by a GitHub action. Review the changes included in this PR and determine if they should be included in the release branch. If yes, merge the PR. Otherwise revert changes that should not be included on this branch.' From fd884643ab9bcf06a2246dff15f8ed5362fb4e8b Mon Sep 17 00:00:00 2001 From: Alex Hoppen Date: Thu, 17 Apr 2025 09:56:01 -0700 Subject: [PATCH 2/3] Grant write permissions for content an pull requests to auto merge GitHub action --- .github/workflows/automerge.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.github/workflows/automerge.yml b/.github/workflows/automerge.yml index 0ad537375..7c65edeef 100644 --- a/.github/workflows/automerge.yml +++ b/.github/workflows/automerge.yml @@ -11,4 +11,7 @@ jobs: uses: swiftlang/github-workflows/.github/workflows/create_automerge_pr.yml@main with: base_branch: release/6.2 + permissions: + contents: write + pull-requests: write if: (github.event_name == 'schedule' && github.repository == 'swiftlang/swift-format') || (github.event_name != 'schedule') # Ensure that we don't run this on a schedule in a fork From f60f4eb479dc1be398777e64993b2d5a961a4f53 Mon Sep 17 00:00:00 2001 From: Alex Hoppen Date: Fri, 18 Apr 2025 13:30:51 -0700 Subject: [PATCH 3/3] Make linter emit warnings instead of errors by default When running `swift-format lint` in an Xcode run script phase and it exits with a non-zero exit code, the build will fail. This started happening since we treated all linter findings as errors in https://github.com/swiftlang/swift-format/pull/943. To fix this: - Diagnose all findings as warnings again and exit with code 0 even if there are findings - Resurrect `--strict` to treat all findings as errors and exit with 1 if there were any findings. rdar://148389716 --- Sources/swift-format/Frontend/Frontend.swift | 11 +++++++++-- Sources/swift-format/Subcommands/Lint.swift | 16 ++++++---------- .../Utilities/DiagnosticsEngine.swift | 12 ++++++++++-- 3 files changed, 25 insertions(+), 14 deletions(-) diff --git a/Sources/swift-format/Frontend/Frontend.swift b/Sources/swift-format/Frontend/Frontend.swift index e1007414b..aaa24ad01 100644 --- a/Sources/swift-format/Frontend/Frontend.swift +++ b/Sources/swift-format/Frontend/Frontend.swift @@ -200,14 +200,21 @@ class Frontend { /// Creates a new frontend with the given options. /// /// - Parameter lintFormatOptions: Options that apply during formatting or linting. - init(configurationOptions: ConfigurationOptions, lintFormatOptions: LintFormatOptions) { + init( + configurationOptions: ConfigurationOptions, + lintFormatOptions: LintFormatOptions, + treatWarningsAsErrors: Bool = false + ) { self.configurationOptions = configurationOptions self.lintFormatOptions = lintFormatOptions self.diagnosticPrinter = StderrDiagnosticPrinter( colorMode: lintFormatOptions.colorDiagnostics.map { $0 ? .on : .off } ?? .auto ) - self.diagnosticsEngine = DiagnosticsEngine(diagnosticsHandlers: [diagnosticPrinter.printDiagnostic]) + self.diagnosticsEngine = DiagnosticsEngine( + diagnosticsHandlers: [diagnosticPrinter.printDiagnostic], + treatWarningsAsErrors: treatWarningsAsErrors + ) self.configurationProvider = ConfigurationProvider(diagnosticsEngine: self.diagnosticsEngine) } diff --git a/Sources/swift-format/Subcommands/Lint.swift b/Sources/swift-format/Subcommands/Lint.swift index 0f6c9bfaf..99d088a39 100644 --- a/Sources/swift-format/Subcommands/Lint.swift +++ b/Sources/swift-format/Subcommands/Lint.swift @@ -28,7 +28,7 @@ extension SwiftFormatCommand { @Flag( name: .shortAndLong, - help: "Fail on warnings. Deprecated: All findings are treated as errors now." + help: "Treat all findings as errors instead of warnings." ) var strict: Bool = false @@ -37,15 +37,11 @@ extension SwiftFormatCommand { func run() throws { try performanceMeasurementOptions.printingInstructionCountIfRequested { - let frontend = LintFrontend(configurationOptions: configurationOptions, lintFormatOptions: lintOptions) - - if strict { - frontend.diagnosticsEngine.emitWarning( - """ - Running swift-format with --strict is deprecated and will be removed in the future. - """ - ) - } + let frontend = LintFrontend( + configurationOptions: configurationOptions, + lintFormatOptions: lintOptions, + treatWarningsAsErrors: strict + ) frontend.run() if frontend.diagnosticsEngine.hasErrors { diff --git a/Sources/swift-format/Utilities/DiagnosticsEngine.swift b/Sources/swift-format/Utilities/DiagnosticsEngine.swift index d014c8129..5d51bef71 100644 --- a/Sources/swift-format/Utilities/DiagnosticsEngine.swift +++ b/Sources/swift-format/Utilities/DiagnosticsEngine.swift @@ -26,20 +26,28 @@ final class DiagnosticsEngine { /// A Boolean value indicating whether any warnings were emitted by the diagnostics engine. private(set) var hasWarnings: Bool + /// Whether to upgrade all warnings to errors. + private let treatWarningsAsErrors: Bool + /// Creates a new diagnostics engine with the given diagnostic handlers. /// /// - Parameter diagnosticsHandlers: An array of functions, each of which takes a `Diagnostic` as /// its sole argument and returns `Void`. The functions are called whenever a diagnostic is /// received by the engine. - init(diagnosticsHandlers: [(Diagnostic) -> Void]) { + init(diagnosticsHandlers: [(Diagnostic) -> Void], treatWarningsAsErrors: Bool = false) { self.handlers = diagnosticsHandlers self.hasErrors = false self.hasWarnings = false + self.treatWarningsAsErrors = treatWarningsAsErrors } /// Emits the diagnostic by passing it to the registered handlers, and tracks whether it was an /// error or warning diagnostic. private func emit(_ diagnostic: Diagnostic) { + var diagnostic = diagnostic + if treatWarningsAsErrors, diagnostic.severity == .warning { + diagnostic.severity = .error + } switch diagnostic.severity { case .error: self.hasErrors = true case .warning: self.hasWarnings = true @@ -135,7 +143,7 @@ final class DiagnosticsEngine { /// diagnostics engine and returns it. private func diagnosticMessage(for finding: Finding) -> Diagnostic { return Diagnostic( - severity: .error, + severity: .warning, location: finding.location.map(Diagnostic.Location.init), category: "\(finding.category)", message: "\(finding.message.text)"