Merge pull request #30993 from rmartine-ias/yarn-plugin-error-detail

Add detail in error messages when yarn plugin can't detect backstage version
This commit is contained in:
Fredrik Adelöw
2025-09-09 16:47:04 +02:00
committed by GitHub
4 changed files with 19 additions and 13 deletions
+1
View File
@@ -31,6 +31,7 @@
},
"dependencies": {
"@backstage/cli-common": "workspace:^",
"@backstage/errors": "workspace:^",
"@backstage/release-manifests": "workspace:^",
"@yarnpkg/core": "^4.4.1",
"@yarnpkg/fslib": "^3.1.2",
@@ -73,17 +73,15 @@ describe('getCurrentBackstageVersion', () => {
});
it.each`
description | content
${'is missing'} | ${{}}
${'is invalid'} | ${{ 'backstage.json': '}{' }}
${'has missing version'} | ${{ 'backstage.json': '{"a":"b"}' }}
${'has invalid version'} | ${{ 'backstage.json': '{"version":"foobar"}' }}
`('throws if backstage.json $description', ({ content }) => {
description | content | message
${'is missing'} | ${{}} | ${/valid version string not found.*no such file/i}
${'is invalid'} | ${{ 'backstage.json': '}{' }} | ${/valid version string not found.*not valid json/i}
${'has missing version'} | ${{ 'backstage.json': '{"a":"b"}' }} | ${/valid version string not found.*version field is missing/i}
${'has invalid version'} | ${{ 'backstage.json': '{"version":"foobar"}' }} | ${/valid version string not found.*exists but is not valid semver/i}
`('throws if backstage.json $description', ({ content, message }) => {
mockDir.addContent(content);
expect(() => getCurrentBackstageVersion()).toThrow(
/valid version string not found/i,
);
expect(() => getCurrentBackstageVersion()).toThrow(message);
});
it('caches repeated calls', () => {
@@ -18,6 +18,7 @@ import assert from 'assert';
import { valid as semverValid } from 'semver';
import { ppath, xfs } from '@yarnpkg/fslib';
import { BACKSTAGE_JSON } from '@backstage/cli-common';
import { ForwardedError } from '@backstage/errors';
import { memoize } from './memoize';
import { getWorkspaceRoot } from './getWorkspaceRoot';
@@ -26,11 +27,16 @@ export const getCurrentBackstageVersion = memoize(() => {
let backstageVersion: string | null = null;
try {
backstageVersion = semverValid(xfs.readJsonSync(backstageJsonPath).version);
const backstageVersionRaw = xfs.readJsonSync(backstageJsonPath).version;
assert(backstageVersionRaw !== undefined, 'Version field is missing');
backstageVersion = semverValid(backstageVersionRaw);
assert(backstageVersion !== null);
} catch {
throw new Error('Valid version string not found in backstage.json');
assert(backstageVersion !== null, 'Version exists but is not valid semver');
} catch (err) {
throw new ForwardedError(
'Valid version string not found in backstage.json',
err,
);
}
return backstageVersion;
+1
View File
@@ -49876,6 +49876,7 @@ __metadata:
"@backstage/backend-test-utils": "workspace:^"
"@backstage/cli": "workspace:^"
"@backstage/cli-common": "workspace:^"
"@backstage/errors": "workspace:^"
"@backstage/release-manifests": "workspace:^"
"@yarnpkg/builder": "npm:^4.2.1"
"@yarnpkg/core": "npm:^4.4.1"