Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(core): throw graceful error in runNxMigration() if no collection #10655

Merged
merged 4 commits into from Jun 10, 2022

Conversation

trakhimenok
Copy link
Contributor

@trakhimenok trakhimenok commented Jun 9, 2022

This will give better context to user when getting an error described in "current behaviour".

Current Behavior

For some unclear reason getting an error when running pnpm exec nx migrate --run-migrations

Running migration remove-show-circular-dependencies-option
Cannot read properties of undefined (reading 'implementation')
TypeError: Cannot read properties of undefined (reading 'implementation')
    at /private/var/folders/93/m19tdtfs4j99996mcr4t434c0000gn/T/tmp-41094-b12xESmQUVHD/node_modules/.pnpm/nx@14.2.2/node_modules/nx/src/command-line/migrate.js:706:42
    at Generator.next (<anonymous>)
    at /private/var/folders/93/m19tdtfs4j99996mcr4t434c0000gn/T/tmp-41094-b12xESmQUVHD/node_modules/.pnpm/tslib@2.4.0/node_modules/tslib/tslib.js:118:75
    at new Promise (<anonymous>)
    at Object.__awaiter (/private/var/folders/93/m19tdtfs4j99996mcr4t434c0000gn/T/tmp-41094-b12xESmQUVHD/node_modules/.pnpm/tslib@2.4.0/node_modules/tslib/tslib.js:114:16)
    at runNxMigration (/private/var/folders/93/m19tdtfs4j99996mcr4t434c0000gn/T/tmp-41094-b12xESmQUVHD/node_modules/.pnpm/nx@14.2.2/node_modules/nx/src/command-line/migrate.js:702:20)
    at /private/var/folders/93/m19tdtfs4j99996mcr4t434c0000gn/T/tmp-41094-b12xESmQUVHD/node_modules/.pnpm/nx@14.2.2/node_modules/nx/src/command-line/migrate.js:617:23
    at Generator.next (<anonymous>)
    at fulfilled (/private/var/folders/93/m19tdtfs4j99996mcr4t434c0000gn/T/tmp-41094-b12xESmQUVHD/node_modules/.pnpm/tslib@2.4.0/node_modules/tslib/tslib.js:115:62)
Command failed: /var/folders/93/m19tdtfs4j99996mcr4t434c0000gn/T/tmp-41094-b12xESmQUVHD/node_modules/.bin/nx _migrate --run-migrations --verbose

Expected Behavior

Expected to get context on where is the problem so might be able to fix.

Related Issue(s)

Fixes #

@nx-cloud
Copy link

nx-cloud bot commented Jun 9, 2022

☁️ Nx Cloud Report

CI is running/has finished running commands for commit 9765504. As they complete they will appear below. Click to see the status, the terminal output, and the build insights.

📂 See all runs for this branch


✅ Successfully ran 12 targets

Sent with 💌 from NxCloud.

@vercel
Copy link

vercel bot commented Jun 9, 2022

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated
nx-dev ✅ Ready (Inspect) Visit Preview Jun 9, 2022 at 8:15PM (UTC)

@trakhimenok trakhimenok changed the title feat(nx): throw graceful error in runNxMigration() if no collection feat(core): throw graceful error in runNxMigration() if no collection Jun 9, 2022
Copy link
Member

@AgentEnder AgentEnder left a comment

Choose a reason for hiding this comment

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

Hey @trakhimenok! Some better messaging in this case would definitely make sense, although it is likely an issue caused by the migration's author.

We recently introduced plugin lint checks that should reduce the likelihood of this occurring, but better messaging still makes sense.

I think try ... catch may be the wrong control flow for this though, do you care to take a look at my suggestion?

Comment on lines 1045 to 1052
try {
const c = g[name];
implRelativePath = c.implementation || c.factory;
} catch (e) {
throw new Error(
`Can not get relative path for migration's implementation as collection not found by name="${name}" for package="${packageName}", collectionPath="${collectionPath}"`
);
}
Copy link
Member

Choose a reason for hiding this comment

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

Rather than a try catch here, we could do something like this:

Suggested change
try {
const c = g[name];
implRelativePath = c.implementation || c.factory;
} catch (e) {
throw new Error(
`Can not get relative path for migration's implementation as collection not found by name="${name}" for package="${packageName}", collectionPath="${collectionPath}"`
);
}
const c = g[name];
implRelativePath = g[name]?.implementation || g[name]?.factory
if (!implRelativePath) {
throw new Error(
`Unable to determine relative path for migration implementation: { "name": "${name}", "package": "${packageName}", "collectionPath": "${collectionPath}"`
);
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@AgentEnder addressed

const implRelativePath = g[name]?.implementation || g[name]?.factory;
if (!implRelativePath) {
throw new Error(
`Can not get relative path for migration's implementation as collection not found by name="${name}" for package="${packageName}", collectionPath="${collectionPath}"`
Copy link
Member

Choose a reason for hiding this comment

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

This messaging is still not quite accurate. The collection was found, but the implementation was not. I'd recommend something like this:

Suggested change
`Can not get relative path for migration's implementation as collection not found by name="${name}" for package="${packageName}", collectionPath="${collectionPath}"`
`Unable to determine implementation path for ${collectionPath}:${name}`

The collection:generator pattern matches expectations from nx g ..., so I think its more readable.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Have a look at new message? I've added also indication if generators or schematics was used.

as requested in PR review
@github-actions
Copy link

This pull request has already been merged/closed. If you experience issues related to these changes, please open a new issue referencing this pull request.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 18, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants