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 downstream ESM imports, fixes #6573 #6600

Merged

Conversation

rzyns
Copy link
Contributor

@rzyns rzyns commented Oct 24, 2023

Uses default import for importing keytar (a CJS module), which is really the only foolproof way of importing CJS from an ESM context.

I confirmed by creating a simple package:

// msal-node-extensions/foo/package.json
{ "type": "module" }
// msal-node-extensions/foo/index.js
import * as msal from "../dist/index.mjs";
msal;

Running node foo/index.mjs on dev gives the following error:

file:///.../microsoft-authentication-library-for-js/extensions/msal-node-extensions/dist/persistence/KeychainPersistence.mjs:3
import { setPassword, getPassword, deletePassword } from 'keytar';
                                   ^^^^^^^^^^^^^^
SyntaxError: Named export 'deletePassword' not found. The requested module 'keytar' is a CommonJS module, which may not support all module.exports as named exports.
CommonJS modules can always be imported via the default export, for example using:

import pkg from 'keytar';
const { setPassword, getPassword, deletePassword } = pkg;

    at ModuleJob._instantiate (node:internal/modules/esm/module_job:124:21)
    at async ModuleJob.run (node:internal/modules/esm/module_job:190:5)

Node.js v18.17.1

Running the same command on this branch succeeds (no output, exit code 0).

Not sure of a good/reliable way to encode that in an automated test, but I'd be happy to add one if anyone has any suggestions

@github-actions github-actions bot added the extensions Related to extensions for the base libraries label Oct 24, 2023
@rzyns rzyns marked this pull request as draft October 24, 2023 00:29
@rzyns rzyns marked this pull request as ready for review October 24, 2023 00:33
@codecov-commenter
Copy link

codecov-commenter commented Oct 24, 2023

Codecov Report

Merging #6600 (56b0d0f) into dev (81d34b4) will decrease coverage by 16.32%.
Report is 360 commits behind head on dev.
The diff coverage is 61.83%.

Flag Coverage Δ
msal-angular ?
msal-browser ?
msal-common ?
msal-core ?
msal-node ?
msal-node-extensions 68.51% <61.57%> (-7.13%) ⬇️
msal-react ?
node-token-validation ?
Files Coverage Δ
extensions/msal-node-extensions/src/Dpapi.ts 100.00% <100.00%> (ø)
.../msal-node-extensions/src/error/NativeAuthError.ts 100.00% <100.00%> (ø)
extensions/msal-node-extensions/src/index.ts 100.00% <100.00%> (ø)
...nsions/msal-node-extensions/src/packageMetadata.ts 100.00% <100.00%> (+100.00%) ⬆️
...-extensions/src/persistence/DataProtectionScope.ts 100.00% <100.00%> (ø)
...nsions/msal-node-extensions/src/utils/Constants.ts 100.00% <100.00%> (ø)
...sions/msal-node-extensions/src/utils/TypeGuards.ts 100.00% <100.00%> (ø)
...msal-node-extensions/src/error/PersistenceError.ts 62.50% <66.66%> (-11.58%) ⬇️
...node-extensions/src/persistence/BasePersistence.ts 64.28% <50.00%> (-2.39%) ⬇️
...e-extensions/src/persistence/PersistenceCreator.ts 91.42% <92.85%> (+0.25%) ⬆️
... and 8 more

... and 214 files with indirect coverage changes

Copy link
Collaborator

@tnorling tnorling 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 PR!

@konstantin-msft
Copy link
Collaborator

@rzyns please rebase onto base branch so we can merge this PR.

rzyns added 2 commits October 26, 2023 11:08
Uses default import for importing keytar (a CJS module), which is really
the only foolproof way of importing CJS from an ESM context
@rzyns rzyns force-pushed the fix-keytar-esm-import-error branch from 56b0d0f to 96d1950 Compare October 26, 2023 15:08
@tnorling tnorling merged commit ffe542b into AzureAD:dev Oct 27, 2023
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
extensions Related to extensions for the base libraries
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants