0
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-03-03 09:31:22 -05:00

fix(repl): handle @types/node not being cached in the repl (#17617)

Closes #17599
This commit is contained in:
David Sherret 2023-02-01 16:37:05 -05:00 committed by GitHub
parent f6f76a76b2
commit dc854e83a4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 45 additions and 7 deletions

View file

@ -4,6 +4,7 @@ use test_util as util;
use test_util::assert_contains;
use test_util::assert_ends_with;
use test_util::assert_not_contains;
use util::TempDir;
#[test]
fn pty_multiline() {
@ -146,7 +147,6 @@ fn pty_complete_expression() {
console.write_line("close();");
let output = console.read_all_output();
assert_contains!(output, "Display all");
assert_contains!(output, "core");
assert_contains!(output, "args");
assert_contains!(output, "exit");
assert_contains!(output, "symlink");
@ -895,6 +895,11 @@ fn repl_with_quiet_flag() {
fn npm_packages() {
let mut env_vars = util::env_vars_for_npm_tests();
env_vars.push(("NO_COLOR".to_owned(), "1".to_owned()));
let temp_dir = TempDir::new();
env_vars.push((
"DENO_DIR".to_string(),
temp_dir.path().to_string_lossy().to_string(),
));
{
let (out, err) = util::run_and_collect_output_with_args(
@ -947,7 +952,7 @@ fn npm_packages() {
true,
vec!["repl", "--quiet", "--allow-read", "--allow-env"],
Some(vec![r#"import foo from "npm:asdfawe52345asdf""#]),
Some(env_vars),
Some(env_vars.clone()),
true,
);
@ -957,4 +962,20 @@ fn npm_packages() {
);
assert!(err.is_empty());
}
{
let (out, err) = util::run_and_collect_output_with_args(
true,
vec!["repl", "--quiet", "--allow-read", "--allow-env"],
Some(vec![
"import path from 'node:path';",
"path.isGlob('asdf') ? 'yes' : 'no'",
]),
Some(env_vars.clone()),
true,
);
assert_contains!(out, "no");
assert!(err.is_empty());
}
}

View file

@ -398,7 +398,9 @@ impl ReplSession {
scope_analysis: false,
})?;
self.check_for_npm_imports(&parsed_module.program()).await?;
self
.check_for_npm_or_node_imports(&parsed_module.program())
.await?;
let transpiled_src = parsed_module
.transpile(&deno_ast::EmitOptions {
@ -428,14 +430,14 @@ impl ReplSession {
})
}
async fn check_for_npm_imports(
async fn check_for_npm_or_node_imports(
&mut self,
program: &swc_ast::Program,
) -> Result<(), AnyError> {
let mut collector = ImportCollector::new();
program.visit_with(&mut collector);
let npm_imports = collector
let resolved_imports = collector
.imports
.iter()
.flat_map(|i| {
@ -445,11 +447,17 @@ impl ReplSession {
.as_ref()
.and_then(|resolver| resolver.resolve(i, &self.referrer).ok())
.or_else(|| ModuleSpecifier::parse(i).ok())
.and_then(|url| NpmPackageReference::from_specifier(&url).ok())
})
.collect::<Vec<_>>();
let npm_imports = resolved_imports
.iter()
.flat_map(|url| NpmPackageReference::from_specifier(url).ok())
.map(|r| r.req)
.collect::<Vec<_>>();
if !npm_imports.is_empty() {
let has_node_specifier =
resolved_imports.iter().any(|url| url.scheme() == "node");
if !npm_imports.is_empty() || has_node_specifier {
if !self.has_initialized_node_runtime {
self.proc_state.prepare_node_std_graph().await?;
crate::node::initialize_runtime(
@ -465,6 +473,15 @@ impl ReplSession {
.npm_resolver
.add_package_reqs(npm_imports)
.await?;
// prevent messages in the repl about @types/node not being cached
if has_node_specifier {
self
.proc_state
.npm_resolver
.inject_synthetic_types_node_package()
.await?;
}
}
Ok(())
}