diff --git a/.changeset/long-camels-provide.md b/.changeset/long-camels-provide.md new file mode 100644 index 0000000000..3ebee75fc2 --- /dev/null +++ b/.changeset/long-camels-provide.md @@ -0,0 +1,5 @@ +--- +'@backstage/cli': patch +--- + +Add check to make sure that the `--link` option for the `start` command is a valid workspace. diff --git a/packages/cli/src/commands/start/command.ts b/packages/cli/src/commands/start/command.ts index 10dc19cad8..addae78fb1 100644 --- a/packages/cli/src/commands/start/command.ts +++ b/packages/cli/src/commands/start/command.ts @@ -14,15 +14,41 @@ * limitations under the License. */ +import fs from 'fs-extra'; import { OptionValues } from 'commander'; +import { resolve as resolvePath } from 'node:path'; import { PackageRole } from '@backstage/cli-node'; import { findRoleFromCommand } from '../../lib/role'; import { startBackend, startBackendPlugin } from './startBackend'; import { startFrontend } from './startFrontend'; +import { ForwardedError } from '@backstage/errors'; export async function command(opts: OptionValues): Promise { const role = await findRoleFromCommand(opts); + if (opts.link) { + const dir = resolvePath(opts.link); + if (!fs.pathExistsSync(dir)) { + throw new Error( + `Invalid workspace link, directory does not exist: ${dir}`, + ); + } + const pkgJson = await fs + .readJson(resolvePath(dir, 'package.json')) + .catch(error => { + throw new ForwardedError( + 'Failed to read package.json in linked workspace', + error, + ); + }); + + if (!pkgJson.workspaces) { + throw new Error( + `Invalid workspace link, directory is not a workspace: ${dir}`, + ); + } + } + const options = { configPaths: opts.config as string[], checksEnabled: Boolean(opts.check),