`swift package plugin --list` ignores `swift.buildPath`
bug
### Description
When `swift.buildPath` is configured, `BuildFlags.withSwiftSDKFlags` adds the necessary parameters to the `package` command. This doesn't work today for the `plugin` subcommand since #1996 added an early exit.
However, while #1996 silenced the issue, I believe we can actually address it.
When operating on the `package` command, `BuildFlags.withSwiftSDKFlags` puts the flags _after_ the subcommand, which work for `diagnose` and `dump-symbol-graph` (maybe because they don't have further subcommands with positional arguments), but not for `plugin`:
```
❯ swift package plugin --scratch-path /tmp/scratch --list
error: Unknown option '--scratch-path'
```
The issue is that `--scratch-path` is a parameter for the `package` command, and therefore should be added _before_ the subcommand:
```
# This works because `--scratch-path` is passed to `package`, not `plugin`
❯ swift package --scratch-path /tmp/scratch plugin --list
Fetching https://github.com/apple/swift-collections.git from cache
[...]
```
### Fixing the issue
I believe the fix should be as simple as changing where we add the build path flags in [BuildFlags.ts](https://github.com/swiftlang/vscode-swift/blob/main/src/toolchain/BuildFlags.ts#L50-L51), which would then allow us to remove the special flag
```typescript
case "package": {
// Build-path flags belong on the parent `package` command, before any
// subcommand, otherwise SwiftPM's argument parser rejects them when
// the subcommand itself has further subcommands or positional args
// (e.g. `swift package plugin --list`).
const command = args.splice(0, 1).concat(this.buildPathFlags());
// command = ["package", "--scratch-path", "<path>"], args = [subcommand, ...]
switch (args[0]) {
case "dump-symbol-graph":
case "diagnose-api-breaking-changes":
case "resolve":
return [...command, ...this.swiftpmSDKFlags(), ...args];
default:
return command.concat(args);
}
}
```
### Steps to reproduce
Notes: I'm reproducing using [build-your-own-redis](https://github.com/codecrafters-io/build-your-own-redis/), specifically within the [`starter_templates/swift/code` folder](https://github.com/codecrafters-io/build-your-own-redis/tree/main/starter_templates/swift/code). But this likely reproduces with any Swift PM projects, especially if they have external dependencies.
1. Checkout [build-your-own-redis](https://github.com/codecrafters-io/build-your-own-redis/)
2. Create `.vscode/settings.json` to set `swift.buildPath` somewhere in `/tmp`. For example: `{"swift.buildPath": "/tmp/codecrafters-swift-scratch/"}`
3. Make sure no `.build` folder exists
```
fd -u '^\.build$' --exec rm {}
```
4. Open the workspace with VS code at the root
5. Open `starter_templates/swift/code/Sources/main.swift`
6. Wait a minute or so, during which you ideally should see the Swift plugin doing a few things (indexing, etc.)
7. At the root of the workspace, look for `.build` folders:
```
fd -u '^\.build$'
```
You will see a `.build` that was created:
```
❯ fd -u '^\.build$'
starter_templates/swift/code/.build/
```
### Proof of root cause
I noticed the issue while using the `course-sdk` tool, where files were often missing or having permission issues. I realised this was because `course-sdk` generates new Swift files, and with VS code open in the workspace, `course-sdk`'s use of SwiftPM and listing + copying files created some races with VS code's Swift PM reindexing.
I therefore looked to move all build files to `/tmp` using `swift.buildPath`, and while a lot ended up in the `/tmp` folder, I still had race issues because `.build` kept being created.
I used [Mac Monitor](https://github.com/Brandon7CC/mac-monitor) to find out who/what was creating the folder, and found this call chain:
```
launchd
└── Code
└── Code Helper (Plugin)
└── swift-package plugin --list # cwd = starter_templates/swift/code
└── CREATE starter_templates/swift/code/.build/
```
I then came to GitHub, looked for issues & PRs with `--scratch-path`, and found #1996.
### Expected behavior
`/tmp/codecrafters-swift-scratch` exists, and no `.build` folder is found in the workspace.
### Extension version
2.16.4
### Swift & OS version
Swift: 6.3
macOS: 26.4.1
关闭于 2026-05-16 5 条评论