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

[BUG] Node sub dependency not resolving #399

Open
emilygarner opened this issue Dec 24, 2024 · 17 comments
Open

[BUG] Node sub dependency not resolving #399

emilygarner opened this issue Dec 24, 2024 · 17 comments
Labels
bundling known issue related to bundling needs info not enough information to reproduce or continue

Comments

@emilygarner
Copy link

The deno-slack versions

"deno-slack-hub/": "https://deno.land/x/[email protected]/",
"deno-slack-sdk/": "https://deno.land/x/[email protected]/",
"deno-slack-api/": "https://deno.land/x/[email protected]/",

Deno runtime version

deno 2.1.4 (stable, release, aarch64-apple-darwin)
v8 13.0.245.12-rusty
typescript 5.6.2

OS info

ProductName: macOS
ProductVersion: 14.3
BuildVersion: 23D56
Darwin Kernel Version 23.3.0: Wed Dec 20 21:30:44 PST 2023; root:xnu-10002.81.5~7/RELEASE_ARM64_T6000

Describe the bug

One of my slack functions is not running after deployment. On "slack run" it works perfect, but after deploying my workflow to slack it will not run. I get the following error:
Function output: Caught error from user supplied module: TypeError: Relative import path "querystring" not prefixed with / or ./ or ../ If you want to use a built-in Node module, add a "node:" prefix (ex. "node:querystring").
This error is strange because I am not using querystring anywhere in my program, so I believe it is a sub dependency of another npm package I am using. However, even after adding "querystring : node:querystring" to my import map AND scopes, the problem still persists. Any help on this would be greatly appreciated.

@WilliamBergamin
Copy link
Contributor

Hi @emilygarner thanks for writing in!

In short when running slack deploy the SDK will bundle your functions locally and send the bundled versions to Slack. In the past we've observed some bundling issues, where dependencies cannot be resolved 🤔

It seems like you may need to specifically configure the subdependency for the HTTPS import used by the parent dependency. This may be tricky, I suspect that specifying querystring in the scopes of your import configuration may not be sufficient. You may need to determine the HTTPS reference used by the parent dependency as the key in the scopes import configuration

Let me know if this helps resolve the issue, else would you be able to share some source code that allows me to reproduce your error?

@WilliamBergamin WilliamBergamin added needs info not enough information to reproduce or continue bundling known issue related to bundling labels Jan 6, 2025
@emilygarner
Copy link
Author

For the source code: The only npm package I am importing is jsforce, so that should help reproduce the issue. I am not sure how to specifically configure the HTTPS import for jsforce

@WilliamBergamin
Copy link
Contributor

WilliamBergamin commented Jan 6, 2025

I was able to reproduce this issue 👍

Ive opened this PR to improve support for npm libraries in our bundling logic. Unfortunately this does not seem to resolve your issue, even with these changes I am still seeing your error at runtime 🤔

It seems like the jsforce npm library you are using is currently incompatible with Deno Bundling and the run on slack infrastructure

@emilygarner
Copy link
Author

Is there any solution to this that we can use?

@WilliamBergamin
Copy link
Contributor

WilliamBergamin commented Jan 7, 2025

There are a few solutions that come to mind

  • You could opt to use the custom step functionality of the bolt-js framework, the bolt-js framework natively supports Node there should be no issues using jsforce as a dependency
    • You will need to define your workflows in the Workflow Builder instead of in code
    • You will need to host your application (heroku, aws, your own servers, etc...)
  • Fork the jsforce repo and embed the source code you are using within your project
    • This makes maintaining your project more complicated
  • Raise this issue to the jsforce maintainers to see if they could modify their project to support Deno Bundling

@emilygarner
Copy link
Author

Hi @WilliamBergamin Thank you for the suggestions, I am testing out a different nom package now to see if that will work but now I am getting an error when I run slack deploy. I am getting a ton of: The package "node:buffer" wasn't found on the file system but is built into node. Are you trying to bundle for node? You can use "platform: 'node'" to do that, which will remove this error. Do you know how I can fix this? Thank you.

@WilliamBergamin
Copy link
Contributor

WilliamBergamin commented Jan 7, 2025

Are you using the following "build" command in your slack.json?

{
  "hooks": {
    "get-hooks": "deno run -q --allow-read --allow-net https://deno.land/x/[email protected]/mod.ts",
    "build": "deno run -q --config=deno.jsonc --allow-read --allow-write --allow-net --allow-run --allow-env --allow-sys=osRelease https://raw.githubusercontent.com/slackapi/deno-slack-hooks/refs/heads/main/src/build.ts"
  }
}

Could you please share this new library so I can try to reproduce this error

@emilygarner
Copy link
Author

emilygarner commented Jan 7, 2025

Yes I used that as my slack.json and it is still not working.

I am importing "simple-oauth2" and "node-fetch"

@mroy-seedbox
Copy link

You shouldn't need node-fetch. It's natively included in Node.js since version 18.

@WilliamBergamin
Copy link
Contributor

WilliamBergamin commented Jan 7, 2025

Yess, I would also suggest using Deno fetch rather then importing an npm package

@emilygarner
Copy link
Author

Using that, I am able to deploy but get the following error
Caught error from user supplied module: ReferenceError: Buffer is not defined

@WilliamBergamin
Copy link
Contributor

Do you get this error when executing slack run as well or does everything work?

@emilygarner
Copy link
Author

Everything works with slack run

@WilliamBergamin
Copy link
Contributor

WilliamBergamin commented Jan 8, 2025

Would you be able share more context on how to reproduce this error?
The source code or sample code would be ideal

@emilygarner
Copy link
Author

simple-oauth2 is now the only npm package I am importing. The only other change is I am now using fetch to make a post request with headers.

@emilygarner
Copy link
Author

@WilliamBergamin Here is my source code for my function that is inside a try catch block:

  const sf_client = new ClientCredentials(config);
  const tokenParams = {
    scope: "",
  };
  const accessToken = await sf_client.getToken(tokenParams);
  const headers = new Headers({
    "Authorization": "Bearer " + accessToken.token["access_token"],
    "Content-type": "application/json; charset=UTF-8",
    Accept: "application/json, text/plain, */*",
  });

  const fetch_string = env["URL"] 

  const response = await fetch(fetch_string, {
    method: "POST",
    headers: headers,
    body: JSON.stringify({
      Subject: originalText,
      RecordTypeId: env["RECORDTYPE_ID"],
    }),
  });

import { ClientCredentials } from "simple-oauth2"; This is at top of file.

Again it works with slack run, not slack deploy

@WilliamBergamin
Copy link
Contributor

Some more context on the bundling behavior of this SDK. This SDK originally relied on the native deno bundle command to bundle projects and send the source code to the run on Slack infrastructure. The deno bundle command was deprecated from the runtime, due to the complexities associated with bundling (from what I understand). Deno recommended migrating to esbuild as an alternative to deno bundle. When support for npm packages was added to the Deno runtime, it caused support for bundling npm packages to lag behind.

I found this issue that seems to indicate that esbuild may not be able to properly bundle the simple-oauth2 library due to it using the Node Buffer without explicitly importing the package.

I would suggest hand rolling your OAuth flow or using a Deno native library like oauth2-client

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bundling known issue related to bundling needs info not enough information to reproduce or continue
Projects
None yet
Development

No branches or pull requests

3 participants