diff --git a/cli/node/mod.rs b/cli/node/mod.rs index d0c3ed03f3..64e08becb7 100644 --- a/cli/node/mod.rs +++ b/cli/node/mod.rs @@ -522,7 +522,7 @@ pub fn node_resolve_npm_reference( npm_resolver, ) .with_context(|| { - format!("Error resolving package config for '{}'.", reference) + format!("Error resolving package config for '{}'", reference) })?; let resolved_path = match maybe_resolved_path { Some(resolved_path) => resolved_path, diff --git a/cli/tests/npm_tests.rs b/cli/tests/npm_tests.rs index 05dc83cfd2..07e36850f8 100644 --- a/cli/tests/npm_tests.rs +++ b/cli/tests/npm_tests.rs @@ -279,7 +279,7 @@ mod npm { exit_code: 1, }); - itest!(types { + itest!(types_general { args: "check --quiet npm/types/main.ts", output: "npm/types/main.out", envs: env_vars_for_npm_tests(), diff --git a/cli/tests/testdata/npm/registry/@denotest/types-exports-subpaths/1.0.0/client.d.ts b/cli/tests/testdata/npm/registry/@denotest/types-exports-subpaths/1.0.0/client.d.ts new file mode 100644 index 0000000000..2d156f5bb9 --- /dev/null +++ b/cli/tests/testdata/npm/registry/@denotest/types-exports-subpaths/1.0.0/client.d.ts @@ -0,0 +1 @@ +export function getClient(): 5; diff --git a/cli/tests/testdata/npm/registry/@denotest/types-exports-subpaths/1.0.0/dist/client.mjs b/cli/tests/testdata/npm/registry/@denotest/types-exports-subpaths/1.0.0/dist/client.mjs new file mode 100644 index 0000000000..938238d952 --- /dev/null +++ b/cli/tests/testdata/npm/registry/@denotest/types-exports-subpaths/1.0.0/dist/client.mjs @@ -0,0 +1,3 @@ +export function getClient() { + return 5; +} diff --git a/cli/tests/testdata/npm/registry/@denotest/types-exports-subpaths/1.0.0/dist/entry-c.d.ts b/cli/tests/testdata/npm/registry/@denotest/types-exports-subpaths/1.0.0/dist/entry-c.d.ts new file mode 100644 index 0000000000..e18f8b5516 --- /dev/null +++ b/cli/tests/testdata/npm/registry/@denotest/types-exports-subpaths/1.0.0/dist/entry-c.d.ts @@ -0,0 +1 @@ +export function entryC(): 12; diff --git a/cli/tests/testdata/npm/registry/@denotest/types-exports-subpaths/1.0.0/dist/entry-c.js b/cli/tests/testdata/npm/registry/@denotest/types-exports-subpaths/1.0.0/dist/entry-c.js new file mode 100644 index 0000000000..070b1ccbd6 --- /dev/null +++ b/cli/tests/testdata/npm/registry/@denotest/types-exports-subpaths/1.0.0/dist/entry-c.js @@ -0,0 +1,3 @@ +export function entryC() { + return 12; +} diff --git a/cli/tests/testdata/npm/registry/@denotest/types-exports-subpaths/1.0.0/entry-b.d.ts b/cli/tests/testdata/npm/registry/@denotest/types-exports-subpaths/1.0.0/entry-b.d.ts new file mode 100644 index 0000000000..79c9c84343 --- /dev/null +++ b/cli/tests/testdata/npm/registry/@denotest/types-exports-subpaths/1.0.0/entry-b.d.ts @@ -0,0 +1,2 @@ +// it will go to this and not the types entry because this entry was first +export function entryB(): string; \ No newline at end of file diff --git a/cli/tests/testdata/npm/registry/@denotest/types-exports-subpaths/1.0.0/package.json b/cli/tests/testdata/npm/registry/@denotest/types-exports-subpaths/1.0.0/package.json new file mode 100644 index 0000000000..35a1cd9f5e --- /dev/null +++ b/cli/tests/testdata/npm/registry/@denotest/types-exports-subpaths/1.0.0/package.json @@ -0,0 +1,19 @@ +{ + "name": "@denotest/types-exports-subpaths", + "version": "1.0.0", + "exports": { + "./client": { + "types": { + "default": "./client.d.ts" + }, + "import": "./dist/client.mjs" + }, + "./entry-b": { + "import": "./entry-b.d.ts", + "types": "./dist/entry-b-wrong.d.ts" + }, + "./entry-c": { + "import": "./dist/entry-c.js" + } + } +} diff --git a/cli/tests/testdata/npm/types/main.out b/cli/tests/testdata/npm/types/main.out index c439234784..47d72f74c4 100644 --- a/cli/tests/testdata/npm/types/main.out +++ b/cli/tests/testdata/npm/types/main.out @@ -1,7 +1,7 @@ error: TS2322 [ERROR]: Type 'number' is not assignable to type 'string'. bar: 1, ~~~ - at [WILDCARD]/npm/types/main.ts:7:3 + at [WILDCARD]/npm/types/main.ts:[WILDCARD] The expected type comes from property 'bar' which is declared here on type 'Foobar' bar: string; @@ -11,7 +11,7 @@ error: TS2322 [ERROR]: Type 'number' is not assignable to type 'string'. TS2322 [ERROR]: Type 'number' is not assignable to type 'string'. prop: 1, ~~~~ - at [WILDCARD]/npm/types/main.ts:11:3 + at [WILDCARD]/npm/types/main.ts:[WILDCARD] The expected type comes from property 'prop' which is declared here on type 'SomeInterface' prop: string; @@ -21,7 +21,7 @@ TS2322 [ERROR]: Type 'number' is not assignable to type 'string'. TS2322 [ERROR]: Type 'string' is not assignable to type 'number'. prop2: "asdf", ~~~~~ - at [WILDCARD]/npm/types/main.ts:12:3 + at [WILDCARD]/npm/types/main.ts:[WILDCARD] The expected type comes from property 'prop2' which is declared here on type 'SomeInterface' prop2: number; @@ -31,7 +31,7 @@ TS2322 [ERROR]: Type 'string' is not assignable to type 'number'. TS2322 [ERROR]: Type 'number' is not assignable to type 'string'. fizz: 1, ~~~~ - at [WILDCARD]/npm/types/main.ts:16:3 + at [WILDCARD]/npm/types/main.ts:[WILDCARD] The expected type comes from property 'fizz' which is declared here on type 'Fizzbuzz' fizz: string; @@ -41,11 +41,26 @@ TS2322 [ERROR]: Type 'number' is not assignable to type 'string'. TS2322 [ERROR]: Type 'number' is not assignable to type 'string'. buzz: 2, ~~~~ - at [WILDCARD]/npm/types/main.ts:17:3 + at [WILDCARD]/npm/types/main.ts:[WILDCARD] The expected type comes from property 'buzz' which is declared here on type 'Fizzbuzz' buzz: string; ~~~~ at [WILDCARD]/@denotest/types/1.0.0/index.d.ts:3:3 -Found 5 errors. +TS2322 [ERROR]: Type '5' is not assignable to type '"test1"'. +const valueA: "test1" = getClient(); + ~~~~~~ + at [WILDCARD]/npm/types/main.ts:[WILDCARD] + +TS2322 [ERROR]: Type 'string' is not assignable to type '"test2"'. +const valueB: "test2" = entryB(); + ~~~~~~ + at [WILDCARD]/types/main.ts:[WILDCARD] + +TS2322 [ERROR]: Type '12' is not assignable to type '"test3"'. +const valueC: "test3" = entryC(); + ~~~~~~ + at [WILDCARD]/types/main.ts:[WILDCARD] + +Found 8 errors. diff --git a/cli/tests/testdata/npm/types/main.ts b/cli/tests/testdata/npm/types/main.ts index 324ed723f7..fb8b65c980 100644 --- a/cli/tests/testdata/npm/types/main.ts +++ b/cli/tests/testdata/npm/types/main.ts @@ -1,6 +1,9 @@ import type { Fizzbuzz } from "npm:@denotest/types"; import type { SomeInterface } from "npm:@denotest/types_imported"; import type { Foobar as FooInterface } from "npm:@denotest/types_imported/subpath"; +import { getClient } from "npm:@denotest/types-exports-subpaths/client"; +import { entryB } from "npm:@denotest/types-exports-subpaths/entry-b"; +import { entryC } from "npm:@denotest/types-exports-subpaths/entry-c"; const foobar: FooInterface = { foo: "foo", @@ -16,3 +19,7 @@ const fizzbuzz: Fizzbuzz = { fizz: 1, buzz: 2, }; + +const valueA: "test1" = getClient(); +const valueB: "test2" = entryB(); +const valueC: "test3" = entryC(); diff --git a/ext/node/errors.rs b/ext/node/errors.rs index 929f51e1b3..b489cb9373 100644 --- a/ext/node/errors.rs +++ b/ext/node/errors.rs @@ -1,5 +1,7 @@ // Copyright 2018-2022 the Deno authors. All rights reserved. MIT license. +use std::path::PathBuf; + use deno_core::error::generic_error; use deno_core::error::type_error; use deno_core::error::AnyError; @@ -61,13 +63,26 @@ pub fn err_invalid_package_target( ) -> AnyError { let rel_error = !is_import && !target.is_empty() && !target.starts_with("./"); let mut msg = "[ERR_INVALID_PACKAGE_TARGET]".to_string(); + let pkg_json_path = PathBuf::from(pkg_path).join("package.json"); if key == "." { assert!(!is_import); - msg = format!("{} Invalid \"exports\" main target {} defined in the package config {}package.json", msg, target, pkg_path) + msg = format!( + "{} Invalid \"exports\" main target {} defined in the package config {}", + msg, + target, + pkg_json_path.display() + ) } else { let ie = if is_import { "imports" } else { "exports" }; - msg = format!("{} Invalid \"{}\" target {} defined for '{}' in the package config {}package.json", msg, ie, target, key, pkg_path) + msg = format!( + "{} Invalid \"{}\" target {} defined for '{}' in the package config {}", + msg, + ie, + target, + key, + pkg_json_path.display() + ) }; if let Some(base) = maybe_referrer { diff --git a/ext/node/resolution.rs b/ext/node/resolution.rs index 855bebc71d..10f085070f 100644 --- a/ext/node/resolution.rs +++ b/ext/node/resolution.rs @@ -502,14 +502,6 @@ fn resolve_package_target( { let condition_target = target_obj.get(key).unwrap().to_owned(); - if mode.is_types() - && key.as_str() != "types" - && condition_target.is_string() - { - // skip because this isn't a types entry - continue; - } - let resolved = resolve_package_target( package_json_path, condition_target,