diff --git a/actions/setup/js/mcp_handler_go.cjs b/actions/setup/js/mcp_handler_go.cjs index ccb0e201f53..bc6a31147ff 100644 --- a/actions/setup/js/mcp_handler_go.cjs +++ b/actions/setup/js/mcp_handler_go.cjs @@ -40,6 +40,7 @@ function createGoHandler(server, toolName, scriptPath, timeoutSeconds = 60) { ["run", scriptPath], { env: process.env, + cwd: process.env.GITHUB_WORKSPACE || process.cwd(), timeout: timeoutSeconds * 1000, // Convert to milliseconds maxBuffer: 10 * 1024 * 1024, // 10MB buffer }, diff --git a/actions/setup/js/mcp_handler_go.test.cjs b/actions/setup/js/mcp_handler_go.test.cjs index 2945263699c..034cfc5b29b 100644 --- a/actions/setup/js/mcp_handler_go.test.cjs +++ b/actions/setup/js/mcp_handler_go.test.cjs @@ -174,4 +174,43 @@ func main() { const output = JSON.parse(result.content[0].text); expect(output).toEqual(complexInput); }, 30000); // Increase timeout to allow for Go compilation + + it("should execute script from GITHUB_WORKSPACE directory", async () => { + // Save original GITHUB_WORKSPACE + const originalWorkspace = process.env.GITHUB_WORKSPACE; + + // Set GITHUB_WORKSPACE to tempDir + process.env.GITHUB_WORKSPACE = tempDir; + + try { + // Create a Go script that outputs current working directory + testScriptPath = path.join(tempDir, "test-cwd.go"); + const goCode = `package main + +import ( + "encoding/json" + "os" +) + +func main() { + cwd, _ := os.Getwd() + result := map[string]interface{}{"cwd": cwd} + json.NewEncoder(os.Stdout).Encode(result) +}`; + fs.writeFileSync(testScriptPath, goCode); + + const handler = createGoHandler(mockServer, "cwd-tool", testScriptPath); + const result = await handler({}); + + const output = JSON.parse(result.content[0].text); + expect(output.cwd).toBe(tempDir); + } finally { + // Restore original GITHUB_WORKSPACE + if (originalWorkspace === undefined) { + delete process.env.GITHUB_WORKSPACE; + } else { + process.env.GITHUB_WORKSPACE = originalWorkspace; + } + } + }, 30000); // Increase timeout to allow for Go compilation }); diff --git a/actions/setup/js/mcp_handler_javascript.cjs b/actions/setup/js/mcp_handler_javascript.cjs index 4ef92f30cfa..37338078b0b 100644 --- a/actions/setup/js/mcp_handler_javascript.cjs +++ b/actions/setup/js/mcp_handler_javascript.cjs @@ -40,6 +40,7 @@ function createJavaScriptHandler(server, toolName, scriptPath, timeoutSeconds = [scriptPath], { env: process.env, + cwd: process.env.GITHUB_WORKSPACE || process.cwd(), timeout: timeoutSeconds * 1000, // Convert to milliseconds maxBuffer: 10 * 1024 * 1024, // 10MB buffer }, diff --git a/actions/setup/js/mcp_handler_javascript.test.cjs b/actions/setup/js/mcp_handler_javascript.test.cjs index b14f27f0045..658faebf07e 100644 --- a/actions/setup/js/mcp_handler_javascript.test.cjs +++ b/actions/setup/js/mcp_handler_javascript.test.cjs @@ -149,34 +149,66 @@ process.stdin.on('end', () => { expect(output).toEqual(complexInput); }); - it("should handle async operations", async () => { - testScriptPath = path.join(tempDir, "async.cjs"); - const jsCode = ` -let input = ''; -process.stdin.on('data', chunk => { - input += chunk; -}); - -process.stdin.on('end', async () => { - const inputs = JSON.parse(input); - - // Simulate async operation - await new Promise(resolve => setTimeout(resolve, 100)); - - const result = { - message: "Async completed", - input: inputs - }; - console.log(JSON.stringify(result)); -}); + it("should execute script from GITHUB_WORKSPACE directory", async () => { + // Save original GITHUB_WORKSPACE + const originalWorkspace = process.env.GITHUB_WORKSPACE; + + // Set GITHUB_WORKSPACE to tempDir + process.env.GITHUB_WORKSPACE = tempDir; + + try { + // Create a JavaScript script that outputs current working directory + testScriptPath = path.join(tempDir, "test-cwd.cjs"); + const jsCode = ` +const result = { cwd: process.cwd() }; +console.log(JSON.stringify(result)); `; - fs.writeFileSync(testScriptPath, jsCode); + fs.writeFileSync(testScriptPath, jsCode); + + const handler = createJavaScriptHandler(mockServer, "cwd-tool", testScriptPath); + const result = await handler({}); + + const output = JSON.parse(result.content[0].text); + expect(output.cwd).toBe(tempDir); + } finally { + // Restore original GITHUB_WORKSPACE + if (originalWorkspace === undefined) { + delete process.env.GITHUB_WORKSPACE; + } else { + process.env.GITHUB_WORKSPACE = originalWorkspace; + } + } + }); - const handler = createJavaScriptHandler(mockServer, "async-tool", testScriptPath); - const result = await handler({ test: "data" }); + it("should use process.cwd() when GITHUB_WORKSPACE is not set", async () => { + // Save original GITHUB_WORKSPACE + const originalWorkspace = process.env.GITHUB_WORKSPACE; - const output = JSON.parse(result.content[0].text); - expect(output.message).toBe("Async completed"); - expect(output.input).toEqual({ test: "data" }); + // Unset GITHUB_WORKSPACE + delete process.env.GITHUB_WORKSPACE; + + try { + // Create a JavaScript script that outputs current working directory + testScriptPath = path.join(tempDir, "test-default-cwd.cjs"); + const jsCode = ` +const result = { cwd: process.cwd() }; +console.log(JSON.stringify(result)); +`; + fs.writeFileSync(testScriptPath, jsCode); + + const handler = createJavaScriptHandler(mockServer, "default-cwd-tool", testScriptPath); + const result = await handler({}); + + const output = JSON.parse(result.content[0].text); + // When GITHUB_WORKSPACE is not set, should use process.cwd() + expect(output.cwd).toBe(process.cwd()); + } finally { + // Restore original GITHUB_WORKSPACE + if (originalWorkspace === undefined) { + delete process.env.GITHUB_WORKSPACE; + } else { + process.env.GITHUB_WORKSPACE = originalWorkspace; + } + } }); }); diff --git a/actions/setup/js/mcp_handler_python.cjs b/actions/setup/js/mcp_handler_python.cjs index 1807e86c56d..cff24f14d29 100644 --- a/actions/setup/js/mcp_handler_python.cjs +++ b/actions/setup/js/mcp_handler_python.cjs @@ -40,6 +40,7 @@ function createPythonHandler(server, toolName, scriptPath, timeoutSeconds = 60) [scriptPath], { env: process.env, + cwd: process.env.GITHUB_WORKSPACE || process.cwd(), timeout: timeoutSeconds * 1000, // Convert to milliseconds maxBuffer: 10 * 1024 * 1024, // 10MB buffer }, diff --git a/actions/setup/js/mcp_handler_shell.cjs b/actions/setup/js/mcp_handler_shell.cjs index cda3f282766..a838e5f9b90 100644 --- a/actions/setup/js/mcp_handler_shell.cjs +++ b/actions/setup/js/mcp_handler_shell.cjs @@ -55,6 +55,7 @@ function createShellHandler(server, toolName, scriptPath, timeoutSeconds = 60) { [], { env, + cwd: process.env.GITHUB_WORKSPACE || process.cwd(), timeout: timeoutSeconds * 1000, // Convert to milliseconds maxBuffer: 10 * 1024 * 1024, // 10MB buffer },