From 4bae68cbe5caedeb2a6763ed012e531bf0b72627 Mon Sep 17 00:00:00 2001 From: Matt Van Horn Date: Sun, 8 Mar 2026 21:46:05 -0700 Subject: [PATCH 1/3] fix: resolve repo root for initialization when started from subdirectory When `entire enable` is run from a subdirectory, change to the repo root before proceeding. This ensures agent hooks (e.g., .gemini/settings.json, .claude/settings.json) are created at the correct location and can be found by the agent regardless of CWD. Prints a note when operating from a subdirectory so the user knows the repo root is being used. Fixes #559 Co-Authored-By: Claude Opus 4.6 --- cmd/entire/cli/setup.go | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/cmd/entire/cli/setup.go b/cmd/entire/cli/setup.go index 13ae0f279..ab6f3d7ff 100644 --- a/cmd/entire/cli/setup.go +++ b/cmd/entire/cli/setup.go @@ -382,11 +382,24 @@ If Entire is already configured but disabled, this re-enables it.`, // Check if we're in a git repository first - this is a prerequisite error, // not a usage error, so we silence Cobra's output and use SilentError // to prevent duplicate error output in main.go - if _, err := paths.WorktreeRoot(ctx); err != nil { + repoRoot, err := paths.WorktreeRoot(ctx) + if err != nil { fmt.Fprintln(cmd.ErrOrStderr(), "Not a git repository. Please run 'entire enable' from within a git repository.") return NewSilentError(errors.New("not a git repository")) } + // Change to repo root so all paths resolve correctly. + // This ensures agent hooks (e.g., .gemini/settings.json, .claude/settings.json) + // are created at the repo root even when running from a subdirectory. + cwd, _ := os.Getwd() //nolint:forbidigo // Need CWD to detect subdirectory + if cwd != repoRoot { + if err := os.Chdir(repoRoot); err != nil { //nolint:forbidigo // Intentional chdir to repo root for correct path resolution + return fmt.Errorf("failed to change to repository root %s: %w", repoRoot, err) + } + paths.ClearWorktreeRootCache() + fmt.Fprintf(cmd.ErrOrStderr(), "Note: Running from repository root (%s)\n\n", repoRoot) + } + if err := validateSetupFlags(opts.UseLocalSettings, opts.UseProjectSettings); err != nil { return err } From 90a8a85940a8f3c77ea9864a0fe633b0654774a0 Mon Sep 17 00:00:00 2001 From: Peyton Montei Date: Fri, 20 Mar 2026 17:28:08 -0700 Subject: [PATCH 2/3] fix: handle os.Getwd error in subdirectory detection Check the error return from os.Getwd instead of discarding it, fixing errcheck lint failure. If Getwd fails, skip the subdirectory check and proceed with enable at the current directory. Co-Authored-By: Claude Opus 4.6 (1M context) --- cmd/entire/cli/setup.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/cmd/entire/cli/setup.go b/cmd/entire/cli/setup.go index ab6f3d7ff..96432c3b3 100644 --- a/cmd/entire/cli/setup.go +++ b/cmd/entire/cli/setup.go @@ -391,9 +391,9 @@ If Entire is already configured but disabled, this re-enables it.`, // Change to repo root so all paths resolve correctly. // This ensures agent hooks (e.g., .gemini/settings.json, .claude/settings.json) // are created at the repo root even when running from a subdirectory. - cwd, _ := os.Getwd() //nolint:forbidigo // Need CWD to detect subdirectory - if cwd != repoRoot { - if err := os.Chdir(repoRoot); err != nil { //nolint:forbidigo // Intentional chdir to repo root for correct path resolution + cwd, err := os.Getwd() //nolint:forbidigo // Need CWD to detect subdirectory + if err == nil && cwd != repoRoot { + if err := os.Chdir(repoRoot); err != nil { return fmt.Errorf("failed to change to repository root %s: %w", repoRoot, err) } paths.ClearWorktreeRootCache() From f6b964a3c22995ab914693c8cb560dfcfced144f Mon Sep 17 00:00:00 2001 From: Matt Van Horn <455140+mvanhorn@users.noreply.github.com> Date: Tue, 24 Mar 2026 14:24:44 -0700 Subject: [PATCH 3/3] fix: replace os.Chdir with scoped path resolution Remove os.Chdir(repoRoot) call that mutated global process state. All downstream path resolution already uses paths.WorktreeRoot(ctx) which correctly resolves the repo root from any subdirectory via git rev-parse --show-toplevel, making the Chdir unnecessary. Co-Authored-By: Claude Opus 4.6 (1M context) --- cmd/entire/cli/setup.go | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/cmd/entire/cli/setup.go b/cmd/entire/cli/setup.go index 96432c3b3..2ff5fbc6a 100644 --- a/cmd/entire/cli/setup.go +++ b/cmd/entire/cli/setup.go @@ -388,15 +388,11 @@ If Entire is already configured but disabled, this re-enables it.`, return NewSilentError(errors.New("not a git repository")) } - // Change to repo root so all paths resolve correctly. - // This ensures agent hooks (e.g., .gemini/settings.json, .claude/settings.json) - // are created at the repo root even when running from a subdirectory. + // Notify the user when running from a subdirectory. + // All path resolution uses paths.WorktreeRoot(ctx) which returns the + // correct repo root regardless of CWD, so no directory change is needed. cwd, err := os.Getwd() //nolint:forbidigo // Need CWD to detect subdirectory if err == nil && cwd != repoRoot { - if err := os.Chdir(repoRoot); err != nil { - return fmt.Errorf("failed to change to repository root %s: %w", repoRoot, err) - } - paths.ClearWorktreeRootCache() fmt.Fprintf(cmd.ErrOrStderr(), "Note: Running from repository root (%s)\n\n", repoRoot) }