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

Astro could not resolve X #9777

Closed
3 tasks done
muuvmuuv opened this issue Dec 7, 2023 · 26 comments · Fixed by #9994 or #10099
Closed
3 tasks done

Astro could not resolve X #9777

muuvmuuv opened this issue Dec 7, 2023 · 26 comments · Fixed by #9994 or #10099
Assignees
Labels
Package: astro Issues related to the Sentry Astro SDK Type: Bug

Comments

@muuvmuuv
Copy link

muuvmuuv commented Dec 7, 2023

Is there an existing issue for this?

How do you use Sentry?

Sentry Saas (sentry.io)

Which SDK are you using?

@sentry/astro

SDK Version

7.85.0

Framework Version

4.0.3

Link to Sentry event

No response

SDK Setup

import cloudflare from "@astrojs/cloudflare"
import svelte from "@astrojs/svelte"
import tailwind from "@astrojs/tailwind"
import { defineConfig } from "astro/config"
import mkcert from "vite-plugin-mkcert"
import icons from "unplugin-icons/vite"
import sentry from "@sentry/astro"
import mdx from "@astrojs/mdx"
import spotlightjs from "@spotlightjs/astro"
import { loadEnv } from "vite"

import { version, name } from "./package.json" assert { type: "json" }
const { SENTRY_AUTH_TOKEN } = loadEnv(process.env.NODE_ENV, process.cwd(), "")

// https://astro.build/config
export default defineConfig({
	site: "XXXXXXX",
	integrations: [
		svelte(),
		tailwind(),
		mdx({
			optimize: true,
		}),
		sentry({
			dsn: "XXXXXXX",
			environment: process.env.NODE_ENV,
			release: `${name}@${version}`,
			sourceMapsUploadOptions: {
				project: "website",
				authToken: SENTRY_AUTH_TOKEN,
			},
		}),
		spotlightjs(),
	],
	markdown: {
		syntaxHighlight: "prism",
	},
	i18n: {
		defaultLocale: "de",
		locales: ["de", "es"],
	},
	experimental: {
		contentCollectionCache: true,
	},
	output: "server",
	adapter: cloudflare(),
	vite: {
		server: {
			https: true,
		},
		plugins: [
			mkcert(),
			icons({
				scale: 1.2,
				compiler: "svelte",
				autoInstall: true,
			}),
			icons({
				scale: 1.2,
				compiler: "astro",
				autoInstall: true,
			}),
		],
	},
})

Steps to Reproduce

  1. Create new Astro Sentry setup with pnpm create
  2. Setup sentry
  3. Do a build

Expected Result

No build errors.

Actual Result

✘ [ERROR] Could not resolve "util"

    node_modules/.pnpm/@sentry+node@7.86.0/node_modules/@sentry/node/esm/integrations/context.js:6:26:
      6 │ import { promisify } from 'util';
        ╵                           ~~~~~~

  The package "util" 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.

Could not resolve "child_process"
@getsantry getsantry bot moved this to Waiting for: Product Owner in GitHub Issues with 👀 Dec 7, 2023
@github-actions github-actions bot added the Package: astro Issues related to the Sentry Astro SDK label Dec 7, 2023
@Lms24
Copy link
Member

Lms24 commented Dec 11, 2023

Hi @muuvmuuv thanks for writing in! My suspicion is that this is related to using the Cloudflare adapter. Our Astro SDK is currently not compatible with non-Node runtimes such as Cloudflare workers or Vercel's Edge runtime. We need to eventually support this but think this will take some time. Will open an issue to track this and update docs.

To verify, if you, for example, switch to the Node adapter, does the build error disappear?

@muuvmuuv
Copy link
Author

Ah ok, didn't know the adapter does such things and interfer complete buidl processes but makes sense. Any idea what a workaround could look like? I will test the Node adapter later but my guess is that this works, I remember that builds worked before I installed cloudflare adapter a few month back but a lot has changed in the code base since then.

@getsantry getsantry bot moved this to Waiting for: Product Owner in GitHub Issues with 👀 Dec 11, 2023
@Lms24
Copy link
Member

Lms24 commented Dec 11, 2023

didn't know the adapter does such things and interfer complete buidl processes

I'm not totally sure it does either but this error really looks like it.

I might be off though. It's hard to debug this without more insights but I'd recommend going bottom-up or top-down and adding/removing your astro.config.mjs configurations until you can (no longer) reproduce the issue. I'd still start with the Node adapter so we can rule it out first.

@Lms24
Copy link
Member

Lms24 commented Dec 11, 2023

Any idea what a workaround could look like?

