qwen-code/docs
tanzhenxin 78ad595581
feat(core): support QWEN_HOME env var to customize config directory (#2953)
* feat(core): support QWEN_CONFIG_DIR env var to customize config directory

Allow users to override the default ~/.qwen config directory location
via the QWEN_CONFIG_DIR environment variable. This enables users on dev
machines with external disk mounts or custom home directory layouts to
persist config at a location of their choosing.

Changes:
- Add QWEN_CONFIG_DIR check to Storage.getGlobalQwenDir() (absolute and
  relative path support)
- Eliminate 11 redundant '.qwen' constant definitions across packages
- Replace 16+ direct os.homedir() + '.qwen' path constructions with
  Storage.getGlobalQwenDir() calls
- Inline env var checks for packages that cannot import from core
  (channels, vscode-ide-companion, standalone scripts)
- Add unit tests for the new env var behavior
- Project-level .qwen/ directories are NOT affected

Closes #2951

* fix(core): use path.resolve/join in QWEN_CONFIG_DIR tests for Windows compat

Hardcoded Unix paths like '/tmp/custom-qwen/settings.json' fail on
Windows where path APIs produce backslash separators. Use path.resolve()
for inputs and path.join() for assertions so the tests pass cross-platform.

* test(cli): remove flaky 'should keep restart prompt when switching scopes' test

Timing-sensitive UI test that fails intermittently on Windows CI due to
async ANSI output not settling within the wait window.

* feat(core): route remaining hardcoded ~/.qwen/ paths through Storage.getGlobalQwenDir()

Update channel status, memory command, extension storage, skills
discovery, and memory discovery to use Storage.getGlobalQwenDir()
instead of hardcoded os.homedir()/.qwen paths, ensuring QWEN_CONFIG_DIR
env var is respected throughout the codebase.

* fix(tests): mock os.homedir before makeFakeConfig for Storage.getGlobalQwenDir

Storage.getGlobalQwenDir() is now called during Config construction,
which requires os.homedir() to be mocked before makeFakeConfig() is
called. Also mock Storage.getGlobalQwenDir in memoryCommand tests
since it uses a cross-package import that vi.spyOn doesn't intercept.

* fix(core): respect QWEN_CONFIG_DIR for .env discovery and install source

findEnvFile() walk-up would find legacy ~/.qwen/.env before checking
QWEN_CONFIG_DIR/.env when the workspace was under $HOME. Skip the
legacy path when a custom config dir is set so the fallback picks up
the correct file.

Also add a legacy fallback in readSourceInfo() since the installer
always writes source.json to ~/.qwen/ regardless of QWEN_CONFIG_DIR.

* refactor(core): rename QWEN_CONFIG_DIR to QWEN_HOME and fix runtime path resolution

Rename the env var before it ships (zero existing users) to match the
convention of CARGO_HOME, GRADLE_USER_HOME, etc. — "HOME" means "root of
all tool state", not just config.

Key changes:
- Rename QWEN_CONFIG_DIR → QWEN_HOME across all packages and scripts
- Add shared path utils in vscode-ide-companion and channels/base to
  eliminate scattered inline env var resolution
- Fix runtime path mismatch: IDE lock files and session paths in the
  vscode extension now route through getRuntimeBaseDir() (checking
  QWEN_RUNTIME_DIR first), matching core Storage behavior
- Fix telemetry_utils.js otel path to check QWEN_RUNTIME_DIR for tmp/
- Add E2E integration tests for QWEN_HOME scenarios

* fix(core): address critical review issues for QWEN_HOME support

Pass resolved QWEN_HOME as a dedicated QWEN_DIR sandbox parameter so
macOS Seatbelt profiles allow writes to custom config directories.
Fix hookRunner treating signal-killed hooks as success by using ?? -1
instead of || 0. Add QWEN_HOME and QWEN_RUNTIME_DIR to the env vars
documentation table.

* fix(sandbox): whitelist QWEN_RUNTIME_DIR in macOS Seatbelt profiles

When QWEN_RUNTIME_DIR is set separately from QWEN_HOME, the sandbox
was blocking writes to the runtime directory (debug logs, chat history,
IDE locks, sessions). Pass RUNTIME_DIR as a sandbox parameter and add
the corresponding subpath rule to all six .sb profiles.

* fix(core): add tilde expansion to QWEN_HOME and align satellite path helpers

- Extract resolvePath() from resolveRuntimeBaseDir() so QWEN_HOME gets
  the same ~/tilde expansion that QWEN_RUNTIME_DIR already had.
- Port resolvePath() to vscode-ide-companion and channels/base mirrors,
  fixing tilde handling in getRuntimeBaseDir() for the IDE companion.
- Add missing os.tmpdir() fallback in channels/base getGlobalQwenDir().
- Add unit tests for tilde expansion in QWEN_HOME.
- Clarify prompts.ts comment that system.md default is global, not
  project-level.

* fix(core): add tilde expansion to scripts and fix extension cache QWEN_HOME support

Add resolvePath() helper to standalone JS scripts (sandbox_command.js,
telemetry.js, telemetry_utils.js) so QWEN_HOME=~/custom expands
consistently with core Storage.resolvePath().

Fix ExtensionManager.refreshCache() to use ExtensionStorage.getUserExtensionsDir()
instead of hardcoded os.homedir(), so extensions installed under a custom
QWEN_HOME are discoverable.

* test: remove flaky InputPrompt tab-suggestion test on Windows

* test: remove flaky tests that fail intermittently on Windows

Removes 'does not accept the prompt suggestion on shift+tab' from
InputPrompt.test.tsx and 'should keep restart prompt when switching
scopes' from SettingsDialog.test.tsx. Both have been observed to fail
intermittently on the Windows CI workers; the underlying behaviors are
covered by adjacent assertions and end-to-end tests.

* revert(core): keep system.md path project-local under .qwen/

The QWEN_HOME refactor incorrectly routed the QWEN_SYSTEM_MD default path
through Storage.getGlobalQwenDir() (i.e. ~/.qwen/system.md or
$QWEN_HOME/system.md). The original semantics — inherited from the
upstream Gemini-CLI sync — are project-local: <cwd>/.qwen/system.md.

System-prompt customization is intentionally per-project so that each
repository can ship its own override without global side effects. Users
who want a global override can still set QWEN_SYSTEM_MD to an absolute
path. This revert keeps that behavior intact while leaving the rest of
the QWEN_HOME plumbing (settings, credentials, extensions, skills, memory)
unchanged.

* refactor(core): unify QWEN_CONFIG_DIR into the canonical QWEN_DIR

Three definitions of the literal '.qwen' string existed across the
codebase:

- QWEN_DIR in config/storage.ts (canonical, used by the Storage class)
- QWEN_CONFIG_DIR in memory/const.ts
- QWEN_CONFIG_DIR in tools/memory-config.ts (a near-clone of the above)

The QWEN_CONFIG_DIR name also collided with a former env-var name (now
renamed to QWEN_HOME on this branch), making it ambiguous whether call
sites referred to a configurable env var or a hardcoded directory name.

Drop the duplicates and route the only call sites (prompts.ts and its
test) through QWEN_DIR from config/storage.ts. The mock factory in
config.test.ts is updated to no longer expose the removed export.

* fix(integration-tests): use 'extensions list' to trigger settings migration

Tests 2b and 3a in cli/qwen-config-dir.test.ts relied on running
\`qwen --help\` to invoke loadSettings() (and thus the V1→V3 settings
migration). That worked when loadSettings() ran before parseArguments()
in the CLI startup sequence. Main has since flipped the order:
parseArguments() runs first, and yargs intercepts --help and exits the
process before loadSettings() is reached, so migration never runs and
the tests' migration probe always reads back V1.

Switch to \`qwen extensions list\` instead. It is a yargs subcommand that
runs through main() to loadSettings() without requiring an API key, so
migration runs as expected. Update the inline comments to document why
--help cannot be used and why this command works.

* fix(memory): route auto-memory base dir through Storage.getGlobalQwenDir()

The auto-memory subsystem (introduced on main in #3087) computed its base
directory by hardcoding path.join(os.homedir(), QWEN_DIR). That bypassed
QWEN_HOME entirely, so global auto-memory artifacts always landed in
~/.qwen/projects/... regardless of the user's configured QWEN_HOME path.

Route the default through Storage.getGlobalQwenDir() so QWEN_HOME is
honored. The QWEN_CODE_MEMORY_BASE_DIR test override stays as the
highest-priority short-circuit.

Discovered while running the QWEN_HOME e2e test plan against the merged
branch — Group B test B3 (memory tool writes to QWEN_HOME) was the only
failing scenario across A/B/C/D groups.

* fix(cli): treat custom QWEN_HOME .env as user-level

When QWEN_HOME points to a directory whose path does not contain
`.qwen` (e.g., `/tmp/qwen-home`), the global `.env` was misclassified
as a project-level env file. As a result, default-excluded variables
such as `DEBUG` and `DEBUG_MODE` were silently dropped even though
they came from the user-level config directory.

The classification now reuses the same user-level path set computed
by `findEnvFile`, so any `.env` inside the resolved global Qwen
directory (or directly under `~/`) is recognized as user-level.

Also drop the misleading "does not expand `~`" note from the
QWEN_HOME documentation — `Storage.getGlobalQwenDir` does expand
leading tildes via `Storage.resolvePath`.

* fix(cli): drop legacy .qwen substring check from env-file classification

The user-level env-file detection now keys solely off the precomputed
user-level path set, which already covers ~/.env and ${QWEN_HOME}/.env.
The legacy substring fallback misclassified <repo>/.qwen/.env as
user-level, so excludedEnvVars no longer applied to it.

* fix(core): align plain-text hook output with documented exit-code semantics

Per docs/users/features/hooks.md, only exit code 2 is a blocking error;
all other non-zero exit codes are non-blocking and execution should
continue. The plain-text branch in convertPlainTextToHookOutput
previously denied on every non-zero, non-1 exit code (3, 127, signal
fallbacks), contradicting the documented behavior.

Collapse all non-blocking non-zero codes to EXIT_CODE_NON_BLOCKING_ERROR
before passing into the converter so they take the warning path
consistently.

* chore: trigger CI

* fix(cli): pass QWEN_HOME and QWEN_RUNTIME_DIR into docker/podman sandbox

The container CLI previously had no awareness of the host's QWEN_HOME or
QWEN_RUNTIME_DIR values. The global qwen dir worked only because the
mount target happens to match the default fallback inside the sandbox,
and the runtime base dir was lost entirely when it diverged from the
global qwen dir.

* fix(cli): canonicalize sandbox QWEN/RUNTIME paths and pin IDE lock dir

Two reviewer-flagged issues from PR #2953:

* macOS Seatbelt was passed `path.resolve` for `QWEN_DIR`/`RUNTIME_DIR`
  while neighbouring directories used `fs.realpathSync`. With a symlinked
  `QWEN_HOME` or `QWEN_RUNTIME_DIR`, sandbox-exec would compare against
  the canonical kernel path and deny writes. Create the dirs (so
  `realpathSync` can succeed on first run) then canonicalize them like
  the surrounding entries.

* The VS Code companion wrote IDE lock files via the runtime base dir
  while the CLI side resolves the runtime dir from settings too. That
  divergence silently desynced lock-file discovery whenever a user set
  `advanced.runtimeOutputDir` without `QWEN_RUNTIME_DIR`. Anchor both
  sides to `getGlobalQwenDir()` since the companion process can only
  see env vars, not CLI settings.

* fix(cli): finish QWEN_HOME plumbing across env, memory, rules, sandbox

Codex review surfaced four user-visible spots where QWEN_HOME wasn't
threaded through:

* `findEnvFile` walked through the user home dir before consulting the
  QWEN_HOME fallback, so `~/.env` shadowed `<QWEN_HOME>/.env` and
  reversed the qwen-specific precedence the default `~/.qwen/.env` path
  enjoys. Add a home-dir-step check that prefers the custom Qwen dir
  when set.

* `MemoryDialog` displayed and edited `~/.qwen/QWEN.md` regardless of
  QWEN_HOME. Memory discovery already routes through Storage, so user
  edits via the dialog were silently ignored at runtime. Route the
  dialog through `Storage.getGlobalQwenDir()` to match.

* `loadRules` looked up global rules at `~/.qwen/rules/`, ignoring
  QWEN_HOME entirely. Use the global Qwen dir like the rest of the
  config surfaces.

* The Docker/Podman sandbox path called `mkdirSync(userSettingsDir)`
  without `recursive`. Pre-PR the dir was always `~/.qwen` and the
  parent existed; with a nested QWEN_HOME like `/tmp/qwen/config` the
  first run threw ENOENT before the mount could be added.

* fix(cli): block project .env from redirecting QWEN_HOME and QWEN_RUNTIME_DIR

A project `.env` could set QWEN_HOME after settings were already loaded
from the real home, splitting global state: settings.json read from
~/.qwen but later writes (installation_id, OAuth credentials, MCP tokens)
landed in the project-controlled directory. The user-configurable
excludedEnvVars list isn't the right place for this — it's a correctness
boundary, not a preference — so always exclude these two vars from
project .env files. User-level .env files (~/.qwen/.env) are unaffected.

* fix(cli): keep workspace .qwen/.env unfiltered and pre-resolve user QWEN_HOME

The env-file classification conflated two concerns: which paths may
override global state vars, and which paths are exempt from the
user-configurable excludedEnvVars filter. Splitting them lets a
workspace `<repo>/.qwen/.env` carry DEBUG/DEBUG_MODE per the documented
contract while still being blocked from redirecting QWEN_HOME or
QWEN_RUNTIME_DIR.

A QWEN_HOME set in `~/.qwen/.env` or `~/.env` would also previously
arrive too late: USER_SETTINGS_PATH was captured at module load and
loadSettings migrated `~/.qwen/settings.json` before loadEnvironment
applied the override, leaving credentials, MCP tokens, and
installation_id pointed at the new directory while settings stayed at
the legacy one. A pre-pass now reads those user-level files for the
two storage-controlling vars before any user settings are loaded, and
the user settings path is re-resolved locally so all global state lands
in one place.

* fix(cli): make user-settings paths lazy to pick up bootstrapped QWEN_HOME

USER_SETTINGS_PATH/USER_SETTINGS_DIR in settings.ts and the duplicate
USER_SETTINGS_DIR in trustedFolders.ts were top-level consts evaluated
at module load — before preResolveHomeEnvOverrides() reads QWEN_HOME
from ~/.env or ~/.qwen/.env. Callers (sandbox launcher, trusted-folders
reader) saw the legacy ~/.qwen path while the main CLI had moved to the
custom home, splitting state.

Convert all three to lazy getter functions and add a regression test
that pokes process.env.QWEN_HOME after import and asserts each getter
reflects it — any future top-level capture turns the test red.

Mirror the same ~/.env / ~/.qwen/.env bootstrap into
scripts/sandbox_command.js, which previously only read process.env
directly and could disagree with the main CLI on the sandbox setting.

Addresses review threads #3159793469, #3177804507, and item #2 of the
2026-05-06 review summary.

* fix(cli): address qwen home review follow-ups

* test(cli): normalize path in QWEN_HOME freshness assertion for Windows

`getUserSettingsDir()` returns `path.dirname(...)`, which on Windows uses
backslash separators. The bare string comparison failed on Windows runners
("\tmp\qwen-lazy-test" vs "/tmp/qwen-lazy-test"). Wrap the expected value
in `path.normalize()` to match the OS-native separator, mirroring the two
sibling assertions that already use `path.join()`.

* fix(cli): close storage-routing leaks via settings.env and project sandbox .env

settings.env (merged) was being applied to process.env without filtering, so
a workspace settings.json could redirect global state by setting
env.QWEN_HOME or env.QWEN_RUNTIME_DIR after the home-scoped .env bootstrap
ran. Apply PROJECT_ENV_HARDCODED_EXCLUSIONS to the settings.env path too.

scripts/sandbox_command.js's project-walk fallback called dotenv.config() to
find QWEN_SANDBOX, which injected every parsed key — including QWEN_HOME /
QWEN_RUNTIME_DIR the main CLI hard-blocks. Replace with a manual parse that
copies only QWEN_SANDBOX.

Add a startup migration warning when QWEN_HOME points to a directory with
no settings.json while ~/.qwen/settings.json exists, so users notice that
their existing OAuth tokens / settings / memory aren't auto-migrated.

* test: cover QWEN_HOME / QWEN_RUNTIME_DIR in duplicated path helpers

Adds targeted unit tests for the two TypeScript mirrors of
Storage.getGlobalQwenDir() / getRuntimeBaseDir() that live outside
packages/core to avoid cross-package imports. Covers default, absolute,
relative, ~/x, ~\x, and bare ~ inputs, plus the runtime/home priority
chain in the IDE companion.

* fix: bootstrap QWEN_HOME before yargs handlers and in VS Code companion

Two storage-routing leaks surfaced by Codex review of feat/qwen-config-dir:

- channel status/stop call readServiceInfo() inside yargs handlers that
  process.exit before loadSettings() runs, so QWEN_HOME defined only in
  ~/.qwen/.env or ~/.env never resolved for them. The same race exists
  for the duplicate-instance check at the top of channel start. Hoist
  preResolveHomeEnvOverrides() to the top of main() so all subcommand
  handlers see the bootstrapped env vars.

- The VS Code companion's getGlobalQwenDir / getRuntimeBaseDir read
  process.env directly, missing the same .env pre-pass. If a user only
  configures QWEN_HOME via ~/.qwen/.env, the CLI looks under the
  redirected dir while the companion writes IDE lock files under
  ~/.qwen, breaking IDE discovery. Mirror the CLI pre-pass in the
  companion (lazy, idempotent) without importing from core.

* fix(config): preserve credentials in legacy ~/.qwen/.env when QWEN_HOME redirects

When QWEN_HOME is bootstrapped from `~/.qwen/.env`, the home-dir env walk
previously skipped that file and never read `<QWEN_HOME>/.env` from the
companion. This stranded non-routing credentials (e.g. OPENAI_API_KEY)
left in `~/.qwen/.env` and let the companion write IDE lock files into a
different runtime dir than the CLI was reading from.

- CLI: fall back to `~/.qwen/.env` after `<QWEN_HOME>/.env` at both the
  home-dir step and the post-walk fallback in findEnvFile, and treat the
  legacy path as user-level for trust and exclusion semantics.
- Companion: after the initial candidate pass discovers QWEN_HOME, also
  read `<QWEN_HOME>/.env` so QWEN_RUNTIME_DIR sourced from there matches
  what the CLI's findEnvFile would pick.

* refactor(cli): simplify QWEN_HOME plumbing — dedupe helpers, latch, comments

- replace local isSameOrChildPath with core's isSubpath in sandbox.ts
- latch preResolveHomeEnvOverrides so it runs once per process
- pass userLevelPaths from loadEnvironment into findEnvFile (no recompute)
- collapse findEnvFile's home-dir branch and post-loop fallback into one
  shared candidate list (drops duplicate existsSync calls)
- factor extensionManager's user-extensions loop into a private helper
- use QWEN_DIR constant instead of '.qwen' literal in skill-manager
- trim narrative / PR-history comments across changed files

* fix(cli): align QWEN_HOME .env bootstrap across CLI, sandbox, telemetry

Telemetry scripts previously read process.env.QWEN_HOME directly, so a
QWEN_HOME set only in ~/.env or ~/.qwen/.env left telemetry writing to
~/.qwen while the CLI routed elsewhere. Extract the bootstrap into
scripts/lib/qwen-home-bootstrap.js and have sandbox_command.js,
telemetry.js, and telemetry_utils.js share it.

Also add a third-pass <new QWEN_HOME>/.env read in
preResolveHomeEnvOverrides so the CLI and VS Code companion agree on
QWEN_RUNTIME_DIR when it is configured under the new home dir.

* test(integration-tests): update QWEN_HOME assertions for v4 schema

Settings schema was bumped to v4 on main (gitCoAuthor migration). The
qwen-config-dir tests still asserted post-migration $version === 3, so
they failed after the merge. Bump the assertions to 4 and the seed in
3a to match, and point a comment at SETTINGS_VERSION so the next bump
is easy to find.
2026-05-09 15:51:52 +08:00
..
design feat(cli): improve slash command discovery (#3736) 2026-05-09 14:25:44 +08:00
developers feat(telemetry): add sensitive span attribute opt-in (#3893) 2026-05-08 00:36:08 +08:00
plans feat(vscode-ide-companion): add agent execution tool display (#2590) 2026-04-18 23:39:26 +08:00
users feat(core): support QWEN_HOME env var to customize config directory (#2953) 2026-05-09 15:51:52 +08:00
_meta.ts feat: refactor docs 2025-12-05 10:51:57 +08:00
index.md fix: lint issues 2025-12-19 15:52:11 +08:00