Skip to content

Panic when using subpath imports for bundled browser build #3067

Closed
@znewsham

Description

I'm trying to use node's subpath imports syntax as follows to get around the issue of absolute imports not being relative to the project root (the reason for using the subpath isn't super relevant). Somewhat confusingly the path I'm trying to import is /imports - which I use #imports/* to represent. Note - I've already checked #2913 - I don't think it's relevant as I'm already on 0.17.17

{
  ...
  imports: {
    "#imports/*": "./imports/*"
  }
  ...
}

This works just fine when doing a server build - but not when doing a client build. Both use the bundle option (however the server build does do some very custom queueing work). On the client build I get the following error:

✘ [ERROR] panic: runtime error: invalid memory address or nil pointer dereference (while parsing "client/main.ts")

  debug.Stack (runtime/debug/stack.go:24)
  helpers.PrettyPrintedStack (internal/helpers/stack.go:9)
  bundler.parseFile.func1 (internal/bundler/bundler.go:189)
  panic (runtime/panic.go:884)
  resolver.resolverQuery.finalizeImportsExportsResult (internal/resolver/resolver.go:2420)
  resolver.resolverQuery.loadPackageImports (internal/resolver/resolver.go:2032)
  resolver.resolverQuery.loadNodeModules (internal/resolver/resolver.go:2113)
  resolver.resolverQuery.resolveWithoutRemapping (internal/resolver/resolver.go:896)
  resolver.resolverQuery.resolveWithoutSymlinks (internal/resolver/resolver.go:883)
  resolver.(*Resolver).Resolve (internal/resolver/resolver.go:512)
  bundler.RunOnResolvePlugins (internal/bundler/bundler.go:871)
  bundler.parseFile (internal/bundler/bundler.go:393)
  bundler.(*scanner).maybeParseFile (internal/bundler/bundler.go:1368)

The triggering import (in client/main.ts) looks like this:

import "#imports/startup"

Interestingly, if I change the import to #imports/startup/index.ts - it works - Note: index.ts is required - #imports/startup/index fails with a different error.

The arguments I'm passing to esbuild are as follows (abridged)

  const context = await esbuild.context({
    absWorkingDir: process.cwd(), 
    minify: isProduction,
    entryPoints: [entryPoint],
    outdir,
    preserveSymlinks: true,
    conditions: [isProduction ? 'production' : 'development', archName],
    external: [
      '*.jpg',
      '*.png',
      '*.svg',
      '/fonts/*',
    ],
    sourcemap: 'linked',
    logLevel: 'error',
    define: {
      ...
    },
    plugins: [
      addJsExtension(buildRoot), // potentially relevant - but explicitly ignores those imports that start with . / or #
      onStart,
      onEnd
    ],
    splitting: true, // same behaviour false too
    bundle: true,
    format: archName === 'web.browser' ? 'esm' : 'iife',
    metafile: true,
  });

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions