Skip to content

Commit

Permalink
fix(node): handle empty 'main' entry in pkg json (#23155)
Browse files Browse the repository at this point in the history
  • Loading branch information
dsherret authored Apr 1, 2024
1 parent b8af46e commit 8698492
Show file tree
Hide file tree
Showing 12 changed files with 43 additions and 4 deletions.
7 changes: 4 additions & 3 deletions ext/node/package_json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,12 +213,13 @@ impl PackageJson {
Ok(package_json)
}

pub fn main(&self, referrer_kind: NodeModuleKind) -> Option<&String> {
if referrer_kind == NodeModuleKind::Esm && self.typ == "module" {
pub fn main(&self, referrer_kind: NodeModuleKind) -> Option<&str> {
let main = if referrer_kind == NodeModuleKind::Esm && self.typ == "module" {
self.module.as_ref().or(self.main.as_ref())
} else {
self.main.as_ref()
}
};
main.map(|m| m.trim()).filter(|m| !m.is_empty())
}

pub fn specifier(&self) -> ModuleSpecifier {
Expand Down
2 changes: 1 addition & 1 deletion ext/node/resolution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1232,7 +1232,7 @@ impl NodeResolver {
) -> Result<Option<ModuleSpecifier>, AnyError> {
let maybe_main = if mode.is_types() {
match package_json.types.as_ref() {
Some(types) => Some(types),
Some(types) => Some(types.as_str()),
None => {
// fallback to checking the main entrypoint for
// a corresponding declaration file
Expand Down
10 changes: 10 additions & 0 deletions tests/specs/check/npm_pkg_empty_main_entry/__test__.jsonc
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"steps": [{
"args": "check index.ts",
"output": "check.out",
"exitCode": 1
}, {
"args": "run index.ts",
"output": "run.out"
}]
}
5 changes: 5 additions & 0 deletions tests/specs/check/npm_pkg_empty_main_entry/check.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Check file:///[WILDLINE]/index.ts
error: TS2322 [ERROR]: Type 'number' is not assignable to type 'string'.
const result: string = add(1, 2);
~~~~~~
at file:///[WILDLINE]/index.ts:4:7
3 changes: 3 additions & 0 deletions tests/specs/check/npm_pkg_empty_main_entry/deno.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"unstable": ["byonm"]
}
5 changes: 5 additions & 0 deletions tests/specs/check/npm_pkg_empty_main_entry/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// this package has an empty "main" entry in its package.json for both the package and @types/package
import { add } from "package";

const result: string = add(1, 2);
console.log(result);

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.

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.

6 changes: 6 additions & 0 deletions tests/specs/check/npm_pkg_empty_main_entry/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"dependencies": {
"package": "*",
"@types/package": "*"
}
}
1 change: 1 addition & 0 deletions tests/specs/check/npm_pkg_empty_main_entry/run.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3

0 comments on commit 8698492

Please sign in to comment.