3

I'm currently working on a PHP REST API project. I'm new to VSCode, I'm currently trying the capabilities of the IDE. In development situation, I use the built-in PHP server to run my API, using CLI :

php -S localhost:8000 -t public

I tried to create a launch configuration to be able to run this built-in server from VSCode, using a .vscode/launch.json file. It worked at first, but I noticed that when I end the debug session, the built-in server is not killed. It still runs. And the next execution of the launch config does not restart it.

So I tried to use the postDebugTask property of the launch config, to kill the php process, following this post. I ended up with the following files:

launch.json

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Run",
            "type": "php",
            "request": "launch",
            "preLaunchTask": "Run PHP built-in server",
            "postDebugTask": "Kill PHP built-in server",
            "port": 9000
        }
    ]
}

tasks.json

{
    // @see https://code.visualstudio.com/docs/editor/tasks
    "version": "2.0.0",
    "tasks": [
        {
            "label": "Run PHP built-in server",
            "type": "shell",
            "command": "php -S localhost:8000 -t public"
        },
        {
            "label": "Kill PHP built-in server",
            "type": "shell",
            "command": "kill $(ps aux | grep php | awk '{print $1}')"
        }
    ]
}

The problem is when I stop the debug session, the "Kill task" is never executed. Any idea why? Could it have something to do with the fact that the php command does not give back prompt in the terminal?

Edit: I also discovered that using preLaunchTask to start the built-in PHP server prevent xdebug from working. Maybe the port binding on 9000 happens too soon.

Starting the built-in server through a launch profile is starting to seem like a bad idea...

Eria
  • 2,653
  • 6
  • 29
  • 56

0 Answers0