Skip to content

Make GLOBAL_ROOT configurable with env vars#83

Open
hannesknoll wants to merge 2 commits intoRyandonofrio3:mainfrom
hannesknoll:main
Open

Make GLOBAL_ROOT configurable with env vars#83
hannesknoll wants to merge 2 commits intoRyandonofrio3:mainfrom
hannesknoll:main

Conversation

@hannesknoll
Copy link

@hannesknoll hannesknoll commented Jan 11, 2026

Currently osgrep uses the directory ~/.osgrep for its cache. I don't like it when programs use my home dir directly for their cache.
Therefore this PR adds the OSGREP_HOME environment variable to override the global osgrep directory location
This default to the XDG compliant path ($XDG_CACHE_HOME/osgrep or ~/.cache/osgrep) instead of ~/.osgrep

The path selection priority order is

  1. $OSGREP_HOME (if set)
  2. $XDG_CACHE_HOME/osgrep (if XDG var set)
  3. ~/.cache/osgrep (default)

For testing I ran

OSGREP_HOME=/tmp/osgrep-test npx osgrep setup
ls /tmp/osgrep-test/  # models/, grammars/
# XDG fallback
XDG_CACHE_HOME=/tmp/xdg npx osgrep setup
ls /tmp/xdg/osgrep/

Note: Project-local .osgrep/ directories (for per-repo index data) remain unchanged.

Summary by CodeRabbit

  • Refactor
    • Updated how the application determines storage paths for data, grammars, and models. Now respects OSGREP_HOME environment variable, with fallback to XDG_CACHE_HOME or ~/.cache directory for configuration storage.

✏️ Tip: You can customize this high-level summary in your review settings.

Support configurable global home directory via:
1. $OSGREP_HOME env var (highest priority)
2. $XDG_CACHE_HOME/osgrep (if XDG var set)
3. ~/.cache/osgrep (default fallback)

Replaces hardcoded ~/.osgrep for global resources (models, grammars).
- model-loader.ts: use PATHS.models instead of hardcoded path
- download-worker.ts: use PATHS.models instead of hardcoded path
- grammar-loader.ts: use PATHS.grammars instead of hardcoded path
- index.ts: use PATHS.globalRoot for legacy data detection
@coderabbitai
Copy link

coderabbitai bot commented Jan 11, 2026

Walkthrough

This pull request consolidates path computation logic into a centralized configuration module. A new getOsgrepHome() function in config.ts respects the OSGREP_HOME environment variable, falling back to XDG_CACHE_HOME or ~/.cache, then appends "osgrep". Multiple modules are updated to import and use PATHS from config instead of independently computing paths via os.homedir().

Changes

Cohort / File(s) Summary
Path Configuration
src/config.ts
Introduces getOsgrepHome() internal function that checks OSGREP_HOME, then XDG_CACHE_HOME (or ~/.cache), appending "osgrep". Reassigns GLOBAL_ROOT to use this function. No public API signature changes.
Legacy Data Path Migration
src/index.ts
Adds import of PATHS from config; switches legacy data path from hardcoded home directory to PATHS.globalRoot/data. Updates log message references.
Grammars Directory Consolidation
src/lib/index/grammar-loader.ts
Removes os.homedir() dependency; updates exported GRAMMARS_DIR constant to use PATHS.grammars instead.
Model Path Resolution
src/lib/setup/model-loader.ts
Removes CACHE_DIR constant and direct OS-homedir caching logic. Updates areModelsDownloaded() to resolve model paths via PATHS.models.
Download Worker Paths
src/lib/workers/download-worker.ts
Replaces hard-coded path and CACHE_DIR references with PATHS.models for both env.cacheDir and destination directory construction in downloadExtraFile(). Removes node:os dependency.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

🐰 A hop and a skip, paths consolidated with care,
No more scattered homedir sprawl in the air,
Environment variables guide the way,
PATHS now central, leading astray never, hooray! ✨

🚥 Pre-merge checks | ✅ 2 | ❌ 1
❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 66.67% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately summarizes the main objective of the pull request—making GLOBAL_ROOT configurable via environment variables (OSGREP_HOME, XDG_CACHE_HOME).

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (1)
src/config.ts (1)

49-57: LGTM! Well-implemented priority system.

The getOsgrepHome() function correctly implements the documented priority:

  1. OSGREP_HOME (if set, resolved to absolute path)
  2. XDG_CACHE_HOME/osgrep (if XDG var set)
  3. ~/.cache/osgrep (default)

The use of path.resolve() for OSGREP_HOME properly handles relative paths.

📝 Optional: Add inline comment for clarity

Consider adding a brief comment above the function to document the priority order:

+// Priority: $OSGREP_HOME → $XDG_CACHE_HOME/osgrep → ~/.cache/osgrep
 function getOsgrepHome(): string {
   if (process.env.OSGREP_HOME) {
     return path.resolve(process.env.OSGREP_HOME);
   }
   // XDG compliance: $XDG_CACHE_HOME/osgrep or ~/.cache/osgrep
   const xdgCache =
     process.env.XDG_CACHE_HOME || path.join(os.homedir(), ".cache");
   return path.join(xdgCache, "osgrep");
 }
📜 Review details

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Cache: Disabled due to data retention organization setting

Knowledge base: Disabled due to data retention organization setting

📥 Commits

Reviewing files that changed from the base of the PR and between 51906e2 and 051c7c1.

📒 Files selected for processing (5)
  • src/config.ts
  • src/index.ts
  • src/lib/index/grammar-loader.ts
  • src/lib/setup/model-loader.ts
  • src/lib/workers/download-worker.ts
🧰 Additional context used
🧬 Code graph analysis (4)
src/lib/workers/download-worker.ts (1)
src/config.ts (1)
  • PATHS (61-65)
src/lib/index/grammar-loader.ts (1)
src/config.ts (1)
  • PATHS (61-65)
src/index.ts (1)
src/config.ts (1)
  • PATHS (61-65)
src/lib/setup/model-loader.ts (1)
src/config.ts (2)
  • PATHS (61-65)
  • MODEL_IDS (4-7)
🔇 Additional comments (8)
src/index.ts (2)

19-19: LGTM! Centralized path configuration.

The import of PATHS aligns with the PR's goal of consolidating path resolution into a single, configurable source.


35-45: LGTM! Proper legacy migration handling.

The legacy data path check correctly uses PATHS.globalRoot and provides clear guidance to users migrating from the old global database structure to the new per-project approach.

src/lib/setup/model-loader.ts (2)

4-4: LGTM! Consistent with centralized configuration.

Adding PATHS to the imports aligns with the PR's refactoring to use centralized path management.


59-65: LGTM! Correctly migrated to PATHS.models.

The function now uses PATHS.models instead of the hardcoded cache directory, maintaining the same logic while adopting the configurable path system.

src/lib/index/grammar-loader.ts (1)

3-6: LGTM! Clean migration to centralized paths.

The grammar directory now uses PATHS.grammars, eliminating the hardcoded dependency on os.homedir() and adopting the configurable path system introduced in this PR.

src/config.ts (1)

61-65: LGTM! Clean PATHS API.

The PATHS object provides a clean, centralized API for all global path references. The structure (globalRoot, models, grammars) is intuitive and well-organized.

src/lib/workers/download-worker.ts (2)

5-8: LGTM! Correct migration to PATHS.

The worker now uses PATHS.models for the cache directory, eliminating the hardcoded dependency on os.homedir() and adopting the configurable path system.


65-69: LGTM! Consistent path construction.

The destination directory correctly uses PATHS.models as the base for model storage. The comment on line 67 provides a helpful example of the resulting path structure.

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