Skip to content

config: Start Menu launch proposes .profiles in C:\Windows\System32 instead of the per-user ~/.profiles directory #191

Closed

Description

@falbany

Summary

Launching ProFiles from the Windows Start Menu with no configuration proposes creating .profiles in C:\Windows\System32. It must propose ~/.profiles/.profiles — next to ~/.profiles/profiles.log — and a later Start-Menu launch must load that file automatically.

Problem

A Start-Menu shortcut with an empty "Start in" field runs the process with CWD = %SystemRoot%\System32. When no configuration is found:

  • the first-run config modal pre-fills its directory field with C:\Windows\System32;
  • clicking Create configuration would write C:\Windows\System32\.profiles (requires elevation, pollutes a system directory);
  • the generated starter file would also set search_dir: C:\Windows\System32, because the template substitutes the process CWD;
  • the app would scan C:\Windows\System32 on first paint.

Logs are already CWD-independent (default_log_path() returns ~/.profiles/profiles.log), but configuration is not.

There is also a latent collision on launchers whose CWD is the home folder (common on Linux .desktop entries): CWD / ".profiles" resolves to ~/.profiles, which is the log directory, not a config file. Creation then reports "already exists", and the app can treat a directory as its active config path.

Reproduction

  1. Install the Windows MSI; ensure no .profiles exists in the C:\Windows\System32 subtree.
  2. Launch ProFiles from the Start Menu.
  3. The web UI opens the Create configuration modal with C:\WINDOWS\system32 pre-filled.
  4. Creating a file there either fails (permissions) or writes C:\Windows\System32\.profiles and builds a starter whose search_dir is System32.

A simulated reproduction is enough for tests: monkeypatch.chdir() to a directory representing %SystemRoot%\System32, with Path.home() pointed at a temp folder.

Root cause

  1. resolve_config_path() (src/profiles/launch/resolution.py:57-61) searches only the CWD subtree and returns None.
  2. ProFileApp._load_configuration() calls load_config(None); load_config() builds defaults from Path.cwd() / ".profiles" (src/profiles/core/config/loader.py:43-44), so AppConfig.config_path becomes C:\Windows\System32\.profiles.
  3. BackgroundServer copies that config into ServerState; active_config_path() (src/profiles/server/state.py:59-70) and _config_response() (src/profiles/server/routes/config.py:89-136) report it as the active/expected path with config_exists: false.
  4. LoadConfigModal pre-fills its directory input from the parent of that path (src/profiles/web/src/components/LoadConfigModal.tsx:45-48).
  5. write_starter_config() substitutes Path.cwd() into STARTER_CONFIG_TEMPLATE as search_dir (src/profiles/core/actions.py:317-319).
  6. ServerState.active_config_path() can also return an existing directory, because it never checks that the fallback path is a file.

Goals

  1. A launcher-style launch with no config proposes ~/.profiles/.profiles (same folder as profiles.log).
  2. Once ~/.profiles/.profiles exists, every later launch — Start Menu included — discovers and loads it.
  3. Terminal/project launches keep the current priority: a project .profiles found in the CWD tree wins, and a missing-config proposal stays in the project folder.
  4. A generated starter scans a sensible directory (the active project, or the home folder for launcher starts) — never System32.
  5. The app never proposes or records a path that is an existing directory as its config file.

Non-goals

  • Moving or renaming the log directory.
  • Migrating existing configs or rewriting installed shortcuts.
  • Writing a config without user consent.
  • Changing --init semantics (still explicitly "generate in CWD").
  • Redesigning the config modal.

Approaches considered

