-
-
Notifications
You must be signed in to change notification settings - Fork 0
feat: Auto-fallback to user scope when not in a git repo #12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| import { join } from "node:path"; | ||
| import { existsSync } from "node:fs"; | ||
| import { dirname, join, resolve } from "node:path"; | ||
| import { homedir } from "node:os"; | ||
|
|
||
| export type Scope = "project" | "user"; | ||
|
|
@@ -47,3 +48,46 @@ export function resolveScope(scope: Scope, projectRoot?: string): ScopeRoot { | |
| skillsDir: join(agentsDir, "skills"), | ||
| }; | ||
| } | ||
|
|
||
| /** Walk up from `dir` looking for a `.git` directory. */ | ||
| export function isInsideGitRepo(dir: string): boolean { | ||
| let current = resolve(dir); | ||
| const root = dirname(current) === current ? current : undefined; | ||
|
|
||
| // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition | ||
| while (true) { | ||
| if (existsSync(join(current, ".git"))) return true; | ||
| const parent = dirname(current); | ||
| if (parent === current || parent === root) return false; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Dead
|
||
| current = parent; | ||
| } | ||
| } | ||
|
|
||
| export class ScopeError extends Error { | ||
| constructor(message: string) { | ||
| super(message); | ||
| this.name = "ScopeError"; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Resolve scope when the user did NOT pass `--user`. | ||
| * | ||
| * - If `agents.toml` exists at `projectRoot` → project scope. | ||
| * - If we're not inside a git repo → user scope (with a notice). | ||
| * - Otherwise (in a repo, no agents.toml) → throw with a helpful message. | ||
| */ | ||
| export function resolveDefaultScope(projectRoot: string): ScopeRoot { | ||
| if (existsSync(join(projectRoot, "agents.toml"))) { | ||
| return resolveScope("project", projectRoot); | ||
| } | ||
|
|
||
| if (!isInsideGitRepo(projectRoot)) { | ||
| console.error("No project found, using user scope (~/.agents/)"); | ||
| return resolveScope("user"); | ||
| } | ||
|
|
||
| throw new ScopeError( | ||
| "No agents.toml found. Run 'dotagents init' to set up this project, or use --user for user scope.", | ||
| ); | ||
| } | ||


There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Init scope resolution skips agents.toml existence check
Medium Severity
The
initcommand's scope resolution logic doesn't check foragents.tomlexistence before falling back based on git repo detection, unlikeresolveDefaultScopeused by every other command. In a non-git directory that already hasagents.toml(e.g., a Mercurial project, or a copied project), other commands correctly resolve to project scope, butdotagents init --forcewould silently target user scope instead, potentially overwriting~/.agents/agents.tomlwhile leaving the project config untouched.Additional Locations (1)
src/scope.ts#L79-L93