Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Dec 1, 2025

Description

This PR adds problem matcher definitions to the Prettier VS Code extension, enabling VS Code tasks running prettier --check to display formatting issues directly in the Problems panel. The implementation is based on Rayat Rahman's (@riotrah) MIT-licensed work, with improvements to regex patterns for better specificity and robustness.

Problem Matchers Added:

  • $prettier - Matches Prettier syntax errors with line and column information
  • $prettier-watch - Same as $prettier but for watch mode tasks (e.g., with onchange)
  • $prettier-warn - Matches file-level warnings for unformatted files
  • $prettier-warn-watch - Same as $prettier-warn but for watch mode tasks

Pattern Improvements:

  • Error pattern uses [^:]+ to match filenames up to the first colon
  • Warning pattern uses [^\s]+\.[a-zA-Z0-9.-]+ to match files with complex extensions (.test.js, .d.ts, .spec.tsx)
  • Patterns specifically exclude Prettier summary messages to prevent false matches

Related Issue

Fixes #2554

Type of Change

  • Bug fix (non-breaking change that fixes an issue)
  • New feature (non-breaking change that adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation update
  • Refactoring (no functional changes)
  • CI/Build changes

Checklist

  • I have read the CONTRIBUTING guidelines
  • My code follows the code style of this project (npm run lint passes)
  • I have run npm run prettier to format my code
  • I have added/updated tests that prove my fix or feature works
  • All new and existing tests pass (npm test)
  • I have updated the CHANGELOG.md with a summary of my changes
Original prompt

This section details on the original issue you should resolve

<issue_title>Problem matcher for VS Code</issue_title>
<issue_description>Is your feature request related to a problem? Please describe.
When running prettier --check as a task, it would be nice to have a problem matcher definitions so any unformatted files show as problems in VS Code's problems view.

Describe the solution you'd like
For the extension to contribute an appropriate problem matcher.

Describe alternatives you've considered
Implement one myself and copy paste it anywhere I need it.

Additional context
https://code.visualstudio.com/docs/editor/tasks#_defining-a-problem-matcher

Do note that the current output format of prettier --check might not be the most suitable for matching with a VS Code problem matcher.
</issue_description>

<agent_instructions>Somebody already wrote a version that might work, but not sure. There code is here:

	"contributes": {
		"problemMatchers": [
			{
				"name": "prettier",
				"label": "Prettier check problems",
				"owner": "prettier",
				"source": "prettier style check",
				"applyTo": "closedDocuments",
				"fileLocation": [
					"relative",
					"${workspaceRoot}"
				],
				"severity": "error",
				"pattern": {
					"regexp": "\\[(error)\\] (.+?): (.+) \\((\\d+?):(\\d+?)\\)",
					"kind": "location",
					"severity": 1,
					"file": 2,
					"message": 3,
					"line": 4,
					"column": 5
				}
			},
			{
				"base": "$prettier",
				"name": "prettier-watch",
				"label": "Prettier check watch problems (prettier + onchange watch mode)",
				"background": {
					"activeOnStart": true,
					"beginsPattern": "Checking formatting",
					"endsPattern": "(\\[warn\\] Code style issues found in the above file\\(s\\))|(All matched files use Prettier code style!)"
				}
			},
			{
				"base": "$prettier",
				"name": "prettier-warn",
				"label": "Prettier check warning problems (prettier file-level warnings)",
				"severity": "warning",
				"pattern": {
					"regexp": "\\[warn\\] (.+)",
					"kind": "file",
					"file": 1,
					"message": 1
				}
			},
			{
				"base": "$prettier-warn",
				"name": "prettier-warn-watch",
				"label": "Prettier check warning watch problems (prettier file-level warnings + onchange watch mode)",
				"background": {
					"activeOnStart": true,
					"beginsPattern": "Checking formatting",
					"endsPattern": "(\\[warn\\] Code style issues found in the above file\\(s\\))|(All matched files use Prettier code style!)"
				}
			}
		]
	},

if we use it reference their license here:

The MIT License (MIT)

Copyright (c) 2022-2022 Rayat Rahman

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
```</agent_instructions>

## Comments on the Issue (you are @copilot in this section)

<comments>
</comments>


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Co-authored-by: ntotten <282782+ntotten@users.noreply.github.com>
@ntotten
Copy link
Member

ntotten commented Dec 1, 2025

@copilot we need tests for this

Copilot AI and others added 2 commits December 1, 2025 23:22
Co-authored-by: ntotten <282782+ntotten@users.noreply.github.com>
Co-authored-by: ntotten <282782+ntotten@users.noreply.github.com>
Copilot AI changed the title [WIP] Add problem matcher for Prettier in VS Code Add problem matchers for Prettier --check output Dec 1, 2025
Copilot AI requested a review from ntotten December 1, 2025 23:30
Co-authored-by: ntotten <282782+ntotten@users.noreply.github.com>
@ntotten
Copy link
Member

ntotten commented Dec 1, 2025

@copilot update the changelog and readme. Mention @riotrah in the changelog as the contributor.

@ntotten ntotten marked this pull request as ready for review December 1, 2025 23:36
Copilot AI review requested due to automatic review settings December 1, 2025 23:36
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds problem matcher support for Prettier's --check output, enabling VS Code tasks to display formatting issues in the Problems panel. The implementation is based on Rayat Rahman's MIT-licensed work, with improvements to regex patterns for better specificity.

Key changes:

  • Added 4 problem matchers to handle error and warning output in both regular and watch modes
  • Created comprehensive test coverage for pattern matching behavior
  • Updated documentation with usage examples for tasks and watch mode

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.

File Description
package.json Defines 4 problem matchers ($prettier, $prettier-watch, $prettier-warn, $prettier-warn-watch) with regex patterns to parse Prettier CLI output
src/test/suite/problemMatcher.test.ts Adds unit tests verifying problem matcher registration and regex pattern matching
README.md Documents the problem matchers with usage examples for regular and watch mode tasks
THIRDPARTY.md Adds MIT license attribution for Rayat Rahman's original problem matcher code
Comments suppressed due to low confidence (1)

src/test/suite/problemMatcher.test.ts:216

  • The test is attempting to access testCase.input on line 215, but shouldNotMatch is an array of strings (line 208-212), not objects with an input property. This will result in match being undefined.match(pattern), which will throw a TypeError at runtime.

Fix:

shouldNotMatch.forEach((testCase) => {
  const match = testCase.match(pattern);  // testCase is already a string
  assert.ok(!match, `Should NOT match: ${testCase}`);
});

Comment on lines +5 to +14

/**
* Helper to get problem matchers from extension
*/
function getProblemMatchers() {
const extension = vscode.extensions.getExtension(EXTENSION_ID);
assert.ok(extension, "Extension should be available");
return extension.packageJSON.contributes.problemMatchers;
}

Copy link

Copilot AI Dec 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test file should follow the project convention of using ensureExtensionActivated() from testUtils.js in a before() hook at the top level, rather than manually checking and activating the extension in each test. This is the pattern used consistently across all other test files in the suite (e.g., format.test.ts, config.test.ts, module.test.ts, etc.).

Recommended refactoring:

import { ensureExtensionActivated } from "./testUtils.js";

describe("Problem Matchers", () => {
  before(async () => {
    await ensureExtensionActivated();
  });

  it("should have prettier problem matcher registered", () => {
    const extension = vscode.extensions.getExtension("prettier.prettier-vscode");
    assert.ok(extension, "Extension should be available");
    // ... rest of test
  });

This ensures consistent test setup and avoids redundant activation logic in every test.

Copilot uses AI. Check for mistakes.
Comment on lines +189 to +195
);
assert.ok(
endsPattern.test("All matched files use Prettier code style!"),
"Should match end pattern for success",
);
});
});
Copy link

Copilot AI Dec 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test cases for file path matching should include edge cases to ensure the regex pattern handles various file path formats correctly:

  • File paths with directories containing dots (e.g., [warn] src/v2.0/file.js)
  • File names with multiple dots (e.g., [warn] component.test.d.ts)
  • File paths with underscores or hyphens (e.g., [warn] my-component_v2.js)

These edge cases would help validate that the regex pattern in package.json (line 507) correctly matches all valid file paths while properly excluding summary messages.

Copilot uses AI. Check for mistakes.
Copy link
Contributor Author

Copilot AI commented Dec 1, 2025

@copilot we need tests for this

Added comprehensive tests in commit 4bc8ecd. The test suite includes:

  • Registration tests verifying all 4 problem matchers exist
  • Pattern matching tests for error outputs with line/column information
  • Pattern matching tests for warning outputs (file-level)
  • Tests verifying summary messages are correctly excluded
  • Background pattern tests for watch mode start/end markers

All tests use helper functions to reduce duplication and improve maintainability.

@ntotten
Copy link
Member

ntotten commented Dec 2, 2025

@copilot rwview the test failures and keep working

…date CHANGELOG

Co-authored-by: ntotten <282782+ntotten@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Dec 2, 2025

@copilot rwview the test failures and keep working

Fixed the test issues in commit 5a7eaf6:

  • Updated test to use ensureExtensionActivated() from testUtils.js for consistent setup
  • Added edge case tests for file paths with dots, hyphens, and underscores
  • Updated CHANGELOG with @riotrah credit for the problem matcher contribution

All tests now follow project conventions and include comprehensive edge case coverage.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Problem matcher for VS Code

2 participants