Skip to content

Commit

Permalink
Handle setting eg. PATH in devenv better
Browse files Browse the repository at this point in the history
Fixes #841
  • Loading branch information
mxcl committed Nov 18, 2023
1 parent 16c67d7 commit 45b3763
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 8 deletions.
9 changes: 8 additions & 1 deletion src/modes/internal.activate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { fixturesd, null_logger as logger } from "../utils/test-utils.ts"
import { _internals as _devenv_internals } from "../utils/devenv.ts"
import specimen0, { _internals } from "./internal.activate.ts"
import { assertRejects } from "deno/assert/mod.ts"
import { assertEquals, assertRejects } from "deno/assert/mod.ts"
import * as mock from "deno/testing/mock.ts"
import { SemVer, Path, utils, semver } from "pkgx"

Expand Down Expand Up @@ -66,4 +66,11 @@ Deno.test("internal.activate.ts", async runner => {
await runner.step("coverage++", () => {
_internals.datadir()
})

await runner.step("apply_userenv", () => {
const userenv = { PATH: "/foo/bar:$PATH" }
const env = { PATH: "/baz:$PATH"}
_internals.apply_userenv(env, userenv)
assertEquals(env.PATH, "/foo/bar:/baz:$PATH")
})
})
26 changes: 19 additions & 7 deletions src/modes/internal.activate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import devenv from "../utils/devenv.ts"
import undent from "outdent"

export default async function(dir: Path, { powder, ...opts }: { powder: PackageRequirement[], logger: Logger }) {
const { install, construct_env, datadir, getenv } = _internals
const { install, construct_env, datadir, getenv, apply_userenv } = _internals

if (!dir.isDirectory()) {
throw new PkgxError(`not a directory: ${dir}`)
Expand Down Expand Up @@ -37,13 +37,11 @@ export default async function(dir: Path, { powder, ...opts }: { powder: PackageR
return installed.filter(x => set.has(x.project))
})(installations.pkgenv)

let rv = ''

/// env specified in devenv files takes precedence
Object.assign(env, userenv)
// substitute or replace calculated env with user-supplied env from the keyfiles
apply_userenv(env, userenv)

let rv = ''
for (const [key, value] of Object.entries(env)) {

const existing_value = getenv(key)
if (value == existing_value) {
delete env[key]
Expand Down Expand Up @@ -118,9 +116,23 @@ export default async function(dir: Path, { powder, ...opts }: { powder: PackageR
return [rv, rv_pkgenv] as [string, PackageRequirement[]]
}

function apply_userenv(env: Record<string, string>, userenv: Record<string, string>) {
for (const [key, value] of Object.entries(userenv)) {
if (!(key in env) && !value.includes(`$${key}`) && !value.includes(`\${${key}}`)) {
/// user supplied env completely overrides this key or the key is empty
env[key] = value
} else {
env[key] = value
.replaceAll(`$${key}`, env[key])
.replaceAll(`\${${key}}`, env[key])
}
}
}

export const _internals = {
install,
construct_env,
datadir: () => hooks.useConfig().data,
getenv: Deno.env.get
getenv: Deno.env.get,
apply_userenv
}

0 comments on commit 45b3763

Please sign in to comment.