Skip to content

[duplicate-code] Duplicate Code Pattern: WASM guard env-var lookup repeated in guard_init.go #3210

@github-actions

Description

@github-actions

Part of duplicate code analysis: #3207

Summary

In internal/server/guard_init.go, the same environment-variable lookup for the WASM guards root directory is performed twice in separate functions. Both functions independently call strings.TrimSpace(os.Getenv(wasmGuardsDirEnvVar)) and handle the empty-string case, leading to 5–8 lines of duplicated initialization code.

Duplication Details

Pattern: Repeated strings.TrimSpace(os.Getenv(wasmGuardsDirEnvVar)) lookup

  • Severity: Low

  • Occurrences: 2 independent reads of the same env var in the same file

  • Locations:

    • internal/server/guard_init.go lines 157–161 (findServerWASMGuardFile)
    • internal/server/guard_init.go lines 191–198 (logWASMGuardsDirConfiguration)
  • Code Sample:

// findServerWASMGuardFile (lines 157–161)
guardsRootDir := strings.TrimSpace(os.Getenv(wasmGuardsDirEnvVar))
if guardsRootDir == "" {
    logGuardInit.Printf("Skipping WASM guard discovery: %s is not set", wasmGuardsDirEnvVar)
    return "", false, nil
}

// logWASMGuardsDirConfiguration (lines 191–198)
guardsRootDir := strings.TrimSpace(os.Getenv(wasmGuardsDirEnvVar))
if guardsRootDir == "" {
    log.Printf("[DIFC] %s is not set", wasmGuardsDirEnvVar)
    return
}
log.Printf("[DIFC] %s=%s", wasmGuardsDirEnvVar, guardsRootDir)

Impact Analysis

  • Maintainability: Any change to how the env var is read (e.g. adding validation, switching to envutil.GetEnvString) must be applied in two places.
  • Bug Risk: Low — the two functions serve different purposes (discovery vs logging), but if one site adds extra trimming or validation the other may be missed.
  • Code Bloat: Minor (~8 lines), but the constant wasmGuardsDirEnvVar is already centralised; only the lookup idiom is duplicated.

Refactoring Recommendations

  1. Extract a small private helper getWASMGuardsRootDir() string that performs the trimmed env-var read:

    // In guard_init.go
    func getWASMGuardsRootDir() string {
        return strings.TrimSpace(os.Getenv(wasmGuardsDirEnvVar))
    }
  2. Update both call sites to use getWASMGuardsRootDir(), reducing each to a single-expression variable assignment.

  3. Estimated effort: 15 minutes.

Implementation Checklist

  • Add getWASMGuardsRootDir() helper in guard_init.go
  • Update findServerWASMGuardFile to call the helper
  • Update logWASMGuardsDirConfiguration to call the helper
  • Run make test to confirm no behaviour change

Parent Issue

See parent analysis report: #3207
Related to #3207

Generated by Duplicate Code Detector ·

  • expires on Apr 12, 2026, 6:03 AM UTC

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions