From 184c9b9b28878dbb841ea5147e3501885344f41f Mon Sep 17 00:00:00 2001 From: David Sherret Date: Thu, 24 Nov 2022 13:13:51 -0500 Subject: [PATCH] fix(npm/types): resolve main entrypoint declaration file when no types entry (#16791) Closes #16782 --- cli/tests/integration/npm_tests.rs | 8 +++++ .../types-no-types-entry/1.0.0/dist/main.d.ts | 1 + .../types-no-types-entry/1.0.0/dist/main.js | 1 + .../types-no-types-entry/1.0.0/package.json | 5 +++ .../npm/types_no_types_entry/main.out | 4 +++ .../testdata/npm/types_no_types_entry/main.ts | 4 +++ ext/node/resolution.rs | 33 +++++++++++-------- 7 files changed, 43 insertions(+), 13 deletions(-) create mode 100644 cli/tests/testdata/npm/registry/@denotest/types-no-types-entry/1.0.0/dist/main.d.ts create mode 100644 cli/tests/testdata/npm/registry/@denotest/types-no-types-entry/1.0.0/dist/main.js create mode 100644 cli/tests/testdata/npm/registry/@denotest/types-no-types-entry/1.0.0/package.json create mode 100644 cli/tests/testdata/npm/types_no_types_entry/main.out create mode 100644 cli/tests/testdata/npm/types_no_types_entry/main.ts diff --git a/cli/tests/integration/npm_tests.rs b/cli/tests/integration/npm_tests.rs index afd023c851..ea577d412e 100644 --- a/cli/tests/integration/npm_tests.rs +++ b/cli/tests/integration/npm_tests.rs @@ -306,6 +306,14 @@ itest!(types_entry_value_not_exists { exit_code: 0, }); +itest!(types_no_types_entry { + args: "run --check=all npm/types_no_types_entry/main.ts", + output: "npm/types_no_types_entry/main.out", + envs: env_vars(), + http_server: true, + exit_code: 0, +}); + #[test] fn parallel_downloading() { let (out, _err) = util::run_and_collect_output_with_args( diff --git a/cli/tests/testdata/npm/registry/@denotest/types-no-types-entry/1.0.0/dist/main.d.ts b/cli/tests/testdata/npm/registry/@denotest/types-no-types-entry/1.0.0/dist/main.d.ts new file mode 100644 index 0000000000..2341a14f08 --- /dev/null +++ b/cli/tests/testdata/npm/registry/@denotest/types-no-types-entry/1.0.0/dist/main.d.ts @@ -0,0 +1 @@ +export function getValue(): 5; diff --git a/cli/tests/testdata/npm/registry/@denotest/types-no-types-entry/1.0.0/dist/main.js b/cli/tests/testdata/npm/registry/@denotest/types-no-types-entry/1.0.0/dist/main.js new file mode 100644 index 0000000000..d0c5dbc70c --- /dev/null +++ b/cli/tests/testdata/npm/registry/@denotest/types-no-types-entry/1.0.0/dist/main.js @@ -0,0 +1 @@ +module.exports.getValue = () => 5; diff --git a/cli/tests/testdata/npm/registry/@denotest/types-no-types-entry/1.0.0/package.json b/cli/tests/testdata/npm/registry/@denotest/types-no-types-entry/1.0.0/package.json new file mode 100644 index 0000000000..377af2bae6 --- /dev/null +++ b/cli/tests/testdata/npm/registry/@denotest/types-no-types-entry/1.0.0/package.json @@ -0,0 +1,5 @@ +{ + "name": "@denotest/types-no-types-entry", + "version": "1.0.0", + "main": "./dist/main.js" +} diff --git a/cli/tests/testdata/npm/types_no_types_entry/main.out b/cli/tests/testdata/npm/types_no_types_entry/main.out new file mode 100644 index 0000000000..6c5d45222d --- /dev/null +++ b/cli/tests/testdata/npm/types_no_types_entry/main.out @@ -0,0 +1,4 @@ +Download http://localhost:4545/npm/registry/@denotest/types-no-types-entry +Download http://localhost:4545/npm/registry/@denotest/types-no-types-entry/1.0.0.tgz +Check file://[WILDCARD]/types_no_types_entry/main.ts +5 diff --git a/cli/tests/testdata/npm/types_no_types_entry/main.ts b/cli/tests/testdata/npm/types_no_types_entry/main.ts new file mode 100644 index 0000000000..15e0604020 --- /dev/null +++ b/cli/tests/testdata/npm/types_no_types_entry/main.ts @@ -0,0 +1,4 @@ +import { getValue } from "npm:@denotest/types-no-types-entry"; + +const result: 5 = getValue(); +console.log(result); diff --git a/ext/node/resolution.rs b/ext/node/resolution.rs index 409f9fcb5b..6f61ebff2a 100644 --- a/ext/node/resolution.rs +++ b/ext/node/resolution.rs @@ -806,19 +806,31 @@ pub fn legacy_main_resolve( ) -> Result, AnyError> { let is_types = conditions == TYPES_CONDITIONS; let maybe_main = if is_types { - package_json.types.as_ref() + match package_json.types.as_ref() { + Some(types) => Some(types), + None => { + // fallback to checking the main entrypoint for + // a corresponding declaration file + if let Some(main) = package_json.main(referrer_kind) { + let main = package_json.path.parent().unwrap().join(main).clean(); + let path = path_to_declaration_path(main, referrer_kind); + if path.exists() { + return Ok(Some(path)); + } + } + None + } + } } else { package_json.main(referrer_kind) }; - let mut guess; if let Some(main) = maybe_main { - guess = package_json.path.parent().unwrap().join(main).clean(); + let guess = package_json.path.parent().unwrap().join(main).clean(); if file_exists(&guess) { return Ok(Some(guess)); } - let mut found = false; // todo(dsherret): investigate exactly how node and typescript handles this let endings = if is_types { match referrer_kind { @@ -838,22 +850,17 @@ pub fn legacy_main_resolve( vec![".js", "/index.js"] }; for ending in endings { - guess = package_json + let guess = package_json .path .parent() .unwrap() .join(&format!("{}{}", main, ending)) .clean(); if file_exists(&guess) { - found = true; - break; + // TODO(bartlomieju): emitLegacyIndexDeprecation() + return Ok(Some(guess)); } } - - if found { - // TODO(bartlomieju): emitLegacyIndexDeprecation() - return Ok(Some(guess)); - } } let index_file_names = if is_types { @@ -866,7 +873,7 @@ pub fn legacy_main_resolve( vec!["index.js"] }; for index_file_name in index_file_names { - guess = package_json + let guess = package_json .path .parent() .unwrap()