Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: allow envars to be overriden in active environments #250

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 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,25 @@ 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 overrode in an already activated system environment
func (e *Env) systemEnvOverrideOps(ops envars.Ops) envars.Ops {
if activeEnv, ok := os.LookupEnv("HERMIT_ENV"); !ok || activeEnv != e.envDir {
return envars.Ops{}
}

system := envars.Parse(os.Environ())
changed := system.Apply(e.Root(), ops).Changed(false)

var overrides envars.Ops
for envar, v_new := range changed {
if v_system, ok := system[envar]; ok && v_new != v_system {
overrides = append(overrides, &envars.Force{Name: envar, Value: v_system})
}
}

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
8 changes: 4 additions & 4 deletions it/full/spec/it_spec.sh
Original file line number Diff line number Diff line change
Expand Up @@ -318,21 +318,21 @@ Describe "Hermit"

Describe "Runtime dependencies"
. bin/activate-hermit
It "Does not install runtime-dependencies to the environment"
It "do not install to the environment"
When call hermit install testbin1
The status should be success
The stderr should be blank
The file ./bin/testbin1 should be exist
The file ./bin/testbin2 should not be exist
End

It "allows installing packages with binaries conflicting with runtime dependencies"
It "allow installing packages with conflicting binaries"
When call hermit install faketestbin2
The status should be success
The stderr should be blank
End

It "Calls the runtime dependency correctly"
It "calls the runtime dependency correctly"
When call ./bin/testbin1
The status should be success
The stdout should equal "Hello from testbin2"
Expand All @@ -344,7 +344,7 @@ Describe "Hermit"
ln -s . ./symlinked
cd ./symlinked
. bin/activate-hermit
It "Allows calling binaries in the environment"
It "allows calling binaries in the environment"
When call ./bin/testbin1
The status should be success
The stdout should not be blank
Expand Down