stackblitz / webcontainer-core

Dev environments. In your web app.
https://webcontainers.io
MIT License
3.95k stars 176 forks source link

Cannot use "cd projectname" #1458

Closed Nazeofel closed 5 months ago

Nazeofel commented 5 months ago

Describe the bug

Hello, I am trying to setup a webContainer but when creating my project with npx I cannot CD into it

Link to the blitz that caused the error

https://stackblitz.com/edit/vitejs-vite-srlthz?file=src%2Fhooks%2FuseContainer.ts,src%2FApp.css,src%2FApp.tsx&terminal=dev

Steps to reproduce

Open the stackblitz

Open dev tools

Check the logs

see that even after the cd command in src/hooks/useWebContainer the ls -a command does not print the inside the of "myproject" folder.

Expected behavior

going into my project folder and seeing the content inside.

image

Parity with Local

Screenshots

No response

Platform

Browser Chrome for the stackblitz

Mac when using it locally

Additional context

No response

SamVerschueren commented 5 months ago

Hey @Nazeofel 👋 ! Thanks for reaching out!

So the thing is that you should see webcontainer.spawn() the same way as Node.js child_process.spawn(). Running cd folder doesn't do anything for the next spawned process as it will just start fresh with the current working directory.

The problem here was that you couldn't specify the working directory of the child process, which is what I now added (because of this issue). You can read more about this property on the API reference on webcontainers.io.

For this to work, you will need to use @webcontainer/api@1.2.0 though. You can then change the ls -a spawned process to

const ls = await webContainer.spawn('ls', ['-a'], {
  cwd: 'myproject'
});

And the one you use to run the dev server

await webContainer.spawn('npm', ['run', 'dev'], {
   cwd: 'myproject'
});

I forked your project and made some small changes https://stackblitz.com/~/edit/vitejs-vite-ypethu?file=src/hooks/useContainer.ts. It's complaining about an API key that is missing though, so that's why it's not starting up.

error - DISCORD_TOKEN or DISCORD_CLIENT_ID not found in environment variables

Hope this helps 🙏 .

SamVerschueren commented 5 months ago

I'm closing this issue for now as I think it is resolved. Let me know if there's anything else you run into.