.env files are not found when running unit tests using Xcode 26.4

This isn’t directly related to Vapor and certainly not Vapor’s fault, but I’ve run into this issue in a Vapor project and I assume other people who follow Vapor’s documentation are affected by this, too:

According to the release notes of Xcode 26.4, an issue was resolved:

  • Fixed: When running unit test targets with no host application, the test runner is now consistently launched with a default working directory of /tmp, from both Xcode and xcodebuild. (162549425)

This Vapor project I’m working on has relied on the app itself as well as the unit tests loading the .env files from the current working directory, as implemented by static DotEnvFile.load(for:fileio:logger:) in Vapor. The project is a Swift package that we open in Xcode and as described in the Vapor documentation, everyone working on this project sets the working directory in Xcode’s scheme editor (the one Xcode implicitly generates at .swiftpm/xcode/package.xcworkspace/) to the project root because that’s where the .env files are located:

This works just fine when running the app, but starting with Xcode 26.4, it doesn’t work anymore when running the unit tests. Previously, it seemed like the Run action’s working directory was implicitly used for the Test action, but that doesn’t happen anymore (see quoted release notes above). Unfortunately, the UI for the Test action looks differently and there is no way to set a custom working directory for the unit tests:

The result is that Vapor fails to load the .env files when running unit tests because they are not located in /tmp, obviously.

So my question is: How do other people deal with this? Is there a better way to set all this up?

Since this approach is the only one mentioned in the Vapor documentation, I assume others are also doing it this way. One workaround we found is using FileManager.default.changeCurrentDirectoryPath(_:) before calling Vapor’s static Application.make(_:_:), but that doesn’t seem right and it’s also not clear how to best pass in the path to set. I also tried setting the PWD environment variable using the scheme editor, but that does not seem to work.

Not really relevant, but this is using Vapor version 4.121.4.

2 Likes

Same here — testing isn’t working in Xcode 26.4 or 26.4.1.
Using Xcode 26.3 allows all tests to run successfully.

I also tried moving the .env file to the project root, but that didn’t help.

Any workarounds?

The workaround I mentioned at the end (using FileManager.default.changeCurrentDirectoryPath(_:) before calling Vapor’s static Application.make(_:_:)) works, but it’s cumbersome because you either have to hardcode the path or pass it in via some other way (e.g. an environment variable configured in the Xcode scheme).

FYI, I also opened a GitHub issue about this: Behavior change in Xcode 26.4 causes Vapor to not find .env files in unit test, rendering documentation invalid · Issue #3454 · vapor/vapor · GitHub

…and now I also filed a feedback: FB22530511 (should be a duplicate of FB22357181).