Skip to content

feat: Support running on windows #181

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 1, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 71 additions & 1 deletion src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { LanguageClient, ServerOptions, LanguageClientOptions, StreamInfo } from 'vscode-languageclient'
import { EvaluatableExpressionRequest } from './protocol'

import { spawn } from 'child_process'

import * as vscode from 'vscode'
import { join } from 'path'
import * as net from 'net'
Expand Down Expand Up @@ -92,7 +94,10 @@ function checkPlatform(): boolean {

function getServerOptions(config: PhpactorConfig): ServerOptions {
let serverOptions
if (!config.remote.enabled) {
if (!config.remote.enabled && process.platform === 'win32') {
// PHP on windows has problems with STDIO so we need special handling
serverOptions = getWindowsServerOptions(config)
} else if (!config.remote.enabled) {
// launch language server via stdio
serverOptions = <ServerOptions>{
run: {
Expand Down Expand Up @@ -133,6 +138,71 @@ function getServerOptions(config: PhpactorConfig): ServerOptions {
return serverOptions
}

function getWindowsServerOptions(config: PhpactorConfig): ServerOptions {
// Find a free port, start PHPActor and connect to it
const serverOptions = async () => {
const findPort = new Promise<number>(resolve => {
const server = net.createServer()
server.listen(0, '127.0.0.1', () => {
const freePort = (server.address()! as net.AddressInfo).port
server.close()
resolve(freePort)
})
})

const freePort = await findPort

const startServer = new Promise<void>((resolve, reject) => {
const childProcess = spawn(
config.executablePath,
[config.path, 'language-server', `--address=127.0.0.1:${freePort}`, ...config.launchServerArgs],

{
env: {
...process.env,
XDEBUG_MODE: 'debug',
PHPACTOR_ALLOW_XDEBUG: '1',
},
}
)

childProcess.stderr.on('data', (chunk: Buffer) => {
const str = chunk.toString()
languageClient.outputChannel.appendLine(str)

// when we get the first line, the server is running
resolve()
})
childProcess.on('exit', (code, signal) => {
languageClient.outputChannel.appendLine(
`Language server exited ` + (signal ? `from signal ${signal}` : `with exit code ${code}`)
)
if (code !== 0) {
languageClient.outputChannel.show()
}

reject(new Error('Language Server exited'))
})
})

await startServer

const socket = net.connect({
host: '127.0.0.1',
port: freePort,
})

const result = <StreamInfo>{
writer: socket,
reader: socket,
}

return result
}

return serverOptions
}

function createClient(config: PhpactorConfig): LanguageClient {
const serverOptions = getServerOptions(config)

Expand Down