From 821a4ae5fd92a03dacf125b35bcf977343d27310 Mon Sep 17 00:00:00 2001 From: Ryan Dahl Date: Mon, 4 May 2020 09:39:40 -0400 Subject: [PATCH] Make it so ts compiler doesn't call cwd op (#5070) Removes duplicate implementation of the module resolution algorithm --- cli/compilers/ts.rs | 3 ++ cli/js/compiler.ts | 4 ++- cli/js/compiler/host.ts | 9 ++++-- cli/js/compiler/imports.ts | 63 ++++++++++---------------------------- 4 files changed, 29 insertions(+), 50 deletions(-) diff --git a/cli/compilers/ts.rs b/cli/compilers/ts.rs index bad2be5b26..8a02efeeae 100644 --- a/cli/compilers/ts.rs +++ b/cli/compilers/ts.rs @@ -177,6 +177,7 @@ fn req( bundle: bool, unstable: bool, ) -> Buf { + let cwd = std::env::current_dir().unwrap(); let j = match (compiler_config.path, compiler_config.content) { (Some(config_path), Some(config_data)) => json!({ "type": request_type as i32, @@ -187,6 +188,7 @@ fn req( "unstable": unstable, "configPath": config_path, "config": str::from_utf8(&config_data).unwrap(), + "cwd": cwd, }), _ => json!({ "type": request_type as i32, @@ -195,6 +197,7 @@ fn req( "outFile": out_file, "bundle": bundle, "unstable": unstable, + "cwd": cwd, }), }; diff --git a/cli/js/compiler.ts b/cli/js/compiler.ts index 385b76eec8..d3cd67119d 100644 --- a/cli/js/compiler.ts +++ b/cli/js/compiler.ts @@ -54,6 +54,7 @@ interface CompilerRequestCompile { unstable: boolean; bundle: boolean; outFile?: string; + cwd: string; } interface CompilerRequestRuntimeCompile { @@ -100,6 +101,7 @@ async function compile( rootNames, target, unstable, + cwd, } = request; util.log(">>> compile start", { rootNames, @@ -132,7 +134,7 @@ async function compile( // if there is a configuration supplied, we need to parse that if (config && config.length && configPath) { - const configResult = host.configure(configPath, config); + const configResult = host.configure(cwd, configPath, config); diagnostics = processConfigureResponse(configResult, configPath); } diff --git a/cli/js/compiler/host.ts b/cli/js/compiler/host.ts index afe184d3e6..de2eacfa9c 100644 --- a/cli/js/compiler/host.ts +++ b/cli/js/compiler/host.ts @@ -2,7 +2,6 @@ import { ASSETS, MediaType, SourceFile } from "./sourcefile.ts"; import { OUT_DIR, WriteFileCallback, getAsset } from "./util.ts"; -import { cwd } from "../ops/fs/dir.ts"; import { assert, notImplemented } from "../util.ts"; import * as util from "../util.ts"; @@ -167,7 +166,11 @@ export class Host implements ts.CompilerHost { } } - configure(path: string, configurationText: string): ConfigureResponse { + configure( + cwd: string, + path: string, + configurationText: string + ): ConfigureResponse { util.log("compiler::host.configure", path); assert(configurationText); const { config, error } = ts.parseConfigFileTextToJson( @@ -179,7 +182,7 @@ export class Host implements ts.CompilerHost { } const { options, errors } = ts.convertCompilerOptionsFromJson( config.compilerOptions, - cwd() + cwd ); const ignoredOptions: string[] = []; for (const key of Object.keys(options)) { diff --git a/cli/js/compiler/imports.ts b/cli/js/compiler/imports.ts index 4261a41230..de44027586 100644 --- a/cli/js/compiler/imports.ts +++ b/cli/js/compiler/imports.ts @@ -1,60 +1,30 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. import { MediaType, SourceFile, SourceFileJson } from "./sourcefile.ts"; -import { normalizeString, CHAR_FORWARD_SLASH } from "./util.ts"; -import { cwd } from "../ops/fs/dir.ts"; import { assert } from "../util.ts"; import * as util from "../util.ts"; import * as compilerOps from "../ops/compiler.ts"; -function resolvePath(...pathSegments: string[]): string { - let resolvedPath = ""; - let resolvedAbsolute = false; - - for (let i = pathSegments.length - 1; i >= -1 && !resolvedAbsolute; i--) { - let path: string; - - if (i >= 0) path = pathSegments[i]; - else path = cwd(); - - // Skip empty entries - if (path.length === 0) { - continue; - } - - resolvedPath = `${path}/${resolvedPath}`; - resolvedAbsolute = path.charCodeAt(0) === CHAR_FORWARD_SLASH; - } - - // At this point the path should be resolved to a full absolute path, but - // handle relative paths to be safe (might happen when cwd() fails) - - // Normalize the path - resolvedPath = normalizeString( - resolvedPath, - !resolvedAbsolute, - "/", - (code) => code === CHAR_FORWARD_SLASH - ); - - if (resolvedAbsolute) { - if (resolvedPath.length > 0) return `/${resolvedPath}`; - else return "/"; - } else if (resolvedPath.length > 0) return resolvedPath; - else return "."; -} - function resolveSpecifier(specifier: string, referrer: string): string { - if (!specifier.startsWith(".")) { - return specifier; + // The resolveModules op only handles fully qualified URLs for referrer. + // However we will have cases where referrer is "/foo.ts". We add this dummy + // prefix "file://" in order to use the op. + // TODO(ry) Maybe we should perhaps ModuleSpecifier::resolve_import() to + // handle this situation. + let dummyPrefix = false; + const prefix = "file://"; + if (referrer.startsWith("/")) { + dummyPrefix = true; + referrer = prefix + referrer; } - const pathParts = referrer.split("/"); - pathParts.pop(); - let path = pathParts.join("/"); - path = path.endsWith("/") ? path : `${path}/`; - return resolvePath(path, specifier); + let r = resolveModules([specifier], referrer)[0]; + if (dummyPrefix) { + r = r.replace(prefix, ""); + } + return r; } +// TODO(ry) Remove. Unnecessary redirection to compilerOps.resolveModules. export function resolveModules( specifiers: string[], referrer?: string @@ -63,6 +33,7 @@ export function resolveModules( return compilerOps.resolveModules(specifiers, referrer); } +// TODO(ry) Remove. Unnecessary redirection to compilerOps.fetchSourceFiles. function fetchSourceFiles( specifiers: string[], referrer?: string