From 238f19449f2d14fee0783d875a2ffa97419002ef Mon Sep 17 00:00:00 2001 From: Luis Peixoto Date: Thu, 29 Oct 2026 10:47:44 -0300 Subject: [PATCH 1/2] Fix Swift formatter version detection --- scripts/__tests__/format-swift-test.js | 57 ++++++++++++++++++++++++++ scripts/format-swift.js | 15 ++++++- 2 files changed, 71 insertions(+), 1 deletion(-) create mode 100644 scripts/__tests__/format-swift-test.js diff --git a/scripts/__tests__/format-swift-test.js b/scripts/__tests__/format-swift-test.js new file mode 100644 index 000000000000..b5dedb31a952 --- /dev/null +++ b/scripts/__tests__/format-swift-test.js @@ -0,0 +1,57 @@ +/** + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + * + * @noflow + * @format + */ + +'use strict'; + +jest.mock('../format-utils', () => ({ + IS_META_CHECKOUT: false, + commandVersion: jest.fn(), + findMetaTool: jest.fn(() => null), +})); +jest.mock('tinyglobby', () => ({globSync: jest.fn(() => [])})); + +const originalSwiftFormat = process.env.SWIFT_FORMAT; + +beforeEach(() => { + delete process.env.SWIFT_FORMAT; +}); + +afterEach(() => { + if (originalSwiftFormat === undefined) { + delete process.env.SWIFT_FORMAT; + } else { + process.env.SWIFT_FORMAT = originalSwiftFormat; + } + jest.resetModules(); + jest.clearAllMocks(); + jest.restoreAllMocks(); +}); + +test.each([ + ['6.4', true], + ['6.2', false], +])('checks the Swift toolchain version when the formatter reports main (%s)', (version, accepted) => { + const {commandVersion} = require('../format-utils'); + const {globSync} = require('tinyglobby'); + commandVersion.mockImplementation((command, args) => { + if (command === 'swift' && args?.[0] === 'format') { + return {status: 0, output: 'main'}; + } + if (command === 'swift') { + return {status: 0, output: `Apple Swift version ${version} (swiftlang)`}; + } + return {status: 1, output: ''}; + }); + jest.spyOn(console, 'warn').mockImplementation(() => {}); + + require('../format-swift'); + + expect(globSync).toHaveBeenCalledTimes(accepted ? 1 : 0); +}); diff --git a/scripts/format-swift.js b/scripts/format-swift.js index 74885bd7d79d..342a16efd7a8 100644 --- a/scripts/format-swift.js +++ b/scripts/format-swift.js @@ -81,7 +81,20 @@ function findSwiftFormat() { } for (const [command, prefixArguments] of candidates) { const result = commandVersion(command, prefixArguments); - const version = parseSwiftFormatVersion(result.output); + let version = parseSwiftFormatVersion(result.output); + if ( + result.status === 0 && + version == null && + result.output.trim() === 'main' && + command === 'swift' && + prefixArguments.length === 1 && + prefixArguments[0] === 'format' + ) { + const toolchain = commandVersion(command); + if (toolchain.status === 0) { + version = parseSwiftFormatVersion(toolchain.output); + } + } if ( result.status === 0 && version != null && From 424e0db5789e8d14e42ffe354d3d022b7b0cf103 Mon Sep 17 00:00:00 2001 From: Luis Peixoto Date: Thu, 29 Oct 2026 18:56:49 -0300 Subject: [PATCH 2/2] Format Swift formatter tests --- scripts/__tests__/format-swift-test.js | 42 +++++++++++++++----------- 1 file changed, 24 insertions(+), 18 deletions(-) diff --git a/scripts/__tests__/format-swift-test.js b/scripts/__tests__/format-swift-test.js index b5dedb31a952..1239400a1109 100644 --- a/scripts/__tests__/format-swift-test.js +++ b/scripts/__tests__/format-swift-test.js @@ -37,21 +37,27 @@ afterEach(() => { test.each([ ['6.4', true], ['6.2', false], -])('checks the Swift toolchain version when the formatter reports main (%s)', (version, accepted) => { - const {commandVersion} = require('../format-utils'); - const {globSync} = require('tinyglobby'); - commandVersion.mockImplementation((command, args) => { - if (command === 'swift' && args?.[0] === 'format') { - return {status: 0, output: 'main'}; - } - if (command === 'swift') { - return {status: 0, output: `Apple Swift version ${version} (swiftlang)`}; - } - return {status: 1, output: ''}; - }); - jest.spyOn(console, 'warn').mockImplementation(() => {}); - - require('../format-swift'); - - expect(globSync).toHaveBeenCalledTimes(accepted ? 1 : 0); -}); +])( + 'checks the Swift toolchain version when the formatter reports main (%s)', + (version, accepted) => { + const {commandVersion} = require('../format-utils'); + const {globSync} = require('tinyglobby'); + commandVersion.mockImplementation((command, args) => { + if (command === 'swift' && args?.[0] === 'format') { + return {status: 0, output: 'main'}; + } + if (command === 'swift') { + return { + status: 0, + output: `Apple Swift version ${version} (swiftlang)`, + }; + } + return {status: 1, output: ''}; + }); + jest.spyOn(console, 'warn').mockImplementation(() => {}); + + require('../format-swift'); + + expect(globSync).toHaveBeenCalledTimes(accepted ? 1 : 0); + }, +);