It's not ideal but you could essentially disable server-side instrumentation for @sentry/astro by creating an empty sentry.server.config.js|ts file. Maybe using toucan-js on the server side would work. It only works only with errors though but I guess that's better than nothing 😅

@muuvmuuv
Copy link
Author

So, the Node adapter works, and it is definitely the Sentry/astro plugin that causes the error together with Cloudflare adapter...

I put sentry.server.config.js in my project root, but that does nothing, unfortunately.

I am fine with not using Sentry on server side. Maybe an option for the plugin would make it so server code isn't bundled and produces these errors?

@getsantry getsantry bot moved this to Waiting for: Product Owner in GitHub Issues with 👀 Dec 12, 2023
@Lms24
Copy link
Member

Lms24 commented Dec 13, 2023

Maybe an option for the plugin would make it so server code isn't bundled and produces these errors?

Yes we could add this fairly easily. Thinking of an API like this one:

// astro.config.mjs
sentry({
  enabled: {
    client: true,
    server: false,
  }
  // or to enable/disable completely
  enabled: false // true by default
})

@florian-lefebvre
Copy link

florian-lefebvre commented Dec 28, 2023

AFAIK this is caused by node imports lacking the node: prefix, eg. (from cloudflare docs):

// Do this:
import { Buffer } from 'node:buffer';

// Do not do this:
import { Buffer } from 'buffer';

That would solve it!

@getsantry getsantry bot moved this to Waiting for: Product Owner in GitHub Issues with 👀 2 Dec 28, 2023
@lforst
Copy link
Member

lforst commented Dec 28, 2023

@florian-lefebvre We cannot use the node: prefix in our packages yet because we are still supporting node versions that don't support these prefixes.

@AbhiPrasad
Copy link
Member

Fix released with https://github.com/getsentry/sentry-javascript/releases/tag/7.92.0 - please give it a try

@F0rce
Copy link

F0rce commented Jan 4, 2024