Approach Verdict Why
A. Per-user config path policy + fallback discovery (chosen) Adopted Fixes every launcher (Start Menu, pinned taskbar, Finder, .desktop, future shortcuts), works for existing installs and all launch styles, and removes the home/log-dir collision.
B. Set the MSI shortcut's "Start in" to %USERPROFILE% Rejected alone Does not fix pinned taskbar, already-installed shortcuts, other launchers, or the CWD=home collision; and it would propose ~/.profiles (the log directory) as a file.
C. Always create configs in ~/.profiles Rejected Breaks the project-local workflow: a terminal launch in a project without a config should still propose project/.profiles.
D. Detect the Start Menu specifically from shortcut metadata Rejected Platform-specific and fragile; CWD is the only reliable signal the process receives.

Design

1. One user-path policy in core

New module src/profiles/core/paths.py:

  • USER_DATA_DIRNAME = ".profiles".
  • user_data_dir() -> Path → Path.home() / USER_DATA_DIRNAME — single source of truth for both logs and the per-user config.
  • user_config_path() -> Path → user_data_dir() / ".profiles".
  • find_user_config() -> Path | None — checks the canonical names in order (.profiles, .profiles.yaml, .profiles.yml) directly inside user_data_dir() (depth 0, no wildcard search).
  • is_uninformative_directory(path: Path | None) -> bool — true for:
    • None or an unresolvable path;
    • a filesystem/drive root (path.parent == path);
    • Path.home() itself;
    • anything inside Path(sys.prefix);
    • anything inside the running executable's directory (packaged app install root);
    • on Windows: anything inside %SystemRoot%/%windir% (covers System32, SysWOW64) and %ProgramFiles%/%ProgramFiles(x86)%.
  • default_config_path(start: Path | None = None) -> Path:
    • uninformative start → user_config_path();
    • informative start → start / ".profiles";
    • if that candidate exists and is a directory → user_config_path() (log-dir collision guard).
  • default_search_directory(start: Path | None = None) -> Path:
    • uninformative start → Path.home();
    • informative start → start (or Path.cwd() when omitted).

src/profiles/launch/context.py::cwd_is_uninformative() delegates to is_uninformative_directory() so launch detection and path policy cannot drift. Existing macOS behavior (/, runtime directory) is preserved; System32 and the home folder become uninformative on every platform.

default_log_path() (src/profiles/core/telemetry/diagnostics.py:266-277) delegates to user_data_dir() so config and logs cannot diverge.

2. Launch resolution: per-user config as the final fallback

resolve_config_path(ctx) becomes:

  1. --config — explicit always wins (unchanged).
  2. An OS-delivered *.profiles document (unchanged).
  3. CWD search, 5 levels — only when the CWD is informative. This deliberately skips System32 and home to avoid walking a system tree or the whole user profile looking for a config.
  4. find_user_config() under ~/.profiles when it exists.
  5. None.

ProFileApp._resolve_startup_config() additionally returns the launch-default target: default_config_path(ctx.working_directory). _load_configuration() loads resolved or launch_default, so the defaults AppConfig carries the correct target path (~/.profiles/.profiles for launcher starts) without any server-specific override. A missing default is not fatal; the existing is_missing rule still applies only to an explicitly provided path that does not exist.

load_config(None) (CLI/library entry point) applies the same policy: skip the CWD search when uninformative, then try find_user_config(), then load defaults bound to default_config_path().

3. Creation writes the right content

src/profiles/launch/creation.py::propose_creation(target, *, search_dir=None):

  • still writes target / ".profiles", skipping an existing file;
  • passes search_dir to write_starter_config(..., cwd=search_dir);
  • default search_dir:
    • target for a normal project directory;
    • default_search_directory() when target is the per-user data directory (scanning the folder that only holds logs and configs is useless).

src/profiles/server/routes/config.py:

  • POST /api/config/create: pass search_dir=state.active_directory or default_search_directory().
  • POST /api/config/template: use default_search_directory(state.active_directory) instead of Path.cwd().
  • _config_response: the empty-config active-directory fallback becomes default_search_directory() (currently Path.cwd(), which makes the first paint scan System32).

