prettier / prettier/plugin-php

composer.json discovery fails in VS Code remote

Offen
#2,443 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Vorherrschende Sprache
PHP
Sterne
1.9k
Forks
139
PR-Merge-Kennzahlen
Keine gemergten PRs in 30 T.

Beschreibung

In my (admittedly specific) use case the plugin is unable to find composer.json, which is plainly present in the root of the workspace.

Environment

Running VS Code with a devcontainer in Rancher Desktop. Prettier plugin is running inside the container.
prettier and @prettier/plugin-php are installed locally inside node_modules and the VS Code plugin uses the local installation.

Settings:
"phpVersion": "composer"

Filesystem layout:

  • /workspaces/myproject/
    • composer.json
    • .prettierrc
    • node_modules/
    • src/
      • a.php
Result

When run on src/a.php through VS Code, the plugin fails.

["ERROR" - 9:22:26 AM] Error formatting document.
["ERROR" - 9:22:26 AM] Could not determine PHP version from composer; Could not find composer.json
Error: Could not determine PHP version from composer; Could not find composer.json
    at resolvePhpVersion (file:///workspaces/myproject/node_modules/@prettier/plugin-php/src/options.mjs:95:13)
    at Object.parse (file:///workspaces/myproject/node_modules/@prettier/plugin-php/src/parser.mjs:11:3)
    at parse5 (file:///workspaces/myproject/node_modules/prettier/index.mjs:16731:24)
    at async coreFormat (file:///workspaces/myproject/node_modules/prettier/index.mjs:17287:25)
    at async formatWithCursor (file:///workspaces/myproject/node_modules/prettier/index.mjs:17504:14)
    at async Module.format2 (file:///workspaces/myproject/node_modules/prettier/index.mjs:19051:25)

When doing the same thing through the CLI, not the VS Code plugin, everything works as expected.

Cause

A little bit of digging turns up the root cause here: https://github.com/prettier/plugin-php/blob/0c883a49850281077218007322f6149f853b2015/src/options.mjs#L23

In my environment, prettier is run in this directory: /vscode/vscode-server/bin/linux-x64/360a4e4fd251bfce169a4ddf857c7d25d1ad40da
The recursive search for composer.json upwards from process.cwd() fails.

I don't feel qualified to suggest a solution but maybe search from the target file's location upwards, instead of the process' working dir?

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginne in src/options.mjs bei der im Issue referenzierten Suche nach composer.json und verfolge anschließend, wie src/parser.mjs den Kontext der PHP-Datei weitergibt. Reproduziere den Unterschied zwischen der CLI- und der VS Code-Remote-Umgebung und verifiziere, dass die Formatierung von src/a.php das composer.json im Workspace-Root finden kann, während das bestehende CLI-Verhalten erhalten bleibt.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
php, vscode
Bereich
tooling
Issue-Typ
Bug
Schwierigkeit
3/5
Geschätzter Aufwand
1-2 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
52/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.