Skip to content

fix(env): expand ~ relative to the home directory in resolvePath - #240

Open
shani-singh1 wants to merge 1 commit into
citrolabs:devfrom
shani-singh1:fix/env-tilde-expansion
Open

fix(env): expand ~ relative to the home directory in resolvePath#240
shani-singh1 wants to merge 1 commit into
citrolabs:devfrom
shani-singh1:fix/env-tilde-expansion

Conversation

@shani-singh1

Copy link
Copy Markdown

Summary

resolvePath("~/workspace") returns /workspace (the home directory is dropped) instead of <home>/workspace. The ~ expansion in env.ts is broken on every platform, so pointing EGO_BROWSER_AGENT_WORKSPACE at a ~-relative path resolves the agent workspace (and therefore learnings, .env loading, and agent_helpers.js) to the wrong directory.

Root cause

package/ego-browser/src/env.ts:

export function resolvePath(path) {
  if (path.startsWith("~")) {
    return resolve(process.env.HOME || process.env.USERPROFILE || ".", path.slice(1));
  }
  return resolve(path);
}

For "~/workspace", path.slice(1) is "/workspace". path.resolve(home, "/workspace") treats /workspace as an absolute path segment, so it returns /workspace and discards home entirely. This is path.resolve's documented right-to-left absolute-segment behavior.

Verified against the built runtime (HOME set to /home/ego-test):

resolvePath("~/workspace") -> /workspace        (expected /home/ego-test/workspace)

~ survives literally when EGO_BROWSER_AGENT_WORKSPACE is set in a .env file (loadEnvFile does not shell-expand), which is exactly what resolvePath exists to handle.

Fix

Join the remainder to the home directory instead of resolving it as a fresh absolute path, so ~ stays home-relative:

const home = process.env.HOME || process.env.USERPROFILE || ".";
return resolve(join(home, path.slice(1)));

join keeps the leading slash from path.slice(1) relative ("~" -> home, "~/ws" -> home/ws); the outer resolve normalizes.

Verification

  • New env.test.mjs: resolvePath("~/workspace"), "~", and "~/a/b" now resolve under the home directory; a non-tilde path still resolves to an absolute path. Fails before this change (~/workspace -> /workspace), passes after. Cross-platform (uses the current OS home/join).
  • Full npm test, tsc --noEmit, and prettier --check are clean.

Impact

  • Public helper API or behavior (resolvePath / EGO_BROWSER_AGENT_WORKSPACE with a ~ path)
  • No externally visible impact for absolute or non-tilde workspace paths

resolvePath("~/workspace") returned "/workspace": path.slice(1) of "~/workspace"
is "/workspace", and resolve(home, "/workspace") treats it as an absolute path
and drops home entirely (resolve's right-to-left absolute-segment behavior). So a
~-relative EGO_BROWSER_AGENT_WORKSPACE resolved the agent workspace (and thus
learnings, .env loading, and agent_helpers.js) to the wrong directory on every
platform. ~ survives literally when the var is set in a .env file, which
loadEnvFile reads without shell expansion.

Join the remainder onto home instead of resolving it as a fresh absolute path so
~ stays home-relative.

Adds env.test.mjs covering "~", "~/workspace", and "~/a/b" expansion plus a
non-tilde path.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant