Skip to content

Commit 7b24b53

Browse files
committed
/// issues where the execution context's working directory is not the
/// project root. static DotEnv _loadEnv() { final env = DotEnv(includePlatformEnvironment: true); try { // Find the project root by looking for pubspec.yaml, then find .env var dir = Directory.current; while (true) { final pubspecFile = File('${dir.path}/pubspec.yaml'); if (pubspecFile.existsSync()) { // Found project root, now look for .env in this directory final envFile = File('${dir.path}/.env'); if (envFile.existsSync()) { _log.info('Found .env file at: ${envFile.path}'); env.load([envFile.path]); return env; } break; // Found pubspec but no .env, break and fall back } // Stop if we have reached the root of the filesystem. if (dir.parent.path == dir.path) { break; } dir = dir.parent; } } catch (e) { _log.warning('Error during robust .env search: $e. Falling back.'); } // Fallback for when the robust search fails _log.warning( '.env file not found by searching for project root. ' 'Falling back to default load().', ); env.load(); return env; } /// Retrieves the database connection URI from the environment.
1 parent b07d6d2 commit 7b24b53

File tree

1 file changed

+30
-22
lines changed

1 file changed

+30
-22
lines changed

lib/src/config/environment_config.dart

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -25,31 +25,39 @@ abstract final class EnvironmentConfig {
2525
/// project root.
2626
static DotEnv _loadEnv() {
2727
final env = DotEnv(includePlatformEnvironment: true);
28-
var dir = Directory.current;
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
42+
}
2943

30-
// Loop to search up the directory tree for the .env file.
31-
while (true) {
32-
final envFile = File('${dir.path}/.env');
33-
if (envFile.existsSync()) {
34-
_log.info('Found .env file at: ${envFile.path}');
35-
// Load the variables from the found file.
36-
env.load([envFile.path]);
37-
return env;
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;
3849
}
39-
40-
// Stop if we have reached the root of the filesystem.
41-
if (dir.parent.path == dir.path) {
42-
_log.warning(
43-
'.env file not found by searching. Falling back to default load().',
44-
);
45-
// Fallback to the original behavior if no file is found.
46-
env.load();
47-
return env;
48-
}
49-
50-
// Move up to the parent directory.
51-
dir = dir.parent;
50+
} catch (e) {
51+
_log.warning('Error during robust .env search: $e. Falling back.');
5252
}
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;
5361
}
5462

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

0 commit comments

Comments
 (0)