src/profiles/core/config/loader.py::propose_config_creation() (the interactive CLI helper) targets default_config_path() and uses default_search_directory() for the template.

ServerState.active_config_path(): keep the existing CWD probe, then fall back to default_config_path(); never return an existing directory as the config file.

Behaviour matrix

Launch source Project config in CWD tree ~/.profiles/.profiles Result
--config PATH any any PATH wins
Double-clicked *.profiles any any document wins
Start Menu / launcher (uninformative CWD) — (search skipped) missing defaults; proposal in ~/.profiles
Start Menu / launcher — exists user config loaded
Terminal in a project exists any project config loaded
Terminal in a project missing exists user config loaded (global default)
Terminal in a project missing missing defaults; proposal in the project folder

The global fallback (row 6) is intentional: ~/.profiles/.profiles is the user's default configuration, so behaviour does not change based on whether the app was started from a terminal or a launcher.

Implementation checklist

  • Add src/profiles/core/paths.py (+ exports where appropriate, if any).
  • Make default_log_path() delegate to user_data_dir().
  • Extend cwd_is_uninformative() via is_uninformative_directory().
  • Update launch/resolution.py (user fallback, uninformative skip, default target).
  • Update app.py startup resolution/loading to carry the launch default.
  • Update launch/creation.py to pass the starter search_dir.
  • Update core/config/loader.py (load_config, propose_config_creation).
  • Update server/state.py and server/routes/config.py (fallbacks + template search dir).
  • Update STARTER_CONFIG_TEMPLATE discovery comment.
  • Update docs: docs/configuration-profile.{en,fr}.md, docs/installation-guide.en.md (first-launch paragraph), README.md / README.fr.md config section, CHANGELOG.md.
  • Update src/profiles/core/AGENTS.md and root AGENTS.md trees for the new module.

Tests

  • New tests/core/test_paths.py: user dir/config path; uninformative None, root, home, sys.prefix, SystemRoot/ProgramFiles (monkeypatched env); informative temp dir; directory-collision guard; default_search_directory.
  • tests/launch/test_context.py: System32 and home are uninformative; existing macOS/runtime cases unchanged.
  • tests/launch/test_resolution.py: user config fallback; project config beats user config; uninformative CWD skips the tree search; explicit/document still win.
  • tests/launch/test_creation.py: starter search_dir is the target for a project dir and home for ~/.profiles.
  • tests/core/config/test_loader.py: load_config() user fallback and default path; propose_config_creation() target.
  • tests/test_app_launch_resolution.py: simulated Start-Menu CWD (System32) → AppConfig.config_path == ~/.profiles/.profiles.
  • tests/server/routes/test_config*.py: GET pre-fill path; create in the user dir; template search dir; ServerState fallback never returns a directory.
  • Web: no change required for this issue; the picker pre-fill follows config_path automatically. Optional assertion that the directory input shows ~/.profiles.

Acceptance criteria

  • With CWD = %SystemRoot%\System32 and an empty home, a simulated launch exposes ~/.profiles/.profiles as the config path, pre-fills the modal with ~/.profiles, and creating writes ~/.profiles/.profiles with search_dir pointing at the home folder — nothing is written under System32.
  • Restarting with the same CWD loads ~/.profiles/.profiles without prompting.
  • A terminal launch inside a project with a project .profiles behaves exactly as before.
  • A terminal launch in a project without a config still proposes project/.profiles.
  • No code path proposes or records an existing directory as the configuration file.
  • Existing Python and web test suites remain green.

Risks / notes

  • Path.home() can raise in exotic service environments; the logger already depends on it, so this is not a new failure mode, but the path helpers should degrade predictably.
  • The per-user config is a global fallback for terminal launches too (see matrix row 6). This is intentional; it can be revisited if it proves surprising.
  • Users who already created a config under System32 keep it in place; the app will not move or delete it. Since the fix makes System32 uninformative, the new code will not auto-load it from a Start-Menu launch.

Related

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions