From e01664d0ae6259022a901b0dea026ca350a49446 Mon Sep 17 00:00:00 2001 From: Kitson Kelly Date: Wed, 28 Oct 2020 20:38:09 +1100 Subject: [PATCH] fix(cli): module graph handles redirects properly (#8159) Fixes #8154 --- cli/module_graph2.rs | 23 +++++++++++++++++------ cli/tests/integration_tests.rs | 6 ++++++ cli/tests/redirect_cache.out | 6 ++++++ cli/tests/subdir/redirects/a.ts | 9 +++++++++ cli/tests/subdir/redirects/b.ts | 5 +++++ 5 files changed, 43 insertions(+), 6 deletions(-) create mode 100644 cli/tests/redirect_cache.out create mode 100644 cli/tests/subdir/redirects/a.ts create mode 100644 cli/tests/subdir/redirects/b.ts diff --git a/cli/module_graph2.rs b/cli/module_graph2.rs index 6c25ca8f21..df2e5fc612 100644 --- a/cli/module_graph2.rs +++ b/cli/module_graph2.rs @@ -763,7 +763,14 @@ impl Graph2 { let root_names: Vec<(ModuleSpecifier, MediaType)> = self .roots .iter() - .map(|ms| (ms.clone(), self.get_media_type(ms).unwrap())) + .map(|ms| { + ( + // root modules can be redirects, so before we pass it to tsc we need + // to resolve the redirect + self.resolve_specifier(ms).clone(), + self.get_media_type(ms).unwrap(), + ) + }) .collect(); let maybe_tsbuildinfo = self.maybe_tsbuildinfo.clone(); let hash_data = @@ -793,7 +800,9 @@ impl Graph2 { for emit in &response.emitted_files { if let Some(specifiers) = &emit.maybe_specifiers { assert!(specifiers.len() == 1, "Unexpected specifier length"); - let specifier = specifiers[0].clone(); + // The specifier emitted might not be the redirected specifier, and + // therefore we need to ensure it is the correct one. + let specifier = graph.resolve_specifier(&specifiers[0]); // Sometimes if tsc sees a CommonJS file it will _helpfully_ output it // to ESM, which we don't really want unless someone has enabled the // check_js option. @@ -805,10 +814,10 @@ impl Graph2 { } match emit.media_type { MediaType::JavaScript => { - codes.insert(specifier, emit.data.clone()); + codes.insert(specifier.clone(), emit.data.clone()); } MediaType::SourceMap => { - maps.insert(specifier, emit.data.clone()); + maps.insert(specifier.clone(), emit.data.clone()); } _ => unreachable!(), } @@ -1139,9 +1148,11 @@ impl Graph2 { // instead. let result = if prefer_types && dep_module.maybe_types.is_some() { let (_, types) = dep_module.maybe_types.clone().unwrap(); - types + // It is possible that `types` points to a redirected specifier, so we + // need to ensure it resolves to the final specifier in the graph. + self.resolve_specifier(&types).clone() } else { - resolved_specifier + dep_module.specifier.clone() }; Ok(result) diff --git a/cli/tests/integration_tests.rs b/cli/tests/integration_tests.rs index cfc946a364..da0a311bb8 100644 --- a/cli/tests/integration_tests.rs +++ b/cli/tests/integration_tests.rs @@ -2859,6 +2859,12 @@ itest!(proto_exploit { output: "proto_exploit.js.out", }); +itest!(redirect_cache { + http_server: true, + args: "cache --reload http://localhost:4548/cli/tests/subdir/redirects/a.ts", + output: "redirect_cache.out", +}); + itest!(deno_test_coverage { args: "test --coverage --unstable test_coverage.ts", output: "test_coverage.out", diff --git a/cli/tests/redirect_cache.out b/cli/tests/redirect_cache.out new file mode 100644 index 0000000000..ad26d01086 --- /dev/null +++ b/cli/tests/redirect_cache.out @@ -0,0 +1,6 @@ +Download http://localhost:4548/cli/tests/subdir/redirects/a.ts +Download http://localhost:4546/cli/tests/subdir/redirects/a.ts +Download http://localhost:4545/cli/tests/subdir/redirects/a.ts +Download http://localhost:4545/cli/tests/subdir/redirects/b.ts +Download http://localhost:4545/cli/tests/subdir/redirects/a.ts +Check http://localhost:4548/cli/tests/subdir/redirects/a.ts diff --git a/cli/tests/subdir/redirects/a.ts b/cli/tests/subdir/redirects/a.ts new file mode 100644 index 0000000000..071ee4728e --- /dev/null +++ b/cli/tests/subdir/redirects/a.ts @@ -0,0 +1,9 @@ +import { createA } from "./b.ts"; + +export class A { + private _a = "a"; +} + +export function start(): A { + return createA(); +} diff --git a/cli/tests/subdir/redirects/b.ts b/cli/tests/subdir/redirects/b.ts new file mode 100644 index 0000000000..cdb71eaae4 --- /dev/null +++ b/cli/tests/subdir/redirects/b.ts @@ -0,0 +1,5 @@ +import { A } from "./a.ts"; + +export function createA(): A { + return new A(); +}