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(check/npm): move not found errors inside npm packages to tsc diagnostics #28337

Merged
Show file tree
Hide file tree
Changes from all 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
19 changes: 14 additions & 5 deletions cli/tsc/97_ts_host.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,8 @@ export const LAST_REQUEST_SCOPE = {
},
};

ts.deno.setIsNodeSourceFileCallback((sourceFile) => {
/** @param sourceFile {ts.SourceFile} */
function isNodeSourceFile(sourceFile) {
const fileName = sourceFile.fileName;
let isNodeSourceFile = IS_NODE_SOURCE_FILE_CACHE.get(fileName);
if (isNodeSourceFile == null) {
Expand All @@ -183,7 +184,9 @@ ts.deno.setIsNodeSourceFileCallback((sourceFile) => {
IS_NODE_SOURCE_FILE_CACHE.set(fileName, isNodeSourceFile);
}
return isNodeSourceFile;
});
}

ts.deno.setIsNodeSourceFileCallback(isNodeSourceFile);

/**
* @param msg {string}
Expand Down Expand Up @@ -344,8 +347,6 @@ const IGNORED_DIAGNOSTICS = [
// Microsoft/TypeScript#26825 but that doesn't seem to be working here,
// so we will ignore complaints about this compiler setting.
5070,
// TS6053: File '{0}' not found.
6053,
// TS7016: Could not find a declaration file for module '...'. '...'
// implicitly has an 'any' type. This is due to `allowJs` being off by
// default but importing of a JavaScript module.
Expand Down Expand Up @@ -785,7 +786,15 @@ export function filterMapDiagnostic(diagnostic) {
if (IGNORED_DIAGNOSTICS.includes(diagnostic.code)) {
return false;
}

// surface not found diagnostics inside npm packages
// because we don't analyze it with deno_graph
if (
// TS6053: File '{0}' not found.
diagnostic.code === 6053 &&
(diagnostic.file == null || !isNodeSourceFile(diagnostic.file))
) {
return false;
}
// make the diagnostic for using an `export =` in an es module a warning
if (diagnostic.code === 1203) {
diagnostic.category = ts.DiagnosticCategory.Warning;
Expand Down
29 changes: 18 additions & 11 deletions cli/tsc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use std::borrow::Cow;
use std::collections::HashMap;
use std::fmt;
use std::io::ErrorKind;
use std::path::PathBuf;
use std::rc::Rc;
use std::sync::Arc;
Expand Down Expand Up @@ -656,22 +657,28 @@ fn op_load_inner(
npm_state: Option<&RequestNpmState>,
media_type: &mut MediaType,
is_cjs: &mut bool,
) -> Result<FastString, LoadError> {
) -> Result<Option<FastString>, LoadError> {
*media_type = MediaType::from_specifier(specifier);
let file_path = specifier.to_file_path().unwrap();
let code = std::fs::read_to_string(&file_path).map_err(|err| {
LoadError::LoadFromNodeModule {
path: file_path.display().to_string(),
error: err,
let code = match std::fs::read_to_string(&file_path) {
Ok(code) => code,
Err(err) if err.kind() == ErrorKind::NotFound => {
return Ok(None);
}
})?;
Err(err) => {
return Err(LoadError::LoadFromNodeModule {
path: file_path.display().to_string(),
error: err,
})
}
};
let code: Arc<str> = code.into();
*is_cjs = npm_state
.map(|npm_state| {
npm_state.cjs_tracker.is_cjs(specifier, *media_type, &code)
})
.unwrap_or(false);
Ok(code.into())
Ok(Some(code.into()))
}

let state = state.borrow_mut::<State>();
Expand Down Expand Up @@ -755,12 +762,12 @@ fn op_load_inner(
&CliSys::default(),
&module.specifier,
);
Some(load_from_node_modules(
load_from_node_modules(
&specifier,
state.maybe_npm.as_ref(),
&mut media_type,
&mut is_cjs,
)?)
)?
}
}
}
Expand All @@ -769,12 +776,12 @@ fn op_load_inner(
.as_ref()
.filter(|npm| npm.node_resolver.in_npm_package(specifier))
{
Some(load_from_node_modules(
load_from_node_modules(
specifier,
Some(npm),
&mut media_type,
&mut is_cjs,
)?)
)?
} else {
None
};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"args": "check --all main.ts",
"output": "check.out",
"exitCode": 1
}
7 changes: 7 additions & 0 deletions tests/specs/check/module_not_found_npm_pkg_internal/check.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Check file:///[WILDLINE]/main.ts
TS6053 [ERROR]: File 'file:///[WILDLINE]/non-existent.d.ts' not found.
/// <reference path="./non-existent.d.ts" />
~~~~~~~~~~~~~~~~~~~
at file:///[WILDLINE]/index.d.ts:1:22

error: Type checking failed.
3 changes: 3 additions & 0 deletions tests/specs/check/module_not_found_npm_pkg_internal/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { Test } from "package";

console.log(Test);

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.

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"dependencies": {
"package": "*"
}
}