Skip to content

Commit

Permalink
fix: allow envars to be overriden in active environments
Browse files Browse the repository at this point in the history
  • Loading branch information
alecthomas authored and quad committed Jul 3, 2023
1 parent 5d83c83 commit 7bc0406
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
18 changes: 18 additions & 0 deletions env.go
Original file line number Diff line number Diff line change
Expand Up @@ -748,6 +748,7 @@ func (e *Env) Exec(l *ui.UI, pkg *manifest.Package, binary string, args []string
return errors.WithStack(err)
}
ops := e.allEnvarOpsForPackages(runtimeDeps, pkg, installed...)
ops = append(ops, e.systemEnvOverrideOps(ops)...)
packageHermitBin, err := e.getPackageRuntimeEnvops(pkg)
if err != nil {
return errors.WithStack(err)
Expand Down Expand Up @@ -777,6 +778,23 @@ func (e *Env) Exec(l *ui.UI, pkg *manifest.Package, binary string, args []string
return errors.Errorf("%s: could not find binary %q", pkg, binary)
}

// systemEnvOverrideOps returns environment variables defined in the system environment that were previously overriden by Hermit
func (e *Env) systemEnvOverrideOps(ops envars.Ops) envars.Ops {
if activeEnv, ok := os.LookupEnv("HERMIT_ENV"); !ok || activeEnv != e.envDir {
return envars.Ops{}
}

var overrides envars.Ops
for _, op := range ops {
envar := op.Envar()
if v, ok := os.LookupEnv(envar); ok {
overrides = append(overrides, &envars.Force{Name: envar, Value: v})
}
}

return overrides
}

func (e *Env) getPackageRuntimeEnvops(pkg *manifest.Package) (envars.Op, error) {
// If the package contains a Hermit env, add that to the PATH for runtime dependencies
pkgEnv, err := OpenEnv(pkg.Root, e.state, e.packageSource, nil, e.httpClient, e.scriptSums)
Expand Down
10 changes: 10 additions & 0 deletions integration/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,16 @@ func TestIntegration(t *testing.T) {
assert test "$(testbin1.sh)" = "FOO=runtimefoo"
assert test "$(testbin2.sh)" = "BAR=hermitbar"
`},
{name: "SystemEnvOverridesAlreadyAcitvatedHermitEnv",
preparations: prep{fixture("testenv4"), activate(".")},
script: `
hermit install testbin1
hermit install testbin2
export FOO=systemfoo
assert test "$(testbin1.sh)" = "FOO=systemfoo"
export BAR=systembar
assert test "$(testbin2.sh)" = "BAR=systembar"
`},
}

checkForShells(t)
Expand Down

0 comments on commit 7bc0406

Please sign in to comment.