After upgrading to "@sentry/astro": "^7.92.0", the issue still persists (I am using output server with the cloudflare() adapter.

Stacktrace

[sentry-vite-plugin] Info: Successfully uploaded source maps to Sentry
23:30:05 [vite]  built in 24.15s
23:30:05 
 finalizing server assets 

23:30:05 [build] Rearranging server assets...
 [ERROR] Could not resolve "fs"

    node_modules/.pnpm/@sentry+node@7.92.0/node_modules/@sentry/node/esm/utils.js:1:20:
      1  import * as fs from 'fs';
                             ~~~~

  The package "fs" 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.

✘ [ERROR] Could not resolve "os"

    node_modules/.pnpm/@sentry+node@7.92.0/node_modules/@sentry/node/esm/client.js:1:20:
      1 │ import * as os from 'os';
        ╵                     ~~~~

  The package "os" 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.

 [ERROR] Could not resolve "http"

    node_modules/.pnpm/@sentry+node@7.92.0/node_modules/@sentry/node/esm/transports/http.js:2:22:
      2  import * as http from 'http';
                               ~~~~~~
                               "./http"

  The package "http" 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.

✘ [ERROR] Could not resolve "fs"

    node_modules/.pnpm/@sentry+node@7.92.0/node_modules/@sentry/node/esm/integrations/modules.js:1:41:
      1 │ import { existsSync, readFileSync } from 'fs';
        ╵                                          ~~~~

  The package "fs" 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.

 [ERROR] Could not resolve "fs"

    node_modules/.pnpm/@sentry+node@7.92.0/node_modules/@sentry/node/esm/integrations/contextlines.js:2:25:
      2  import { readFile } from 'fs';
                                  ~~~~

  The package "fs" 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.

✘ [ERROR] Could not resolve "https"

    node_modules/.pnpm/@sentry+node@7.92.0/node_modules/@sentry/node/esm/transports/http.js:3:23:
      3 │ import * as https from 'https';
        ╵                        ~~~~~~~

  The package "https" 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.

 [ERROR] Could not resolve "child_process"

    node_modules/.pnpm/@sentry+node@7.92.0/node_modules/@sentry/node/esm/integrations/context.js:2:25:
      2  import { execFile } from 'child_process';
                                  ~~~~~~~~~~~~~~~

  The package "child_process" 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.

✘ [ERROR] Could not resolve "fs"

    node_modules/.pnpm/@sentry+node@7.92.0/node_modules/@sentry/node/esm/integrations/context.js:3:34:
      3 │ import { readFile, readdir } from 'fs';
        ╵                                   ~~~~

  The package "fs" 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.

 [ERROR] Could not resolve "inspector"

    node_modules/.pnpm/@sentry+node@7.92.0/node_modules/@sentry/node/esm/integrations/local-variables/local-variables-sync.js:73:32:
      73      const { Session } = require('inspector');
                                          ~~~~~~~~~~~

  The package "inspector" 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.

✘ [ERROR] Could not resolve "stream"

    node_modules/.pnpm/@sentry+node@7.92.0/node_modules/@sentry/node/esm/transports/http.js:4:25:
      4 │ import { Readable } from 'stream';
        ╵                          ~~~~~~~~

  The package "stream" 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.

 [ERROR] Could not resolve "url"

    node_modules/.pnpm/@sentry+node@7.92.0/node_modules/@sentry/node/esm/transports/http.js:5:20:
      5  import { URL } from 'url';
                             ~~~~~

  The package "url" 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.

✘ [ERROR] Could not resolve "zlib"

    node_modules/.pnpm/@sentry+node@7.92.0/node_modules/@sentry/node/esm/transports/http.js:6:27:
      6 │ import { createGzip } from 'zlib';
        ╵                            ~~~~~~

  The package "zlib" 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.

 [ERROR] Could not resolve "http"

    node_modules/.pnpm/@sentry+node@7.92.0/node_modules/@sentry/node/esm/integrations/spotlight.js:1:22:
      1  import * as http from 'http';
                               ~~~~~~
                               "./http"

  The package "http" 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.

✘ [ERROR] Could not resolve "os"

    node_modules/.pnpm/@sentry+node@7.92.0/node_modules/@sentry/node/esm/integrations/context.js:4:20:
      4 │ import * as os from 'os';
        ╵                     ~~~~

  The package "os" 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.

 [ERROR] Could not resolve "http"

    node_modules/.pnpm/@sentry+node@7.92.0/node_modules/@sentry/node/esm/integrations/http.js:58:31:
      58      const httpModule = require('http');
                                         ~~~~~~
                                         "./http"

  The package "http" 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.

✘ [ERROR] Could not resolve "domain"

    node_modules/.pnpm/@sentry+node@7.92.0/node_modules/@sentry/node/esm/async/domain.js:2:24:
      2 │ import * as domain from 'domain';
        │                         ~~~~~~~~
        ╵                         "./domain"

  The package "domain" 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.

 [ERROR] Could not resolve "net"

    node_modules/.pnpm/https-proxy-agent@5.0.1/node_modules/https-proxy-agent/dist/agent.js:15:38:
      15  const net_1 = __importDefault(require("net"));
                                                ~~~~~

  The package "net" 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.

✘ [ERROR] Could not resolve "url"

    node_modules/.pnpm/@sentry+node@7.92.0/node_modules/@sentry/node/esm/integrations/spotlight.js:2:20:
      2 │ import { URL } from 'url';
        ╵                     ~~~~~

  The package "url" 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.

 [ERROR] Could not resolve "async_hooks"

    node_modules/.pnpm/@sentry+node@7.92.0/node_modules/@sentry/node/esm/async/hooks.js:3:29:
      3  import * as async_hooks from 'async_hooks';
                                      ~~~~~~~~~~~~~

  The package "async_hooks" 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.

✘ [ERROR] Could not resolve "url"

    node_modules/.pnpm/@sentry+node@7.92.0/node_modules/@sentry/node/esm/integrations/anr/index.js:2:20:
      2 │ import { URL } from 'url';
        ╵                     ~~~~~

  The package "url" 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.

 [ERROR] Could not resolve "https"

    node_modules/.pnpm/@sentry+node@7.92.0/node_modules/@sentry/node/esm/integrations/http.js:73:34:
      73        const httpsModule = require('https');
                                            ~~~~~~~

  The package "https" 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.

✘ [ERROR] Could not resolve "url"

    node_modules/.pnpm/@sentry+node@7.92.0/node_modules/@sentry/node/esm/integrations/utils/http.js:2:20:
      2 │ import { URL } from 'url';
        ╵                     ~~~~~

  The package "url" 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.

 [ERROR] Could not resolve "inspector"

    node_modules/.pnpm/@sentry+node@7.92.0/node_modules/@sentry/node/esm/integrations/anr/index.js:99:30:
      99      const inspector = require('inspector');
                                        ~~~~~~~~~~~

  The package "inspector" 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.

✘ [ERROR] Could not resolve "tls"

    node_modules/.pnpm/https-proxy-agent@5.0.1/node_modules/https-proxy-agent/dist/agent.js:16:38:
      16 │ const tls_1 = __importDefault(require("tls"));
         ╵                                       ~~~~~

  The package "tls" 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.

 [ERROR] Could not resolve "url"

    node_modules/.pnpm/https-proxy-agent@5.0.1/node_modules/https-proxy-agent/dist/agent.js:17:38:
      17  const url_1 = __importDefault(require("url"));
                                                ~~~~~

  The package "url" 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.

✘ [ERROR] Could not resolve "assert"

    node_modules/.pnpm/https-proxy-agent@5.0.1/node_modules/https-proxy-agent/dist/agent.js:18:41:
      18 │ const assert_1 = __importDefault(require("assert"));
         ╵                                          ~~~~~~~~

  The package "assert" 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.

 [ERROR] Could not resolve "events"

    node_modules/.pnpm/agent-base@6.0.2/node_modules/agent-base/dist/src/index.js:5:25:
      5  const events_1 = require("events");
                                  ~~~~~~~~

  The package "events" 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.

Could not resolve "domain"
  Stack trace:

Am I missing something?

@anonrig
Copy link
Contributor

anonrig commented Jan 4, 2024

@F0rce Does the error persist with output: hybrid?

@F0rce
Copy link

F0rce commented Jan 4, 2024

@anonrig Yes, error persists using output: "hybrid

Stacktrace is exactly the same, so I did not attach it

node --version

✗ node --version
v20.10.0

@getsantry getsantry bot moved this to Waiting for: Product Owner in GitHub Issues with 👀 2 Jan 4, 2024
@Lms24
Copy link
Member

Lms24 commented Jan 8, 2024

@anonrig do you have further ideas?

For now, from my PoV, we're aware that Astro+Sentry doesn't work well on CF or Vercel Edge (which is why we also only guarantee that it works on Node-based server environments). If this is easily fixable, we can further investigate but full CF worker compatibility needs to be tackled when we choose to support Astro, Sveltekit, Remix, etc on None-node runtimes.

@anonrig
Copy link
Contributor

anonrig commented Jan 8, 2024

I'm working on it. Meanwhile, if you update your astro.config file to include the following object, it would fix your Cloudflare deploy.

vite: {
  ssr: {
    noExternal: ['@sentry/astro'],
  },
}

@muuvmuuv
Copy link
Author

muuvmuuv commented Jan 8, 2024

@anonrig that part is already in 7.92.0 so not needed. See https://github.com/getsentry/sentry-javascript/pull/9994/files

@getsantry getsantry bot moved this to Waiting for: Product Owner in GitHub Issues with 👀 2 Jan 8, 2024
@muuvmuuv
Copy link
Author

muuvmuuv commented Jan 8, 2024

Maybe an option for the plugin would make it so server code isn't bundled and produces these errors?

Yes we could add this fairly easily. Thinking of an API like this one:

// astro.config.mjs
sentry({
  enabled: {
    client: true,
    server: false, // ←
  }
  // or to enable/disable completely
  enabled: false // true by default
})

This works together with the latest release, thanks! I let it on you to close this because some other people seem to still have issues regarding this.

@anonrig
Copy link
Contributor

anonrig commented Jan 8, 2024

@anonrig that part is already in 7.92.0 so not needed. See https://github.com/getsentry/sentry-javascript/pull/9994/files

Yes, unfortunately, the proposed changes do not work. Here's a reproduction: anonrig/yagiz.co#61. We'll be adding Cloudflare integration and E2E testing to not regress in the future.

@AbhiPrasad
Copy link
Member

We released https://github.com/getsentry/sentry-javascript/releases/tag/7.93.0 with @anonrig latest changes - hopefully should fix everything!

@AbhiPrasad
Copy link
Member

Actually I've been told it doesn't fix everything - sorry for false alarm! Some more fixes on the way.

@F0rce
Copy link

F0rce commented Jan 10, 2024

@AbhiPrasad I just updated to the latest version ("@sentry/astro":"^7.93.0",) and it seems that your false alarm was false.

I just updated my astro.config.ts with #9777 (comment) (in version 7.92.0) after the update I removed this part from my astro.config.ts:

      // ...(isPreview
      //   ? {
      //       enabled: {
      //         server: false, // temp fix
      //       },
      //     }
      //   : {}),

I ran my build command and everything seems to be working perfectly fine. Just for reference, this is my full vite build log (GitHub Gist).

If you need more information, please let me know. I'll try to deploy now and see if cloudflare throws any weird errors, if so I'll update my comment.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Package: astro Issues related to the Sentry Astro SDK Type: Bug
Projects
Archived in project
Archived in project
8 participants