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

CJS build for credential provider references undefined __importStar #5750

Closed
3 tasks done
nonken opened this issue Jan 31, 2024 · 5 comments
Closed
3 tasks done

CJS build for credential provider references undefined __importStar #5750

nonken opened this issue Jan 31, 2024 · 5 comments
Assignees
Labels
bug This issue is a bug. closed-for-staleness p1 This is a high priority issue

Comments

@nonken
Copy link

nonken commented Jan 31, 2024

Checkboxes for prior research

Describe the bug

const { AssumeRoleCommand, STSClient } = await import("./loadSts");

This line await imports a dependency at runtime. Wen the code gets compiled down to CJS, it results in the following code:

const { AssumeRoleCommand, STSClient } = await Promise.resolve().then(() => __importStar(require("./loadSts")));

In the compiled file fromTemporaryCredentials.csj there is no __importStar to be found.
When manually adding

const {__importStar} = require('tslib');

at the top of the file it will work.

SDK version number

@aws-sdk/[email protected]

Which JavaScript Runtime is this issue in?

Node.js

Details of the browser/Node.js/ReactNative version

v20.11.0

Reproduction Steps

  • Create a nodejs project that uses cjs.
  • Create a build using esbuild.
  • Run the service and it will fail.

Observed Behavior

        const { AssumeRoleCommand, STSClient: STSClient2 } = await Promise.resolve().then(() => __importStar(require_loadSts2()));
                                                                                                ^

ReferenceError: __importStar is not defined

Expected Behavior

I expect it to work.

Possible Solution

const {__importStar} = require('tslib');

Add this to the file and it works, but this is after it was built.

Additional Information/Context

No response

@nonken nonken added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Jan 31, 2024
@kuhe kuhe self-assigned this Jan 31, 2024
@kuhe kuhe added p1 This is a high priority issue and removed needs-triage This issue or PR still needs to be triaged. labels Jan 31, 2024
@nonken
Copy link
Author

nonken commented Jan 31, 2024

It seems like you can workaround this issue by doing the following when using esbuild:

const esbuild = require('esbuild');
const fs = require('fs');

//
// We are injecting __importStar into the credential loader of the aws-sdk.
// This because they have a bug when building their cjs dist.
//
// https://github.com/aws/aws-sdk-js-v3/issues/5750
//
// Once this is resolved we can remove this.
//
const tslibInjectorPlugin = {
  name: 'tslib-injector',
  setup(build) {
    build.onLoad({filter: /fromTemporaryCredentials\.js$/}, async (args) => {
      const contents = await fs.promises.readFile(args.path, 'utf8');

      if (!contents.includes("require('tslib')")) {
        const modifiedContents = `const { __importStar } = require('tslib');\n${contents}`;
        return {contents: modifiedContents, loader: 'js'};
      }

      return null;
    });
  },
};

esbuild
  .build({
    entryPoints: ['src/server.js'], // Adjust as per your entry file
    bundle: true,
    platform: 'node',
    outfile: 'dist/server.js', // Adjust as per your desired output file
    plugins: [tslibInjectorPlugin],
  })
  .catch(() => process.exit(1));

@kuhe kuhe added the pending-release This issue will be fixed by an approved PR that hasn't been released yet. label Jan 31, 2024
@kuhe
Copy link
Contributor

kuhe commented Jan 31, 2024

We expect to include this fix in the next release. There was a correction needed in an old TSConfig file that was previously undetected.

@nonken
Copy link
Author

nonken commented Feb 1, 2024

We expect to include this fix in the next release. There was a correction needed in an old TSConfig file that was previously undetected.

Thank you ser, lightspeed

@kuhe
Copy link
Contributor

kuhe commented Feb 1, 2024

A patch was released in https://www.npmjs.com/package/@aws-sdk/credential-providers/v/3.504.1

@kuhe kuhe added closing-soon This issue will automatically close in 4 days unless further comments are made. and removed pending-release This issue will be fixed by an approved PR that hasn't been released yet. labels Feb 1, 2024
@github-actions github-actions bot added closed-for-staleness and removed closing-soon This issue will automatically close in 4 days unless further comments are made. labels Feb 6, 2024
@github-actions github-actions bot closed this as completed Feb 6, 2024
Copy link

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs and link to relevant comments in this thread.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 20, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug This issue is a bug. closed-for-staleness p1 This is a high priority issue
Projects
None yet
Development

No branches or pull requests

2 participants