Skip to content

Improve developer experience by changing message "failed to load module for ssr" #10002

Closed

Description

@lloydjatkinson

Description

As an indirect user of Vite by using Astro, I would like the failed to load module for ssr to express why the module failed to load.

I believe there are multiple conditions that can trigger this - the one I experience often is a wrong path to a file. It's only through past experience of this error that I know, in my case at least, that this usually means the file was not found.

Suggested solution

const result =
mod.ssrTransformResult ||
(await transformRequest(url, server, { ssr: true }))
if (!result) {
// TODO more info? is this even necessary?
throw new Error(`failed to load module for ssr: ${url}`)
}

Complete the TODO and display a more informative message stating why the module failed to load.

Alternative

No response

Additional context

No response

Validations

Activity

  1. bluwy commented on Sep 6, 2022

    @bluwy
    Member

    Hi, can you provide a repro to test this one out? I tried to bring this edgecase out in the past but couldn't get it to reach the error.

  2. added
    p2-nice-to-have
    and removed on Oct 24, 2022
  3. bluwy commented on Oct 24, 2022

    @bluwy
    Member
  4. self-assigned this
    on Oct 24, 2022
  5. Princesseuh commented on Nov 9, 2022

    @Princesseuh
    Contributor

    This error can also happen when a type is imported without import type, as shown in withastro/astro#5333

    As a downstream person in this, the information I'd love to have when this error happen are the following:

    • Where the error actually happened (including line numbers, if possible), in Astro we sometimes have this error when loading a .astro file that loads another file where the issue is caused and we currently can't know where the error happened exactly, just that it happened while loading a .astro module
    • Why the error happened, did the file not exist? Did it get incorrectly bundled because of a missing type-only import?

    I know this issue is assigned to @bluwy, but just in case I'm definitely willing to contribute to this, though I might need a bit of guidance on Vite's internals.

  6. bluwy commented on Nov 9, 2022

    @bluwy
    Member

    I haven't started yet so feel free to take it too. The issue is likely that transformRequest (and some child function) is swallowing the error

    (await transformRequest(url, server, { ssr: true }))

    Based on the behaviour so far, where importing something wrongly causes the vague error, I'm guessing it's failing the runtime import phase

    const ssrImport = async (dep: string) => {
    if (dep[0] !== '.' && dep[0] !== '/') {
    return nodeImport(dep, mod.file!, resolveOptions)
    }
    // convert to rollup URL because `pendingImports`, `moduleGraph.urlToModuleMap` requires that
    dep = unwrapId(dep)
    if (!isCircular(dep) && !pendingImports.get(dep)?.some(isCircular)) {
    pendingDeps.push(dep)
    if (pendingDeps.length === 1) {
    pendingImports.set(url, pendingDeps)
    }
    const mod = await ssrLoadModule(
    dep,
    server,
    context,
    urlStack,
    fixStacktrace
    )
    if (pendingDeps.length === 1) {
    pendingImports.delete(url)
    } else {
    pendingDeps.splice(pendingDeps.indexOf(dep), 1)
    }
    // return local module to avoid race condition #5470
    return mod
    }
    return moduleGraph.urlToModuleMap.get(dep)?.ssrModule
    }

    The ssrImport is called for each import bla from 'ha', by transforming the imports into a runtime ssrImport call, through

    async function ssrTransformScript(

    Happy to chat and sync to go through the internals! May be better to run the debugger too to see where it's silently fail in case i'm making a wrong guess 😅

  7. locked and limited conversation to collaborators on Nov 27, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

p2-nice-to-have

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions