Skip to content

Commit 4fc7adf

Browse files
thenoidRocky Olsen
andauthored
fix: restore CWD after local_preview MCP tool to support subdirectory workspaces (#390)
When the MCP server is started from a subdirectory of a git repository, createLocalPreviewRun archives from that subdirectory rather than the repo root, causing Spacelift to fail with "project root ... does not exist". Save the original CWD before calling createLocalPreviewRun with FindRepositoryRoot=true (same as the CLI), then defer a restore so the long-running MCP server process has its working directory restored after each call. Co-authored-by: Rocky Olsen <rolsen@lucidchart.com>
1 parent 7b8c3c8 commit 4fc7adf

1 file changed

Lines changed: 11 additions & 1 deletion

File tree

internal/cmd/stack/mcp.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"encoding/json"
66
"fmt"
7+
"os"
78
"strings"
89

910
"github.com/mark3labs/mcp-go/mcp"
@@ -639,6 +640,15 @@ func registerLocalPreviewTool(s *server.MCPServer, options McpOptions) {
639640

640641
awaitForCompletion := request.GetString("await_for_completion", "true") == "true"
641642

643+
// Save and restore CWD: createLocalPreviewRun with FindRepositoryRoot=true
644+
// calls os.Chdir() to the git root. We restore afterward so the long-running
645+
// MCP server process isn't left with a permanently changed working directory.
646+
origDir, err := os.Getwd()
647+
if err != nil {
648+
return nil, fmt.Errorf("failed to get working directory: %w", err)
649+
}
650+
defer os.Chdir(origDir) //nolint: errcheck
651+
642652
// Create a string builder to capture output
643653
var outputBuilder strings.Builder
644654

@@ -649,7 +659,7 @@ func registerLocalPreviewTool(s *server.MCPServer, options McpOptions) {
649659
EnvironmentVars: envVars,
650660
Targets: targets,
651661
Path: path,
652-
FindRepositoryRoot: false,
662+
FindRepositoryRoot: true,
653663
DisregardGitignore: false,
654664
UseHeaders: options.UseHeadersForLocalPreview,
655665
NoUpload: false,

0 commit comments

Comments
 (0)