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

fix(core): fix a bug where a package's code could not load its own package #779

Merged
merged 8 commits into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions fixtures/node_modules/elem-react-dom/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions fixtures/node_modules/elem-react-dom/package.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions fixtures/node_modules/elem-react-dom/server.edge.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions fixtures/test_modules/test-react-dom.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const assert = require("assert");

import * as dom1 from "elem-react-dom/server.edge";
assert.ok(dom1.name == "react-dom/server.edge");

const dom2 = require("elem-react-dom/server.edge");
assert.ok(dom2.name == "react-dom/server.edge");
9 changes: 6 additions & 3 deletions llrt_core/src/module_loader/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ pub fn require_resolve<'a>(
// 5. LOAD_PACKAGE_SELF(X, dirname(Y))
if let Ok(Some(path)) = load_package_self(ctx, x, &dirname_y, is_esm) {
trace!("+- Resolved by `LOAD_PACKAGE_SELF`: {}\n", path);
return to_abs_path(path.into());
richarddavison marked this conversation as resolved.
Show resolved Hide resolved
return Ok(path.into());
}

// 6. LOAD_NODE_MODULES(X, dirname(Y))
Expand Down Expand Up @@ -534,7 +534,7 @@ fn load_package_self(ctx: &Ctx<'_>, x: &str, dir: &str, is_esm: bool) -> Result<
// 1. Find the closest package scope SCOPE to DIR.
let mut package_json_file: Vec<u8>;
let package_json: BorrowedValue;
match find_the_closest_package_scope(dir) {
let package_json_path: Box<str> = match find_the_closest_package_scope(dir) {
// 2. If no scope was found, return.
None => {
return Ok(None);
Expand All @@ -552,6 +552,7 @@ fn load_package_self(ctx: &Ctx<'_>, x: &str, dir: &str, is_esm: bool) -> Result<
return Ok(None);
}
}
path
},
};
// 5. let MATCH = PACKAGE_EXPORTS_RESOLVE(pathToFileURL(SCOPE),
Expand All @@ -560,7 +561,9 @@ fn load_package_self(ctx: &Ctx<'_>, x: &str, dir: &str, is_esm: bool) -> Result<
// 6. RESOLVE_ESM_MATCH(MATCH)
if let Ok((path, _)) = package_exports_resolve(&package_json, &name, is_esm) {
trace!("| load_package_self(2.c): {}", path);
return Ok(Some(path.into()));
let dir = package_json_path.trim_end_matches("package.json");
let module_path = correct_extensions([dir, path].concat());
return Ok(Some(module_path.into()));
}

Ok(None)
Expand Down
4 changes: 4 additions & 0 deletions tests/unit/require.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,10 @@ it("require `uuid` module element", () => {
_require(`${CWD}/fixtures/test_modules/test-uuid.js`);
});

it("require `react-dom` module element", () => {
_require(`${CWD}/fixtures/test_modules/test-react-dom.js`);
});

//create a test that spawns a subprocess and executes require.mjs from fixtures and captures stdout
it("should handle blocking requires", (done) => {
const proc = spawn(process.argv0, [`${CWD}/fixtures/require.mjs`]);
Expand Down
Loading