diff --git a/.github/bin/format-package-json.mjs b/.github/bin/format-package-json.mjs index 144af8880..5db7b7df1 100644 --- a/.github/bin/format-package-json.mjs +++ b/.github/bin/format-package-json.mjs @@ -1,4 +1,5 @@ import { promises as fsp, constants } from 'fs'; +import path from 'path'; const packageJSONInfo = JSON.parse(await fsp.readFile('./package.json', 'utf8')); const packageJSONInfoCopy = JSON.stringify(packageJSONInfo, null, '\t'); @@ -199,7 +200,7 @@ const formatted = {}; Object.assign(formatted, packageJSONInfo); if (process.env.GITHUB_ACTIONS && JSON.stringify(formatted, null, '\t') !== packageJSONInfoCopy) { - console.error('package.json has an incorrect field order. Run "npm run lint" to resolve.') + console.error(`::error file=${path.relative(process.env.GITHUB_WORKSPACE, path.resolve('./package.json'))},line=1,col=1::package.json has an incorrect field order. Run "npm run lint" to resolve.`); process.exit(1); } diff --git a/packages/postcss-tape/src/github-annotations.ts b/packages/postcss-tape/src/github-annotations.ts index db8759287..2835e9f01 100644 --- a/packages/postcss-tape/src/github-annotations.ts +++ b/packages/postcss-tape/src/github-annotations.ts @@ -1,8 +1,17 @@ +import path from 'path'; + export function formatGitHubActionAnnotation(message, level = 'error', options = {}) { let output = '::' + level; const outputOptions = Object.keys(options).map((key) => { - return `${key}=${escapeValue(String(options[key]))}`; + let value = String(options[key]); + + if (key === 'file' && process.env.GITHUB_WORKSPACE) { + // make file paths relative to the workspace root. + value = path.relative(process.env.GITHUB_WORKSPACE, path.resolve(value)); + } + + return `${key}=${escapeValue(value)}`; }).join(','); if (outputOptions) { diff --git a/packages/postcss-tape/src/index.ts b/packages/postcss-tape/src/index.ts index cc305393b..b67c8f9c5 100644 --- a/packages/postcss-tape/src/index.ts +++ b/packages/postcss-tape/src/index.ts @@ -248,7 +248,7 @@ export default function runner(currentPlugin: PluginCreator) { console.log(formatGitHubActionAnnotation( formatCSSAssertError(testCaseLabel, testCaseOptions, err, true), 'error', - { file: normalizeFilePathForGithubAnnotation(expectFilePath), line: 1, col: 1 }, + { file: expectFilePath, line: 1, col: 1 }, )); } else { failureSummary.add(testCaseLabel); @@ -344,7 +344,7 @@ export default function runner(currentPlugin: PluginCreator) { console.log(formatGitHubActionAnnotation( 'testing older PostCSS:\n' + formatCSSAssertError(testCaseLabel, testCaseOptions, err, true), 'error', - { file: normalizeFilePathForGithubAnnotation(expectFilePath), line: 1, col: 1 }, + { file: expectFilePath, line: 1, col: 1 }, )); } else { failureSummary.add(testCaseLabel); @@ -366,7 +366,7 @@ export default function runner(currentPlugin: PluginCreator) { console.log(formatGitHubActionAnnotation( formatWarningsAssertError(testCaseLabel, testCaseOptions, result.warnings(), testCaseOptions.warnings, true), 'error', - { file: normalizeFilePathForGithubAnnotation(expectFilePath), line: 1, col: 1 }, + { file: expectFilePath, line: 1, col: 1 }, )); } else { failureSummary.add(testCaseLabel); @@ -390,22 +390,3 @@ export default function runner(currentPlugin: PluginCreator) { console.warn('pass ' + (currentPlugin() as Plugin).postcssPlugin); }; } - -function normalizeFilePathForGithubAnnotation(filePath: string) { - // Any plugin or packages is located in `/`; - const parts = process.cwd().split('/').slice(-2); - - const workspaces = [ - 'packages', - 'plugins', - 'plugin-packs', - 'cli', - 'experimental', - ]; - - if (!workspaces.includes(parts[0])) { - throw new Error('PostCSS Tape was intended to be run from /'); - } - - return path.join(parts.join('/'), filePath); -}