gpt-pilot icon indicating copy to clipboard operation
gpt-pilot copied to clipboard

[Bug]: When pressing Start App - no such file or directory, open 'E:\Program Files\Microsoft VS Code\package.json' error

Open radrad opened this issue 6 months ago • 5 comments

Version

VisualStudio Code extension

Operating System

Windows 10

What happened?

I am following this tutorial: https://www.youtube.com/watch?v=iwLe6UWyaS4&ab_channel=MatthewBerman. For some reason a working directory is not set to d:\gptpilot\gpt-pilot\workspace\Auth-Me, but to the installation folder for VS code: E:\Program Files\Microsoft VS Code\

I can go to terminal and cd d:\gptpilot\gpt-pilot\workspace\Auth-Me and start node server just fine. ❯ npm start

[email protected] start node src/server.js

I checked VS code entension settings for User and Workspace which were empty and set Installation path for GPT Pilot: to D:\gptpilot

So what is it withing VS extension that cannot understand what is the current working folder: Auth-Me created by code generation. The code is generated/modified just fine.

Error log:

npm

ERR! code ENOENT

npm ERR! syscall open

npm ERR! path E:\Program Files\Microsoft VS Code\package.json

npm ERR! errno -4058

npm ERR! enoent Could not read package.json: Error: ENOENT: no such file or directory, open 'E:\Program Files\Microsoft VS Code\package.json'

npm ERR! enoent This is related to npm not being able to find a file.

npm ERR! enoent

radrad avatar Jan 01 '24 22:01 radrad

I think I fixed this problem on windows 10.

I was debugging GPT Pilot svelte vs code extension and I put breakpoint on this line: Line 270: const cmd = cd ${vscode.workspace.workspaceFolders?.[0].uri.fsPath}/${folder} && ${command};

... where a command is about to be sent for the execution in windows command prompt. D:\gptpilot\gpt-pilot\workspace\Autho-Me && npm start "cd D:\\gptpilot\gpt-pilot\workspace/Autho-Me && npm start"

With existing code above doesn't work on Windows: See I was getting an error*

Thanks to GPT chat 4 (see below my question and answer) Current working directory is sent as a separate argument: cwd so the command prompt will just execute npm start command in the current directory just fine.

Since VS code is not public, I cannot send a PR, so can someone fix the code please.

I was getting an error*

npm ERR! A complete log of this run can be found in: C:\Users<user>\AppData\Local\npm-cache_logs\2024-01-02T05_00_46_271Z-debug-0.log

npm ERR! code ENOENT

npm ERR! syscall open

npm ERR! path E:\Program Files\Microsoft VS Code\package.json

npm ERR! errno -4058

npm ERR! enoent Could not read package.json: Error: ENOENT: no such file or directory, open 'E:\Program Files\Microsoft VS Code\package.json'

npm ERR! enoent This is related to npm not being able to find a file.

npm ERR! enoent

Thanks to GPT chat 4, it suggested splitting cd (change directory) and runnng npm start commands and to pass current directory as a separate argument: cwd. This fixed the problem

The error message suggests that npm is trying to find a package.json file in the E:\Program Files\Microsoft VS Code directory, which is likely not the intended directory. This could be due to the way the command is being executed in the Node.js child process.

The cd command changes the current directory of the shell, and && runs another command in the same shell. However, if the shell is not correctly interpreting the &&, it might not be running the cd and npm start commands in the same context.

Here's how you can modify the runCommand function to change the current working directory of the child process directly, instead of using the cd command:

async runCommand(command, folder) { const cmd =${command}`; const cwd = path.join(vscode.workspace.workspaceFolders?.[0].uri.fsPath, folder); this.processStdout = ''; this.runCommandProcess = (0, child_process_1.spawn)(shell, [shellFlag, cmd], { cwd }); this.runCommandProcess.stdout.on('data', (data) => { this.sendSeparatedLines(data.toString(), 'green'); this.processStdout += data.toString(); }); this.runCommandProcess.stderr.on('data', (data) => { this.sendSeparatedLines(data.toString(), 'red'); this.processStdout += data.toString(); }); }

`

In this version, the cwd option is passed to child_process.spawn to set the current working directory of the child process. This should have the same effect as running the cd command, but it avoids potential issues with command concatenation and shell interpretation.

radrad avatar Jan 02 '24 05:01 radrad

Thanks @radrad for looking into this!

We're currently doing:

const cmd = `cd ${vscode.workspace.workspaceFolders?.[0].uri.fsPath}/${folder} && ${command}`;
...
spawn(shell, [shellFlag, cmd]);

Instead, we should be doing something along the lines of (not tested):

const cwd = path.join(vscode.workspace.workspaceFolders?.[0].uri.fsPath, folder);
spawn(shell, [shellFlag, command], { cwd });

I am also not sure that we should be allowing workspaceFolders to be empty here (as implied by ?), because this will also result in an incorrect path.

senko avatar Jan 02 '24 08:01 senko

@senko Is VS code open source? I sent you an email if you can read it.

Yes, you are right. Let's make GPT interaction reliable on Windows. Also I want to mention there was an error when trying to do "mkdir views" based on gpt response and the "views" folder actually existed, but the vs code extension could not figure this out. So I would recommend that you manage current folder (that you are going to send to cmp prompt centrally and use it throughout code basem, since I belive you are not keeping cmd prompt as global variable and you need to spawn it each time a command is called.

radrad avatar Jan 02 '24 09:01 radrad

@radrad the VSCode extension is not open source currently.

Regarding the view sub-folder I believe this is the same problem as https://github.com/Pythagora-io/gpt-pilot/issues/440

WRT the code folder ideally all the commands should be self sufficient and expect to be run from project root, as that's easier to keep track of.

senko avatar Jan 02 '24 13:01 senko

Can we agree that to set working folder as a separate argument: cwd and execute intended command: command next:

const cwd = path.join(vscode.workspace.workspaceFolders?.[0].uri.fsPath, folder); spawn(shell, [shellFlag, command], { cwd });

I hope you can check every occurance of this call throughout the code base and provide the fix to VS code and non VS code.

radrad avatar Jan 02 '24 17:01 radrad