Skip to content

Commit b07d6d2

Browse files
committed
fix(config): make .env file loading robust
- Refactors `EnvironmentConfig` to actively search for the `.env` file by traversing up the directory tree from the current working directory. - This change makes the server startup process resilient to cases where the Dart Frog development server's execution context is not the project root, resolving the "file not found" error for `.env`.
1 parent 38853ea commit b07d6d2

File tree

2 files changed

+48
-5
lines changed

2 files changed

+48
-5
lines changed

.env

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# This is an example environment file.
2+
# Copy this file to .env and fill in your actual configuration values.
3+
# The .env file is ignored by Git and should NOT be committed.
4+
5+
# DATABASE_URL="mongodb://user:password@localhost:27017/ht_api_db"
6+
DATABASE_URL="mongodb://localhost:27017/ht_api_db"

lib/src/config/environment_config.dart

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import 'dart:io';
2+
13
import 'package:dotenv/dotenv.dart';
24
import 'package:logging/logging.dart';
35

@@ -11,11 +13,46 @@ import 'package:logging/logging.dart';
1113
abstract final class EnvironmentConfig {
1214
static final _log = Logger('EnvironmentConfig');
1315

14-
// The DotEnv instance that loads the .env file and platform variables.
15-
// It's initialized once and reused.
16-
static final _env = DotEnv(includePlatformEnvironment: true)..load();
16+
// The DotEnv instance is now loaded via a helper method to make it more
17+
// resilient to current working directory issues.
18+
static final _env = _loadEnv();
19+
20+
/// Helper method to load the .env file more robustly.
21+
///
22+
/// It searches for the .env file starting from the current directory
23+
/// and moving up to parent directories. This makes it resilient to
24+
/// issues where the execution context's working directory is not the
25+
/// project root.
26+
static DotEnv _loadEnv() {
27+
final env = DotEnv(includePlatformEnvironment: true);
28+
var dir = Directory.current;
29+
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;
38+
}
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;
52+
}
53+
}
1754

18-
/// Retrieves the PostgreSQL database connection URI from the environment.
55+
/// Retrieves the database connection URI from the environment.
1956
///
2057
/// The value is read from the `DATABASE_URL` environment variable.
2158
///
@@ -33,7 +70,7 @@ abstract final class EnvironmentConfig {
3370
return dbUrl;
3471
}
3572

36-
/// Retrieves the current environment mode (e.g., 'development', 'production').
73+
/// Retrieves the current environment mode (e.g., 'development').
3774
///
3875
/// The value is read from the `ENV` environment variable.
3976
/// Defaults to 'production' if the variable is not set.

0 commit comments

Comments
 (0)