Skip to content

Commit

Permalink
refactor(tsc): do not store some typescript declaration file text in …
Browse files Browse the repository at this point in the history
…multiple places (denoland#17410)
  • Loading branch information
dsherret committed Jan 14, 2023
1 parent 429ccff commit 1712a88
Show file tree
Hide file tree
Showing 4 changed files with 173 additions and 157 deletions.
58 changes: 12 additions & 46 deletions cli/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ mod ts {
"es2018.regexp",
"es2019.array",
"es2019",
"es2019.intl",
"es2019.object",
"es2019.string",
"es2019.symbol",
Expand All @@ -116,6 +117,7 @@ mod ts {
"es2022.error",
"es2022.intl",
"es2022.object",
"es2022.sharedmemory",
"es2022.string",
"esnext",
"esnext.array",
Expand Down Expand Up @@ -150,6 +152,15 @@ mod ts {
build_libs.push(op_lib.to_owned());
}

// used in the tests to verify that after snapshotting it has the same number
// of lib files loaded and hasn't included any ones lazily loaded from Rust
std::fs::write(
PathBuf::from(env::var_os("OUT_DIR").unwrap())
.join("lib_file_names.json"),
serde_json::to_string(&build_libs).unwrap(),
)
.unwrap();

#[op]
fn op_build_info(state: &mut OpState) -> Value {
let build_specifier = "asset:https:///bootstrap.ts";
Expand Down Expand Up @@ -196,7 +207,7 @@ mod ts {
// we need a basic file to send to tsc to warm it up.
if args.specifier == build_specifier {
Ok(json!({
"data": r#"console.log("hello deno!");"#,
"data": r#"Deno.writeTextFile("hello.txt", "hello deno!");"#,
"version": "1",
// this corresponds to `ts.ScriptKind.TypeScript`
"scriptKind": 3
Expand Down Expand Up @@ -423,51 +434,6 @@ fn main() {
println!("cargo:rustc-env=TS_VERSION={}", ts::version());
println!("cargo:rerun-if-env-changed=TS_VERSION");

println!(
"cargo:rustc-env=DENO_CONSOLE_LIB_PATH={}",
deno_console::get_declaration().display()
);
println!(
"cargo:rustc-env=DENO_URL_LIB_PATH={}",
deno_url::get_declaration().display()
);
println!(
"cargo:rustc-env=DENO_WEB_LIB_PATH={}",
deno_web::get_declaration().display()
);
println!(
"cargo:rustc-env=DENO_FETCH_LIB_PATH={}",
deno_fetch::get_declaration().display()
);
println!(
"cargo:rustc-env=DENO_WEBGPU_LIB_PATH={}",
deno_webgpu_get_declaration().display()
);
println!(
"cargo:rustc-env=DENO_WEBSOCKET_LIB_PATH={}",
deno_websocket::get_declaration().display()
);
println!(
"cargo:rustc-env=DENO_WEBSTORAGE_LIB_PATH={}",
deno_webstorage::get_declaration().display()
);
println!(
"cargo:rustc-env=DENO_CACHE_LIB_PATH={}",
deno_cache::get_declaration().display()
);
println!(
"cargo:rustc-env=DENO_CRYPTO_LIB_PATH={}",
deno_crypto::get_declaration().display()
);
println!(
"cargo:rustc-env=DENO_BROADCAST_CHANNEL_LIB_PATH={}",
deno_broadcast_channel::get_declaration().display()
);
println!(
"cargo:rustc-env=DENO_NET_LIB_PATH={}",
deno_net::get_declaration().display()
);

println!("cargo:rustc-env=TARGET={}", env::var("TARGET").unwrap());
println!("cargo:rustc-env=PROFILE={}", env::var("PROFILE").unwrap());

Expand Down
29 changes: 22 additions & 7 deletions cli/lsp/tsc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ impl AssetDocument {
type AssetsMap = HashMap<ModuleSpecifier, AssetDocument>;

fn new_assets_map() -> Arc<Mutex<AssetsMap>> {
let assets = tsc::STATIC_ASSETS
let assets = tsc::LAZILY_LOADED_STATIC_ASSETS
.iter()
.map(|(k, v)| {
let url_str = format!("asset:https:///{}", k);
Expand Down Expand Up @@ -3455,6 +3455,8 @@ mod tests {
use crate::lsp::documents::Documents;
use crate::lsp::documents::LanguageId;
use crate::lsp::text::LineIndex;
use crate::tsc::AssetText;
use pretty_assertions::assert_eq;
use std::path::Path;
use std::path::PathBuf;
use test_util::TempDir;
Expand Down Expand Up @@ -3913,18 +3915,31 @@ mod tests {
Default::default(),
)
.unwrap();
let assets = result.as_array().unwrap();
let assets: Vec<AssetText> = serde_json::from_value(result).unwrap();
let mut asset_names = assets
.iter()
.map(|a| {
a.specifier
.replace("asset:https:///lib.", "")
.replace(".d.ts", "")
})
.collect::<Vec<_>>();
let mut expected_asset_names: Vec<String> = serde_json::from_str(
include_str!(concat!(env!("OUT_DIR"), "/lib_file_names.json")),
)
.unwrap();

asset_names.sort();
expected_asset_names.sort();

// You might have found this assertion starts failing after upgrading TypeScript.
// Just update the new number of assets (declaration files) for this number.
assert_eq!(assets.len(), 72);
// Ensure build.rs is updated so these match.
assert_eq!(asset_names, expected_asset_names);

// get some notification when the size of the assets grows
let mut total_size = 0;
for asset in assets {
let obj = asset.as_object().unwrap();
let text = obj.get("text").unwrap().as_str().unwrap();
total_size += text.len();
total_size += asset.text.len();
}
assert!(total_size > 0);
assert!(total_size < 2_000_000); // currently as of TS 4.6, it's 0.7MB
Expand Down
43 changes: 28 additions & 15 deletions cli/tsc/99_main_compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@ delete Object.prototype.__proto__;
// paths must be either relative or absolute. Since
// analysis in Rust operates on fully resolved URLs,
// it makes sense to use the same scheme here.
const ASSETS = "asset:https:///";
const ASSETS_URL_PREFIX = "asset:https:///";

/** Diagnostics that are intentionally ignored when compiling TypeScript in
* Deno, as they provide misleading or incorrect information. */
Expand Down Expand Up @@ -431,6 +431,7 @@ delete Object.prototype.__proto__;
noEmit: true,
strict: true,
target: ts.ScriptTarget.ESNext,
lib: ["lib.deno.window.d.ts"],
};

// todo(dsherret): can we remove this and just use ts.OperationCanceledException?
Expand Down Expand Up @@ -546,10 +547,10 @@ delete Object.prototype.__proto__;
return sourceFile;
},
getDefaultLibFileName() {
return `${ASSETS}/lib.esnext.d.ts`;
return `${ASSETS_URL_PREFIX}lib.esnext.d.ts`;
},
getDefaultLibLocation() {
return ASSETS;
return ASSETS_URL_PREFIX;
},
writeFile(fileName, data, _writeByteOrderMark, _onError, _sourceFiles) {
if (logDebug) {
Expand Down Expand Up @@ -887,6 +888,20 @@ delete Object.prototype.__proto__;
debug("<<< exec stop");
}

function getAssets() {
/** @type {{ specifier: string; text: string; }[]} */
const assets = [];
for (const sourceFile of sourceFileCache.values()) {
if (sourceFile.fileName.startsWith(ASSETS_URL_PREFIX)) {
assets.push({
specifier: sourceFile.fileName,
text: sourceFile.text,
});
}
}
return assets;
}

/**
* @param {number} id
* @param {any} data
Expand Down Expand Up @@ -935,16 +950,7 @@ delete Object.prototype.__proto__;
);
}
case "getAssets": {
const assets = [];
for (const sourceFile of sourceFileCache.values()) {
if (sourceFile.fileName.startsWith(ASSETS)) {
assets.push({
specifier: sourceFile.fileName,
text: sourceFile.text,
});
}
}
return respond(id, assets);
return respond(id, getAssets());
}
case "getApplicableRefactors": {
return respond(
Expand Down Expand Up @@ -1281,7 +1287,10 @@ delete Object.prototype.__proto__;
// we are caching in memory common type libraries that will be re-used by
// tsc on when the snapshot is restored
assert(
host.getSourceFile(`${ASSETS}${specifier}`, ts.ScriptTarget.ESNext),
host.getSourceFile(
`${ASSETS_URL_PREFIX}${specifier}`,
ts.ScriptTarget.ESNext,
),
);
}
// this helps ensure as much as possible is in memory that is re-usable
Expand All @@ -1292,12 +1301,16 @@ delete Object.prototype.__proto__;
options: SNAPSHOT_COMPILE_OPTIONS,
host,
});
ts.getPreEmitDiagnostics(TS_SNAPSHOT_PROGRAM);
assert(ts.getPreEmitDiagnostics(TS_SNAPSHOT_PROGRAM).length === 0);

// remove this now that we don't need it anymore for warming up tsc
sourceFileCache.delete(buildSpecifier);

// exposes the two functions that are called by `tsc::exec()` when type
// checking TypeScript.
globalThis.startup = startup;
globalThis.exec = exec;
globalThis.getAssets = getAssets;

// exposes the functions that are called when the compiler is used as a
// language service.
Expand Down
Loading

0 comments on commit 1712a88

Please sign in to comment.