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

fix(core): prevent breaking error when registering transpiler #16929

Merged
merged 9 commits into from
May 12, 2023

Conversation

passbyval
Copy link
Contributor

@passbyval passbyval commented May 10, 2023

Current Behavior

Some versions of @swc-node/register do not return a cleanup function, causing registerTsProject to call an undefined function:

 >  NX   Failed to load plugin '@nx' declared in 'apps/nextgen-ui/.eslintrc.json': fn is not a function

@swc-node/register 1.6.5 vs 1.4.2 for reference.

Expected Behavior

To check @swc-node/register's return value, and return a no-op for versions that do not have a cleanup function.

@vercel
Copy link

vercel bot commented May 10, 2023

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

Name Status Preview Comments Updated (UTC)
nx-dev ✅ Ready (Inspect) Visit Preview 💬 Add feedback May 12, 2023 4:40pm

@passbyval passbyval changed the title Fix error when running eslint after using @nx/linter:workspace-rule generator fix(linter) error when running eslint after using @nx/linter:workspace-rule generator May 10, 2023
@passbyval passbyval changed the title fix(linter) error when running eslint after using @nx/linter:workspace-rule generator [WIP] fix(linter) error when running eslint after using @nx/linter:workspace-rule generator May 10, 2023
@passbyval passbyval changed the title [WIP] fix(linter) error when running eslint after using @nx/linter:workspace-rule generator [WIP] fix(js) error when running eslint after using @nx/linter:workspace-rule generator May 11, 2023
@passbyval passbyval changed the title [WIP] fix(js) error when running eslint after using @nx/linter:workspace-rule generator fix(js): prevent breaking error when registering transpiler May 11, 2023
@passbyval passbyval changed the title fix(js): prevent breaking error when registering transpiler fix(nx-plugin): prevent breaking error when registering transpiler May 11, 2023
prevent 'fn is not a function' error when running registerTranspiler.

depending on the version, @swc-node/register does not always return a cleanup function, so an
explicit check for this is now added.
Comment on lines 50 to 55
const cleanupFn = register(compilerOptions);
const isFunction = typeof cleanupFn === 'function';

if (!isFunction) return () => {};

return cleanupFn;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Here is the fix.

The rest of the PR is purely refactor to avoid variable reassignment and reduce the amount of nesting. e.g. early returns.

Copy link
Member

Choose a reason for hiding this comment

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

This makes sense, swapping to a ternary to avoid extra variables

Comment on lines 42 to 48
type ISwcRegister = typeof import('@swc-node/register/register')['register'];
type ISwcRegisterVoid = (...args: Parameters<ISwcRegister>) => void;

// These are requires to prevent it from registering when it shouldn't
const register = require('@swc-node/register/register').register as
| ISwcRegister
| ISwcRegisterVoid;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Add explicit type-checking to account for versions of @swc-node/register that do not return cleanup functions.

Copy link
Member

Choose a reason for hiding this comment

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

I'm removing some of this, since we are removing the strict portion it doesn't end up helping and looks a bit confusing

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Makes sense without strict null checks 👍

@@ -35,6 +35,59 @@ export const registerTsProject = (
};
};

export function getSwcTranspiler(
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Needed to enable strictNullChecks otherwise TypeScript was allowing this function to return null/undefined even when the return type was a function.

Copy link
Member

Choose a reason for hiding this comment

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

I hear you, and I agree, but I don't think we should do this via an extra tsconfig, since that won't get checked at build time.

We'd eventually like to enable strict mode for the nx package globally, but can't just yet. I've removed this part of the changes.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sounds good, and strict mode is a great idea for the entire project.

@AgentEnder AgentEnder changed the title fix(nx-plugin): prevent breaking error when registering transpiler fix(core): prevent breaking error when registering transpiler May 12, 2023
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.

Thanks for the fix @passbyval! I've made some changes and it looks good to go 🎉

@@ -35,6 +35,59 @@ export const registerTsProject = (
};
};

export function getSwcTranspiler(
Copy link
Member

Choose a reason for hiding this comment

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

I hear you, and I agree, but I don't think we should do this via an extra tsconfig, since that won't get checked at build time.

We'd eventually like to enable strict mode for the nx package globally, but can't just yet. I've removed this part of the changes.

Comment on lines 42 to 48
type ISwcRegister = typeof import('@swc-node/register/register')['register'];
type ISwcRegisterVoid = (...args: Parameters<ISwcRegister>) => void;

// These are requires to prevent it from registering when it shouldn't
const register = require('@swc-node/register/register').register as
| ISwcRegister
| ISwcRegisterVoid;
Copy link
Member

Choose a reason for hiding this comment

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

I'm removing some of this, since we are removing the strict portion it doesn't end up helping and looks a bit confusing

Comment on lines 50 to 55
const cleanupFn = register(compilerOptions);
const isFunction = typeof cleanupFn === 'function';

if (!isFunction) return () => {};

return cleanupFn;
Copy link
Member

Choose a reason for hiding this comment

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

This makes sense, swapping to a ternary to avoid extra variables

@AgentEnder AgentEnder merged commit 8460c63 into nrwl:master May 12, 2023
@passbyval passbyval deleted the patch-1 branch May 12, 2023 22:32
@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 May 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.

2 participants