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: require.main.filename check in DefaultCredentialManager #2297

Merged
merged 5 commits into from
Oct 10, 2024

Conversation

traeok
Copy link
Member

@traeok traeok commented Oct 8, 2024

What It Does

In some environments (namely ESM and the Node.js REPL), the main property is not always available within the require object, so the if check for require.main.filename != null results in a TypeError.

How to Test

This one is a bit difficult to test, but this should work as an example:

  • Make an empty folder and set up a basic NPM project using npm init

  • Build a TGZ for @zowe/imperative using npm install && npm run build && npm pack in the packages/imperative folder. Install the generated tarball in this new NPM project alongside @zowe/secrets-for-zowe-sdk as normal dependencies:

    • npm install zowe-imperative-8.1.0.tgz @zowe/secrets-for-zowe-sdk
  • Open the Node.js REPL within this folder: node -i

  • Import DefaultCredentialManager within the REPL and create a new instance of it. Then attempt to initialize it. Initialization should be successful with no errors thrown:

    > const { DefaultCredentialManager } = await import("@zowe/imperative")
    > const credMgr = new DefaultCredentialManager()
    > await credMgr.initialize()

Review Checklist
I certify that I have:

Copy link

codecov bot commented Oct 8, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 91.06%. Comparing base (a8a90f8) to head (4279a63).
Report is 7 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #2297   +/-   ##
=======================================
  Coverage   91.06%   91.06%           
=======================================
  Files         628      628           
  Lines       17867    17867           
  Branches     3735     3805   +70     
=======================================
  Hits        16271    16271           
  Misses       1595     1595           
  Partials        1        1           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@traeok traeok marked this pull request as ready for review October 8, 2024 22:50
@adam-wolfe adam-wolfe added this to the V3.1.0 milestone Oct 9, 2024
Copy link

Copy link
Member

@t1m0thyj t1m0thyj left a comment

Choose a reason for hiding this comment

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

LGTM, thanks @traeok!

if (require.main.filename != null) {
requireOpts.paths = [require.main.filename, ...require.resolve.paths("@zowe/secrets-for-zowe-sdk")];
}
requireOpts.paths = [require.main?.filename, ...require.resolve.paths("@zowe/secrets-for-zowe-sdk")].filter(Boolean);
Copy link
Member

Choose a reason for hiding this comment

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

Technically this changes the original behavior so that we always pass in a list of paths to require.resolve, but I don't think this change should be impactful 😋

Copy link
Member Author

@traeok traeok Oct 10, 2024

Choose a reason for hiding this comment

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

This is a good point - I don't want to make any assumptions, and its possible that the default module resolution is special for certain projects. So, I've updated this to add optional chaining to the if check so that we only apply the paths if a filename is provided in require.main 😋 that way we keep the same behavior as before, but it still resolves the TypeError encountered in the attached issue.

@traeok traeok merged commit 4d2539a into master Oct 10, 2024
17 checks passed
@traeok traeok deleted the fix/imperative/cred-mgr-check branch October 10, 2024 13:41
@traeok traeok added the release-patch Indicates a patch to existing code has been applied label Oct 10, 2024
Copy link

Release succeeded for the master branch. 🎉

The following packages have been published:

Powered by Octorelease 🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
release-patch Indicates a patch to existing code has been applied released
Projects
Status: Closed
Development

Successfully merging this pull request may close these issues.

ESM and REPL environments hit exception in DefaultCredentialManager.initialize
5 participants