Repository: flutter
Issue: [macOS]: PathNotFoundException when building app with flavors
Labels: c: regression,c: crash,tool,platform-mac,a: build,has reproducible steps,P2,found in release: 3.22,team-macos,triaged-macos

### Steps to reproduce

1. Use Xcode to open the macOS project
2. Copy a target
3. Rename the new scheme to AppStore
4. Run the command `flutter build macos --flavor AppStore`

### Expected results

After setting the flavor, building macOS applications runs normally, just like using version 3.19.6.

### Actual results

PathNotFoundException: PathNotFoundException: Directory listing failed, path = 'build/macos/Build/Products/Release-AppStore/FaFaRunnerAppStore.app/' (OS Error: No such file or directory, errno = 2)

```
#0      _Directory._fillWithDirectoryListing (dart:io-patch/directory_patch.dart:42:24)
#1      _Directory.listSync (dart:io/directory_impl.dart:228:5)
#2      ForwardingDirectory.listSync (package:file/src/forwarding/forwarding_directory.dart:43:12)
#3      ForwardingDirectory.listSync (package:file/src/forwarding/forwarding_directory.dart:43:12)
#4      OperatingSystemUtils.getDirectorySize (package:flutter_tools/src/base/os.dart:111:53)
#5      buildMacOS (package:flutter_tools/src/macos/build_macos.dart:172:43)
<asynchronous suspension>
#6      BuildMacosCommand.runCommand (package:flutter_tools/src/commands/build_macos.dart:62:5)
<asynchronous suspension>
#7      FlutterCommand.run.<anonymous closure> (package:flutter_tools/src/runner/flutter_command.dart:1394:27)
<asynchronous suspension>
#8      AppContext.run.<anonymous closure> (package:flutter_tools/src/base/context.dart:153:19)
<asynchronous suspension>
#9      CommandRunner.runCommand (package:args/command_runner.dart:212:13)
<asynchronous suspension>
#10     FlutterCommandRunner.runCommand.<anonymous closure> (package:flutter_tools/src/runner/flutter_command_runner.dart:372:9)
<asynchronous suspension>
#11     AppContext.run.<anonymous closure> (package:flutter_tools/src/base/context.dart:153:19)
<asynchronous suspension>
#12     FlutterCommandRunner.runCommand (package:flutter_tools/src/runner/flutter_command_runner.dart:308:5)
<asynchronous suspension>
#13     run.<anonymous closure>.<anonymous closure> (package:flutter_tools/runner.dart:130:9)
<asynchronous suspension>
#14     AppContext.run.<anonymous closure> (package:flutter_tools/src/base/context.dart:153:19)
<asynchronous suspension>
#15     main (package:flutter_tools/executable.dart:93:3)
<asynchronous suspension>
```

### Code sample

<details open><summary>Code sample</summary>

```dart
[Paste your code here]
```

</details>


### Screenshots or Video

<details open>
<summary>Screenshots / Video demonstration</summary>

[Upload media here]

</details>


### Logs

<details open><summary>Logs</summary>

```console
[Paste your logs here]
```

</details>


### Flutter Doctor output

<details open><summary>Doctor output</summary>

```console
[32m[✓][39m Flutter (Channel stable, 3.22.0, on macOS 14.5 23F79 darwin-x64, locale zh-Hans-CN)
    [32m•[39m Flutter version 3.22.0 on channel stable at /Users/ying/flutter
    [32m•[39m Upstream repository https://github.com/flutter/flutter.git
    [32m•[39m Framework revision 5dcb86f68f (8 days ago), 2024-05-09 07:39:20 -0500
    [32m•[39m Engine revision f6344b75dc
    [32m•[39m Dart version 3.4.0
    [32m•[39m DevTools version 2.34.3

[32m[✓][39m Android toolchain - develop for Android devices (Android SDK version 34.0.0)
    [32m•[39m Android SDK at /Users/ying/Library/Android/sdk
    [32m•[39m Platform android-34, build-tools 34.0.0
    [32m•[39m ANDROID_SDK_ROOT = /Users/ying/Library/Android/sdk
    [32m•[39m Java binary at: /Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java
    [32m•[39m Java version OpenJDK Runtime Environment (build 17.0.10+0-17.0.10b1087.21-11572160)
    [32m•[39m All Android licenses accepted.

[32m[✓][39m Xcode - develop for iOS and macOS (Xcode 15.4)
    [32m•[39m Xcode at /Applications/Xcode.app/Contents/Developer
    [32m•[39m Build 15F31d
    [32m•[39m CocoaPods version 1.15.2

[32m[✓][39m Chrome - develop for the web
    [32m•[39m Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[32m[✓][39m Android Studio (version 2023.3)
    [32m•[39m Android Studio at /Applications/Android Studio.app/Contents
    [32m•[39m Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    [32m•[39m Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    [32m•[39m Java version OpenJDK Runtime Environment (build 17.0.10+0-17.0.10b1087.21-11572160)

[32m[✓][39m VS Code (version 1.89.1)
    [32m•[39m VS Code at /Applications/Visual Studio Code.app/Contents
    [32m•[39m Flutter extension version 3.88.0

[32m[✓][39m Connected device (3 available)
    [32m•[39m Kimmy's iPhone (mobile) • 00008020-000A1D110A32002E • ios            • iOS 17.5 21F79
    [32m•[39m macOS (desktop)         • macos                     • darwin-x64     • macOS 14.5 23F79 darwin-x64
    [32m•[39m Chrome (web)            • chrome                    • web-javascript • Google Chrome 125.0.6422.61

[33m[!][39m Network resources
    [31m✗[39m A network error occurred while checking "https://maven.google.com/": Operation timed out
    [31m✗[39m An HTTP error occurred while checking "https://github.com/": Connection closed before full header was received

[33m![39m Doctor found issues in 1 category.
```

</details>