forklaunch 1.15.0

Launch faster with forklaunch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
use std::collections::HashMap;

use anyhow::Result;

use crate::core::{
    ast::infrastructure::env::{EnvVarUsage, fold_optionality},
    manifest::{ProjectType, application::ApplicationManifestData},
};

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub(crate) enum EnvironmentVariableScope {
    Application,
    Service,
    Worker,
}

#[derive(Debug, Clone)]
pub(crate) struct ScopedEnvVar {
    pub name: String,
    pub scope: EnvironmentVariableScope,
    pub scope_id: Option<String>, // service/worker name if scoped
    pub used_by: Vec<String>,     // List of projects using this variable
    pub value: Option<String>,    // Captured value from docker-compose (if available)
    /// Declared optionality folded across every project that uses this
    /// variable. `None` means no project declared a type for it.
    pub optional: Option<bool>,
}

impl EnvironmentVariableScope {
    #[allow(dead_code)]
    pub(crate) fn as_str(&self) -> &'static str {
        match self {
            EnvironmentVariableScope::Application => "application",
            EnvironmentVariableScope::Service => "service",
            EnvironmentVariableScope::Worker => "worker",
        }
    }
}

/// Determine the scope for each environment variable based on usage patterns
pub(crate) fn determine_env_var_scopes(
    project_env_vars: &HashMap<String, Vec<EnvVarUsage>>,
    manifest: &ApplicationManifestData,
) -> Result<Vec<ScopedEnvVar>> {
    let mut var_usage: HashMap<String, Vec<String>> = HashMap::new();
    // Optionality is folded a second time here: a variable promoted to
    // application scope is one variable, so a project declaring it required
    // must win over another declaring it optional.
    let mut var_optionality: HashMap<String, Vec<Option<bool>>> = HashMap::new();

    for (project_name, env_vars) in project_env_vars {
        for env_var in env_vars {
            var_usage
                .entry(env_var.var_name.clone())
                .or_insert_with(Vec::new)
                .push(project_name.clone());
            var_optionality
                .entry(env_var.var_name.clone())
                .or_insert_with(Vec::new)
                .push(env_var.optional);
        }
    }

    let mut scoped_vars = Vec::new();

    for (var_name, projects) in var_usage {
        let mut unique_projects: Vec<String> = projects
            .into_iter()
            .collect::<std::collections::HashSet<_>>()
            .into_iter()
            .collect();
        unique_projects.sort();

        let (scope, scope_id) = if unique_projects.len() > 1
            || unique_projects
                .iter()
                .any(|p| p == "core" || p == "monitoring")
        {
            (EnvironmentVariableScope::Application, None)
        } else if let Some(project_name) = unique_projects.first() {
            let project_type = manifest
                .projects
                .iter()
                .find(|p| &p.name == project_name)
                .map(|p| &p.r#type);

            match project_type {
                Some(ProjectType::Service) => (
                    EnvironmentVariableScope::Service,
                    Some(project_name.clone()),
                ),
                Some(ProjectType::Worker) => {
                    (EnvironmentVariableScope::Worker, Some(format!("{}-worker", project_name)))
                }
                _ => (EnvironmentVariableScope::Application, None),
            }
        } else {
            (EnvironmentVariableScope::Application, None)
        };

        let optional = fold_optionality(
            var_optionality
                .get(&var_name)
                .map(|v| v.as_slice())
                .unwrap_or(&[]),
        );

        scoped_vars.push(ScopedEnvVar {
            name: var_name,
            scope,
            scope_id,
            used_by: unique_projects,
            value: None,
            optional,
        });
    }

    // Collect project names for inter-service URL detection
    let project_names: Vec<String> = manifest.projects.iter().map(|p| p.name.clone()).collect();

    // Post-process: promote observability/monitoring vars and inter-service URL vars to Application scope
    for var in &mut scoped_vars {
        // Promote OTEL_, LOKI_, TEMPO_, PROMETHEUS_ vars to Application scope
        if is_observability_var(&var.name) {
            var.scope = EnvironmentVariableScope::Application;
            var.scope_id = None;
            continue;
        }

        // Promote inter-service URL vars to Application scope
        if is_inter_service_url_var(&var.name, &project_names) {
            var.scope = EnvironmentVariableScope::Application;
            var.scope_id = None;
            continue;
        }

        // Promote common infrastructure/application vars to Application scope
        if is_common_var(&var.name) {
            var.scope = EnvironmentVariableScope::Application;
            var.scope_id = None;
        }
    }

    scoped_vars.sort_by(|a, b| match a.scope.cmp(&b.scope) {
        std::cmp::Ordering::Equal => a.name.cmp(&b.name),
        other => other,
    });

    Ok(scoped_vars)
}

/// Vars that are inherently per-component and must never be promoted to application scope.
/// Each service/worker has its own distinct value for these.
const NEVER_APPLICATION_SCOPED: &[&str] = &["QUEUE_NAME", "OTEL_SERVICE_NAME", "REDIS_URL"];

/// Check if a var is inherently per-component and must never be application-scoped.
pub(crate) fn is_never_application_scoped(var_name: &str) -> bool {
    let upper = var_name.to_ascii_uppercase();
    NEVER_APPLICATION_SCOPED.iter().any(|&v| v == upper)
}

/// Check if a variable should always be application-scoped (never service/worker-scoped).
/// This covers observability vars, inter-service URLs, HMAC keys, and JWKS keys.
pub(crate) fn is_application_scoped_var(var_name: &str, project_names: &[String]) -> bool {
    is_observability_var(var_name)
        || is_inter_service_url_var(var_name, project_names)
        || is_shared_key_var(var_name)
        || is_common_var(var_name)
}

/// Check if a var name is a shared key variable (HMAC, JWKS) that should be application-scoped.
fn is_shared_key_var(var_name: &str) -> bool {
    let upper = var_name.to_ascii_uppercase();
    if upper.contains("HMAC") {
        return true;
    }
    if upper.contains("JWKS") && upper.contains("PUBLIC") && upper.contains("KEY") {
        return true;
    }
    false
}

/// Common infrastructure and application vars that are always application-scoped.
/// These are shared by all services/workers with the same value.
/// DB_NAME is excluded because it differs per component.
const COMMON_APPLICATION_VARS: &[&str] = &[
    "NODE_ENV",
    "HOST",
    "PORT",
    "PROTOCOL",
    "VERSION",
    "DOCS_PATH",
    "CORS_ORIGINS",
    "DB_HOST",
    "DB_PORT",
    "DB_USER",
    "DB_PASSWORD",
    "DB_SSL",
    "PGSSLMODE",
    "REDIS_TLS",
    "IAM_DB_NAME",
    "OTEL_LEVEL",
];

/// Check if a var name is a common application/infrastructure variable.
fn is_common_var(var_name: &str) -> bool {
    let upper = var_name.to_ascii_uppercase();
    COMMON_APPLICATION_VARS.iter().any(|&v| v == upper)
}

/// Check if a var name is an observability/monitoring variable that should be application-scoped.
/// Per-component vars like OTEL_SERVICE_NAME are excluded since they differ per service.
fn is_observability_var(var_name: &str) -> bool {
    let upper = var_name.to_ascii_uppercase();

    // These are per-component, not application-scoped
    const COMPONENT_SCOPED: &[&str] = &["OTEL_SERVICE_NAME", "OTEL_RESOURCE_ATTRIBUTES"];

    if COMPONENT_SCOPED.iter().any(|&v| v == upper) {
        return false;
    }

    upper.starts_with("OTEL_")
        || upper.starts_with("LOKI_")
        || upper.starts_with("TEMPO_")
        || upper.starts_with("MIMIR_")
        // Retained for back-compat: projects scaffolded before the Mimir
        // migration may still carry PROMETHEUS_* vars.
        || upper.starts_with("PROMETHEUS_")
}

/// Auth/infrastructure URL vars that Pulumi computes at deploy time.
const PULUMI_INJECTED_URL_VARS: &[&str] = &[
    "JWKS_PUBLIC_KEY_URL",
    "BETTER_AUTH_BASE_URL",
];

/// Check if a var is a Pulumi-injected URL var (auth/infrastructure URLs).
pub(crate) fn is_pulumi_injected_url(var_name: &str) -> bool {
    let upper = var_name.to_ascii_uppercase();
    PULUMI_INJECTED_URL_VARS.iter().any(|&v| v == upper)
}

/// Public wrapper: check if a var name is an inter-service URL variable.
/// These are Pulumi-injected at deploy time and should not appear in .env.template files.
pub(crate) fn is_inter_service_url(var_name: &str, project_names: &[String]) -> bool {
    is_inter_service_url_var(var_name, project_names)
}

/// Check if a var is Pulumi-injected at deploy time (inter-service URLs or auth URLs).
pub(crate) fn is_pulumi_injected(var_name: &str, project_names: &[String]) -> bool {
    is_inter_service_url_var(var_name, project_names) || is_pulumi_injected_url(var_name)
}

/// Known infixes for inter-service URL vars mapped to normalized transport.
/// "http", "api", "service" all resolve to "http". "ws" resolves to "ws".
const KNOWN_URL_INFIXES: &[(&str, &str)] = &[
    ("HTTP", "http"),
    ("WS", "ws"),
    ("API", "http"),
    ("SERVICE", "http"),
    ("GRPC", "grpc"),
];

/// Check if a var name matches the pattern `{SERVICE_NAME}[_INFIX]_{URL|URI|FQDN|HOST}`
/// where SERVICE_NAME corresponds to a known project (converted from kebab-case to SCREAMING_SNAKE_CASE)
/// and INFIX is an optional segment like `SERVICE`, `API`, etc.
fn is_inter_service_url_var(var_name: &str, project_names: &[String]) -> bool {
    parse_inter_service_url_var(var_name, project_names).is_some()
}

/// Parse an inter-service URL var name into (target_service, transport, port_env_var).
/// Transport is normalized: "api"/"service"/"http" → "http", "ws" → "ws".
/// port_env_var indicates which env var on the target service provides the port
/// (e.g. "PORT" for http, "WS_PORT" for ws).
/// Returns `None` if the var doesn't match the pattern.
pub(crate) fn parse_inter_service_url_var(
    var_name: &str,
    project_names: &[String],
) -> Option<(String, String, String)> {
    let upper = var_name.to_ascii_uppercase();

    const URL_SUFFIXES: &[&str] = &["_URL", "_URI", "_FQDN", "_HOST"];

    for suffix in URL_SUFFIXES {
        if let Some(prefix) = upper.strip_suffix(suffix) {
            for project_name in project_names {
                let screaming = project_name.to_ascii_uppercase().replace('-', "_");
                if prefix == screaming {
                    // Exact match: e.g. BILLING_URL → http transport, PORT
                    return Some((project_name.clone(), "http".to_string(), "PORT".to_string()));
                }
                if let Some(rest) = prefix.strip_prefix(&screaming) {
                    if rest.starts_with('_') {
                        let infix = &rest[1..]; // strip leading '_'
                        let transport = KNOWN_URL_INFIXES
                            .iter()
                            .find(|(k, _)| *k == infix)
                            .map(|(_, v)| *v)
                            .unwrap_or("http");
                        let port_env_var = match transport {
                            "ws" => "WS_PORT",
                            "grpc" => "GRPC_PORT",
                            _ => "PORT",
                        };
                        return Some((
                            project_name.clone(),
                            transport.to_string(),
                            port_env_var.to_string(),
                        ));
                    }
                }
            }
        }
    }

    None
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::core::manifest::{
        ProjectEntry, ProjectType, ResourceInventory, application::ApplicationManifestData,
    };

    fn make_manifest(projects: Vec<(&str, ProjectType)>) -> ApplicationManifestData {
        ApplicationManifestData {
            id: "test".to_string(),
            cli_version: "1.0.0".to_string(),
            app_name: "test".to_string(),
            camel_case_app_name: "test".to_string(),
            pascal_case_app_name: "Test".to_string(),
            kebab_case_app_name: "test".to_string(),
            title_case_app_name: "Test".to_string(),
            modules_path: "src/modules".to_string(),
            docker_compose_path: None,
            dockerfile: None,
            git_repository: None,
            runtime: "node".to_string(),
            formatter: "prettier".to_string(),
            linter: "eslint".to_string(),
            validator: "zod".to_string(),
            http_framework: "express".to_string(),
            test_framework: None,
            app_description: "Test".to_string(),
            author: "Test".to_string(),
            license: "MIT".to_string(),
            projects: projects
                .into_iter()
                .map(|(name, r#type)| ProjectEntry {
                    name: name.to_string(),
                    r#type,
                    description: format!("Test {}", name),
                    variant: None,
                    resources: Some(ResourceInventory {
                        database: None,
                        cache: None,
                        queue: None,
                        object_store: None,
                        redis_partition: None,
                    }),
                    routers: None,
                    metadata: None,
                    serves: None,
                })
                .collect(),
            project_peer_topology: HashMap::new(),
            database: "postgresql".to_string(),
            is_postgres: true,
            is_sqlite: false,
            is_mysql: false,
            is_mariadb: false,
            is_better_sqlite: false,
            is_libsql: false,
            is_mssql: false,
            is_mongo: false,
            is_in_memory_database: false,
            is_eslint: true,
            is_biome: false,
            is_oxlint: false,
            is_prettier: true,
            is_express: true,
            is_hyper_express: false,
            is_zod: true,
            is_typebox: false,
            is_bun: false,
            is_node: true,
            is_vitest: false,
            is_jest: false,
            platform_application_id: None,
            platform_organization_id: None,
            compliance: None,
        }
    }

    #[test]
    fn test_observability_vars_promoted_to_application() {
        let mut project_vars = HashMap::new();
        project_vars.insert(
            "billing".to_string(),
            vec![
                EnvVarUsage {
                    var_name: "OTEL_EXPORTER_OTLP_ENDPOINT".to_string(),
                    optional: None,
                },
                EnvVarUsage {
                    var_name: "PORT".to_string(),
                    optional: None,
                },
            ],
        );

        let manifest = make_manifest(vec![("billing", ProjectType::Service)]);
        let scoped = determine_env_var_scopes(&project_vars, &manifest).unwrap();

        let otel_var = scoped
            .iter()
            .find(|v| v.name == "OTEL_EXPORTER_OTLP_ENDPOINT")
            .unwrap();
        assert_eq!(otel_var.scope, EnvironmentVariableScope::Application);
        assert_eq!(otel_var.scope_id, None);

        let port_var = scoped.iter().find(|v| v.name == "PORT").unwrap();
        assert_eq!(port_var.scope, EnvironmentVariableScope::Application);
    }

    #[test]
    fn test_loki_tempo_prometheus_promoted() {
        assert!(is_observability_var("LOKI_URL"));
        assert!(is_observability_var("TEMPO_ENDPOINT"));
        assert!(is_observability_var("MIMIR_URL"));
        assert!(is_observability_var("PROMETHEUS_PUSH_GATEWAY"));
        assert!(is_observability_var("OTEL_EXPORTER_OTLP_ENDPOINT"));
        assert!(!is_observability_var("DB_HOST"));
        // Per-component OTEL vars stay at service scope
        assert!(!is_observability_var("OTEL_SERVICE_NAME"));
        assert!(!is_observability_var("OTEL_RESOURCE_ATTRIBUTES"));
    }

    #[test]
    fn test_inter_service_url_promoted_to_application() {
        let mut project_vars = HashMap::new();
        project_vars.insert(
            "billing".to_string(),
            vec![EnvVarUsage {
                var_name: "PLATFORM_MANAGEMENT_URL".to_string(),
                optional: None,
            }],
        );

        let manifest = make_manifest(vec![
            ("billing", ProjectType::Service),
            ("platform-management", ProjectType::Service),
        ]);

        let scoped = determine_env_var_scopes(&project_vars, &manifest).unwrap();

        let url_var = scoped
            .iter()
            .find(|v| v.name == "PLATFORM_MANAGEMENT_URL")
            .unwrap();
        assert_eq!(url_var.scope, EnvironmentVariableScope::Application);
        assert_eq!(url_var.scope_id, None);
    }

    #[test]
    fn test_inter_service_url_pattern_matching() {
        let projects = vec![
            "billing".to_string(),
            "platform-management".to_string(),
            "auth".to_string(),
        ];

        assert!(is_inter_service_url_var("BILLING_URL", &projects));
        assert!(is_inter_service_url_var("PLATFORM_MANAGEMENT_URL", &projects));
        assert!(is_inter_service_url_var("AUTH_URI", &projects));
        assert!(is_inter_service_url_var("BILLING_FQDN", &projects));
        assert!(is_inter_service_url_var("AUTH_HOST", &projects));
        // Infix variants (e.g. _SERVICE_, _API_)
        assert!(is_inter_service_url_var("BILLING_SERVICE_URL", &projects));
        assert!(is_inter_service_url_var("BILLING_API_URL", &projects));
        assert!(is_inter_service_url_var("AUTH_SERVICE_URI", &projects));
        assert!(is_inter_service_url_var("PLATFORM_MANAGEMENT_API_URL", &projects));
        // Non-matches
        assert!(!is_inter_service_url_var("UNKNOWN_URL", &projects));
        assert!(!is_inter_service_url_var("BILLING_PORT", &projects));
    }
}