Skip to content

Commit 20a92d2

Browse files
committed
fix(config): correct directory traversal in .env search
- Fixes a subtle error in the directory traversal logic within the `EnvironmentConfig._loadEnv` method. The previous implementation had a flaw in how it checked for the parent directory, which could prevent it from correctly locating the `pubspec.yaml` and `.env` files in certain project structures or when the Dart Frog development server starts from a subdirectory. - The corrected logic now accurately traverses up the directory tree, repeatedly checking if the parent directory is the same as the current directory (indicating the root of the filesystem). This ensures that the search correctly identifies the project root and locates the `.env` file.
1 parent 7b24b53 commit 20a92d2

File tree

1 file changed

+24
-31
lines changed

1 file changed

+24
-31
lines changed

lib/src/config/environment_config.dart

Lines changed: 24 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -24,40 +24,33 @@ abstract final class EnvironmentConfig {
2424
/// issues where the execution context's working directory is not the
2525
/// project root.
2626
static DotEnv _loadEnv() {
27-
final env = DotEnv(includePlatformEnvironment: true);
28-
try {
29-
// Find the project root by looking for pubspec.yaml, then find .env
30-
var dir = Directory.current;
31-
while (true) {
32-
final pubspecFile = File('${dir.path}/pubspec.yaml');
33-
if (pubspecFile.existsSync()) {
34-
// Found project root, now look for .env in this directory
35-
final envFile = File('${dir.path}/.env');
36-
if (envFile.existsSync()) {
37-
_log.info('Found .env file at: ${envFile.path}');
38-
env.load([envFile.path]);
39-
return env;
40-
}
41-
break; // Found pubspec but no .env, break and fall back
27+
final env = DotEnv(includePlatformEnvironment: true); // Start with default
28+
var currentDir = Directory.current;
29+
_log.fine('Starting .env search from: ${currentDir.path}');
30+
// Traverse up the directory tree to find pubspec.yaml
31+
while (currentDir.parent.path != currentDir.path) {
32+
final pubspecPath = '${currentDir.path}/pubspec.yaml';
33+
_log.finer('Checking for pubspec.yaml at: ');
34+
if (File(pubspecPath).existsSync()) {
35+
// Found pubspec.yaml, now load .env from the same directory
36+
final envPath = '${currentDir.path}/.env';
37+
_log.info('Found pubspec.yaml, now looking for .env at: ');
38+
if (File(envPath).existsSync()) {
39+
_log.info('Found .env file at: ');
40+
env.load([envPath]); // Load variables from the found .env file
41+
return env; // Return immediately upon finding
42+
} else {
43+
_log.warning('pubspec.yaml found, but no .env in the same directory.');
44+
break; // Stop searching since pubspec.yaml should contain .env
4245
}
43-
44-
// Stop if we have reached the root of the filesystem.
45-
if (dir.parent.path == dir.path) {
46-
break;
47-
}
48-
dir = dir.parent;
4946
}
50-
} catch (e) {
51-
_log.warning('Error during robust .env search: $e. Falling back.');
47+
currentDir = currentDir.parent; // Move to the parent directory
48+
_log.finer('Moving up to parent directory: ${currentDir.path}');
5249
}
53-
54-
// Fallback for when the robust search fails
55-
_log.warning(
56-
'.env file not found by searching for project root. '
57-
'Falling back to default load().',
58-
);
59-
env.load();
60-
return env;
50+
// If loop completes without returning, .env was not found
51+
_log.warning('.env not found by searching. Falling back to default load().');
52+
env.load(); // Fallback to default load
53+
return env; // Return even if fallback
6154
}
6255

6356
/// Retrieves the database connection URI from the environment.

0 commit comments

Comments
 (0)