0

I'm using VS Code with php-debug extension, IIS Express and PHP (IIS/PHP from Web PI) and trying to automate IIS Express startup with a preLaunchTask on launch.json file from Visual Studio Code. So I wrote those things below on my launch.json and tasks.json files.

All things work nicely, except for the fact that VS Code keeps locked on IIS Startup, because IIS stay running, waiting for the user input (i.e.: "Q" to quit IIS). This way, even with IIS running ok, I couldn't debug inside VS Code, because it's waiting the IIS task to finish.

I've tried with Powershell and Start-Job (PS script excerpt below) without success, because PowerShell seems to start IIS, but nothing happens (IIS is not started).

How could I accomplish that? Where I'm doing it wrong? Thank you in advance.

launch.json

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Listen for XDebug",
            "type": "php",
            "request": "launch",
            "port": 9000, "preLaunchTask": "start_server"
        }
    ]
}

tasks.json -- cmd version

{
   "version": "0.1.0",
   "command": "cmd.exe",
   "isShellCommand": true,
   "args": [
       "/C",
       "runphp.cmd"
   ],
   "tasks": [
       {
           "taskName": "start_server",
           "isBuildCommand": false,
           "showOutput": "always"
       }
   ]
}

tasks.json -- Powershell version

{
   "version": "0.1.0",
   "command": "PowerShell.exe",
   "isShellCommand": true,
   "args": [
       "-ExecutionPolicy",
       "ByPass",
       "-File",
       ".\\wahoo.ps1" // my powershell script
   ],
   "tasks": [
       {
           "taskName": "start_server",
           "isBuildCommand": false,
           "showOutput": "always"
       }
   ]
}

wahoo.ps1 -- only used when I set the powershell version from tasks.json

$scriptDir = Split-Path -Path $MyInvocation.MyCommand.Definition -Parent
$command = @'
cd $scriptDir | cmd.exe /C runphp.cmd
'@

Start-Job -Name "IISPHP" -Arg $command -ScriptBlock {
    param ($command)
    Invoke-Expression -Command:$command
    Start-Sleep 3
}

My runphp.cmdis an adapted version of the original runphp.cmd from IIS/PHP installation folder to suit my needs and contains the following:

runphp.cmd

SET pf=%ProgramFiles%
if DEFINED ProgramFiles(x86) SET pf=%ProgramFiles(x86)%

set appcmd="%pf%\iis express\appcmd.exe"
set iisexpress="%pf%\iis express\iisexpress.exe"

cd > tmpFile
set /p currentdir= < tmpFile
del tmpFile

where php-cgi.exe > tmpFile
set /p phprt= < tmpFile
del tmpFile
if DEFINED phprt goto setup_iis
SET phprt=%pf%\PHP\v5.3\php-cgi.exe

:setup_iis
%appcmd% set config /section:system.webServer/fastCgi "/+[fullPath='%phprt%']" "/apphostconfig:%currentdir%\applicationhost.config"
%appcmd% set config /section:system.webServer/handlers "/+[name='PHP-FastCGI',path='*.php',modules='FastCgiModule',verb='*', scriptProcessor='%phprt%',resourceType='Either']" "/apphostconfig:%currentdir%\applicationhost.config"
%iisexpress% /site:EmptySite /config:"%currentdir%\applicationhost.config"

2 Answers 2

1

I had a similar problem (in my case I needed to run a browser from within VS Code) and this is how it worked for me.

At first I created a file only containing one line for passing two commandline-parameters of that file to the command-interpreter.

my_vsCode_tasks.cmd

%1 %2

which says 'run program %1 with argument %2'.

And this was my corresponding

tasks.json

{
    "version": "0.1.0",
    "command": "C:\\path\\to\\file\\my_vsCode_tasks.cmd",
    "tasks" : [
        {
            "taskName": "Tool 1",
            "suppressTaskName": true,
            "args": ["C:\\Program Files (x86)\\my\\first\\tool.exe", "${file}"],
        },
        {
            "taskName": "Tools 2",
            "suppressTaskName": true,
            "args": ["C:\\Program Files (x86)\\my\\second\\tool2.exe", "${file}"],
        },
    ]
}

I also tried it alternatively with powershell. Worked too. This was my

runTool.ps1

$whichTool=$args[0]
$whichArg=$args[1]
start-process "$whichTool" -ArgumentList "$whichArg"

With a slightly different

tasks.json

{
   "version": "0.1.0",
   "command": "call",
   "isShellCommand": true,
   "showOutput": "always",
   "args": ["powershell", "-NoProfile", "-file", "C:\\path\\to\\file\\runTool.ps1"],
   "tasks": [
        {
            "taskName": "C:\Program Files (x86)\my\second\tool.exe",
            "suppressTaskName": false,
            "args": ["${file}"]
        }
    ]
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you @RobertK I'll check this out!
-1

There's an IISExpress extension made for VS Code: https://github.com/warrenbuckley/IIS-Express-Code

1 Comment

That extension is useless for PHP projects as it injects a default applicationHost.config file that does not have the necessary configuration settings to enable PHP. The developer has stated that he does not plan to support custom applicationHosts.config files either.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.