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

Rolldown should throw errors for implicit external modules #1174

Open
zhusjfaker opened this issue May 20, 2024 · 4 comments
Open

Rolldown should throw errors for implicit external modules #1174

zhusjfaker opened this issue May 20, 2024 · 4 comments

Comments

@zhusjfaker
Copy link
Collaborator

Is a module not found in rolldown an external module?

How to define an external module

from case
path: /crates/rolldown/tests/esbuild/packagejson/test_package_json_exports_alternatives
test_package_json_exports_alternatives

expect esbuild output in terminal

✘ [ERROR] Could not resolve "pkg/apples/red.js"

    src/test_package_json_exports_alternatives/src/entry.js:1:21:
      1 │ import redApple from 'pkg/apples/red.js'~~~~~~~~~~~~~~~~~~~

  The module "./good-apples/red.js" was not found on the file system:

    src/test_package_json_exports_alternatives/node_modules/pkg/package.json:3:18:
      3 │     "./apples/": ["./good-apples/", "./bad-apples/"],
        ╵                   ~~~~~~~~~~~~~~~~

  You can mark the path "pkg/apples/red.js" as external to exclude it from the bundle, which will
  remove this error and leave the unresolved path in the bundle.

✘ [ERROR] Could not resolve "pkg/books/red"

    src/test_package_json_exports_alternatives/src/entry.js:3:20:
      3 │ import redBook from 'pkg/books/red'~~~~~~~~~~~~~~~

  The module "./good-books/red-book.js" was not found on the file system:

    src/test_package_json_exports_alternatives/node_modules/pkg/package.json:4:18:
      4 │     "./books/*": ["./good-books/*-book.js", "./bad-books/*-book.js"]
        ╵                   ~~~~~~~~~~~~~~~~~~~~~~~~

  You can mark the path "pkg/books/red" as external to exclude it from the bundle, which will remove
  this error and leave the unresolved path in the bundle.

but now rolldown will bundle process result is successful

dist/entry.js

// These modules are treated as external. not throw any error or panic 
import { default as redApple } from "pkg/apples/red.js";
import { default as redBook } from "pkg/books/red";

// node_modules/pkg/good-apples/green.js
var green_default = '\u{1f34f}';

// node_modules/pkg/good-books/green-book.js
var green_book_default = '\u{1f4d7}';

// src/entry.js
console.log({
	redApple,
	greenApple:green_default,
	redBook,
	greenBook:green_book_default
});

Cause analysis

crates/rolldown_plugin/src/utils/resolve_id_with_plugins.rs

// line 72
let resolved = resolver.resolve(importer.map(Path::new), request, import_kind)?;
  Ok(resolved.map(|resolved| ResolvedRequestInfo {
    path: resolved.path,
    module_type: resolved.module_type,
    is_external: false,
    package_json: resolved.package_json,
  }))

// Instead of dealing with oxc-resolver errors, we opt for direct passthroughs, 
// whereas oxc returns a not-found error if it can't resolve.

crates/rolldown/src/module_loader/normal_module_task.rs

// line 288

/* 
Here you can clearly see that all the not-found errors from the previous passthrough are treated as external modules. 
 */
  
match resolved_id {
        Ok(info) => {
          ret.push(info);
        }
        Err(e) => match &e {
          ResolveError::NotFound(..) => {
            warnings.push(
              BuildError::unresolved_import_treated_as_external(
                specifier.to_string(),
                self.resolved_path.path.to_string(),
                Some(e),
              )
              .with_severity_warning(),
            );
            ret.push(ResolvedRequestInfo {
              path: specifier.to_string().into(),
              module_type: ModuleType::Unknown,
              is_external: true,
              package_json: None,
            });
          }
          _ => {
            build_errors.push((&dependencies[idx], e));
          }
        },
      }
    }
@zhusjfaker

This comment was marked as spam.

@zhusjfaker
Copy link
Collaborator Author

Personally, I think external is a local module that needs to be tagged or http, if it's written as a local module and not tagged, it should throw an error.

@hyf0
Copy link
Member

hyf0 commented May 20, 2024

We used to throw errors for implicit external modules. And for better DX, I changed to throw warnings rather than errors, which will terminate the build.

We could add a option to control wether throw errors for implicit external modules. Let's wait for feedbacks.

@hyf0 hyf0 changed the title [Need for discussion] Is a module not found in rolldown an external module? How to define an external module Rolldown should throw errors for implicit external modules May 20, 2024
@zhusjfaker
Copy link
Collaborator Author

We used to throw errors for implicit external modules. And for better DX, I changed to throw warnings rather than errors, which will terminate the build.

We could add a option to control wether throw errors for implicit external modules. Let's wait for feedbacks.

If there's a final conclusion, maybe I can help implement the code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants